Skip to content

Commit

Permalink
Merge pull request #30 from aaronist/javadoc_support
Browse files Browse the repository at this point in the history
  • Loading branch information
JLLeitschuh authored Aug 30, 2023
2 parents 5189369 + 4e363b5 commit 6533cad
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/org/openrewrite/analysis/trait/expr/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openrewrite.analysis.trait.util.TraitErrors;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Javadoc;

import java.util.UUID;

Expand Down Expand Up @@ -62,7 +63,7 @@ public UUID getId() {
}

static Validation<TraitErrors, ExprFallback> viewOf(Cursor cursor) {
if (cursor.getValue() instanceof J.Identifier) {
if (cursor.getValue() instanceof J.Identifier && cursor.firstEnclosing(Javadoc.class) == null) {
return TraitErrors.invalidTraitCreation(ExprFallback.class, "Identifiers are only Expr when they are VarAccess");
}
if (cursor.getValue() instanceof J.FieldAccess && cursor.firstEnclosing(J.Import.class) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Javadoc;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -237,6 +238,9 @@ static Validation<TraitErrors, VarAccess> viewOf(Cursor cursor, J.Identifier ide
assert ident.getFieldType() == null;
return TraitErrors.invalidTraitCreationError("J.Identifier within a method invocation name is not a variable access");
}
if (cursor.firstEnclosing(Javadoc.class) != null) {
return TraitErrors.invalidTraitCreationError("J.Identifier as an argument in a method call directly only appears in Javadoc comments");
}
if (checkType(parent, J.MethodDeclaration.class, parentMethodDeclaration -> parentMethodDeclaration.getName() == ident)) {
assert ident.getFieldType() == null;
return TraitErrors.invalidTraitCreationError("J.Identifier within a method declaration name is not a variable access");
Expand Down
184 changes: 184 additions & 0 deletions src/test/java/org/openrewrite/analysis/trait/variable/JavadocTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.analysis.trait.variable;

import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.analysis.trait.expr.VarAccess;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;

public class JavadocTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J preVisit(J tree, ExecutionContext executionContext) {
return VarAccess.viewOf(getCursor())
.map(var -> {
assertNotNull(var.getVariable(), "VarAccess.getVariable() is null");
return SearchResult.found(tree);
})
.orSuccess(tree);
}
}));
}

@Test
void javadocWithSee() {
rewriteRun(
java(
"""
class Test {
/**
* @see #bar(String str)
*/
void foo() {
// no-op
}
void bar(String str)
{
// no-op
}
}
"""
)
);
}

@Test
void differentJavadocWithSee() {
rewriteRun(
java(
"""
class Test {
/**
* @see #bar(String)
*/
void foo() {
// no-op
}
void bar(String str)
{
// no-op
}
}
"""
)
);
}

@Test
void javadocStaticField() {
rewriteRun(
java(
"""
class Test {
/**
* @value #HELLO
*/
static final String HELLO = "hello";
}
"""
)
);
}

@Test
void javadocWithCode() {
rewriteRun(
java(
"""
class Test {
/**
* This method takes a {@code String} and
* a {@code int} as parameters
*/
void foobar(String s, int i) {
// no-op
}
}
"""
)
);
}

@Test
void javadocWithCodeHtml() {
rewriteRun(
java(
"""
class Test {
/**
* This method takes a <code>String</code> and
* a <code>int</code> as parameters
*/
void foobar(String s, int i) {
// no-op
}
}
"""
)
);
}

@Test
void javadocWithThrows() {
rewriteRun(
java(
"""
import java.io.IOException;
class Test {
/**
* @throws IOException This method throws an exception
*/
void foo() throws IOException {
throw new IOException();
}
}
"""
)
);
}

@Test
void javadocWithLink() {
rewriteRun(
java(
"""
class Test {
/**
* {@link #foo(String, float) foo}
*/
void foo(String s, float f) {
// no-op
}
}
"""
)
);
}
}

0 comments on commit 6533cad

Please sign in to comment.