Skip to content

Commit

Permalink
Fix/ignore some type validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Oct 30, 2023
1 parent 6cfbf8f commit 6813d97
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ void test() {
@Test
void identifiesGuardsForControlParenthesesWithMissingTypeInformation() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()),
java(
"""
class Test {
Expand All @@ -309,6 +310,7 @@ void test() {
@Test
void doesNotFlagArbitraryParenthesesAsGuards() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()),
java(
"""
class Test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import java.util.stream.Stream;

Expand All @@ -45,46 +46,48 @@ static Stream<String> fileProvider() {
void eachJavaFile(String input) {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
// Force a case where data flow occurs inside a doAfterVisit on a non-top-level visitor run.
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
// The doAfterVisit
doAfterVisit(new JavaIsoVisitor<>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
doRunDataFlow();
return super.visitMethodInvocation(method, ctx);
}
});
return method;
}
}.visitNonNull(classDecl, ctx, getCursor().getParentOrThrow());
return super.visitClassDeclaration(classDecl, ctx);
}
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
// Force a case where data flow occurs inside a doAfterVisit on a non-top-level visitor run.
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
// The doAfterVisit
doAfterVisit(new JavaIsoVisitor<>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
doRunDataFlow();
return super.visitMethodInvocation(method, ctx);
}
});
return method;
}
}.visitNonNull(classDecl, ctx, getCursor().getParentOrThrow());
return super.visitClassDeclaration(classDecl, ctx);
}

@Override
public Expression visitExpression(Expression expression, ExecutionContext executionContext) {
doRunDataFlow();
return super.visitExpression(expression, executionContext);
}
@Override
public Expression visitExpression(Expression expression, ExecutionContext executionContext) {
doRunDataFlow();
return super.visitExpression(expression, executionContext);
}

private void doRunDataFlow() {
Dataflow.startingAt(getCursor()).findSinks(new TaintFlowSpec() {
@Override
public boolean isSource(DataFlowNode srcNode) {
return true;
}
private void doRunDataFlow() {
Dataflow.startingAt(getCursor()).findSinks(new TaintFlowSpec() {
@Override
public boolean isSource(DataFlowNode srcNode) {
return true;
}

@Override
public boolean isSink(DataFlowNode sinkNode) {
return true;
}
});
}
})).cycles(1),
@Override
public boolean isSink(DataFlowNode sinkNode) {
return true;
}
});
}
}))
.cycles(1)
.typeValidationOptions(TypeValidation.none()),
java(
StringUtils.readFully(requireNonNull(DataflowFunctionalTest.class
.getResourceAsStream("/" + input)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void noLiteralsPresent() {
"""
class Test {
boolean test(int i, float f, double d, char c, String s) {
boolean b1 = s.contains(c.toString());
boolean b1 = s.contains(Character.toString(c));
if (b1 && i - f == d) {
return i > d;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.openrewrite.java.Assertions.java;
Expand Down Expand Up @@ -167,6 +168,7 @@ void foo() throws IOException {
@Test
void javadocWithLink() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().methodInvocations(false).build()),
java(
"""
class Test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public FileUtils() {
/**
* An empty array of type <code>File</code>.
*/
public static final File[] EMPTY_FILE_ARRAY = new File.get(0);
public static final File[] EMPTY_FILE_ARRAY = new File[0];

/**
* The UTF-8 character set, used to decode octets in URLs.
Expand Down

0 comments on commit 6813d97

Please sign in to comment.