Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle annotations in TypeFromExpressionVisitor; fixes #5435 #5442

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions checker/tests/tainting/Issue5435.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Issue5435 {
public @interface A1 {}

public @interface A2 {
A1[] m() default {@A1()};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ protected void testAnnotationInsertion() {
System.lineSeparator(),
"Sanity check of erasing then reinserting annotations produced a different AST.",
"File: " + root.getSourceFile(),
"Original node: " + visitor.getMismatchedNode1(),
"Node with annotations re-inserted: " + visitor.getMismatchedNode2(),
"Node class: " + visitor.getMismatchedNode1().getClass().getSimpleName(),
"Original node: " + oneLine(visitor.getMismatchedNode1()),
"Node with annotations re-inserted: " + oneLine(visitor.getMismatchedNode2()),
"Original annotations: " + visitor.getMismatchedNode1().getAnnotations(),
"Re-inserted annotations: " + visitor.getMismatchedNode2().getAnnotations(),
"Original AST:",
Expand All @@ -454,6 +455,16 @@ protected void testAnnotationInsertion() {
}
}

/**
* Replace newlines in the printed representation by spaces.
*
* @param arg an object to format
* @return the object's toString representation, on one line
*/
private String oneLine(Object arg) {
return arg.toString().replace(System.lineSeparator(), " ");
}

/**
* Type-check classTree and skips classes specified by the skipDef option. Subclasses should
* override {@link #processClassTree(ClassTree)} instead of this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ public abstract class DoubleJavaParserVisitor extends VoidVisitorAdapter<Node> {
* corresponding elements in order.
*
* @param list1 first list of nodes
* @param list2 second list of nodes
* @param list2 second list of nodes, which has the same size as the first list
*/
private void visitLists(List<? extends Node> list1, List<? extends Node> list2) {
assert list1.size() == list2.size();
if (list1.size() != list2.size()) {
throw new Error(
String.format(
"%s.visitLists(%s [size %d], %s [size %d])",
this.getClass().getCanonicalName(), list1, list1.size(), list2, list2.size()));
}
for (int i = 0; i < list1.size(); i++) {
list1.get(i).accept(this, list2.get(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2247,7 +2247,15 @@ public abstract void processCompoundAssignment(
* @param javaParserNodes list of corresponding JavaParser nodes
*/
private void visitLists(List<? extends Tree> javacTrees, List<? extends Node> javaParserNodes) {
assert javacTrees.size() == javaParserNodes.size();
if (javacTrees.size() != javaParserNodes.size()) {
throw new BugInCF(
"%s.visitLists(%s [size %d], %s [size %d])",
this.getClass().getCanonicalName(),
javacTrees,
javacTrees.size(),
javaParserNodes,
javaParserNodes.size());
}
Iterator<? extends Node> nodeIter = javaParserNodes.iterator();
for (Tree tree : javacTrees) {
tree.accept(this, nodeIter.next());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.checkerframework.framework.type;

import com.sun.source.tree.AnnotatedTypeTree;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ArrayAccessTree;
import com.sun.source.tree.ArrayTypeTree;
import com.sun.source.tree.AssignmentTree;
Expand Down Expand Up @@ -74,6 +75,16 @@
*/
class TypeFromExpressionVisitor extends TypeFromTreeVisitor {

/** Creates a new TypeFromTreeVisitor. */
TypeFromExpressionVisitor() {
// nothing to do
}

@Override
public AnnotatedTypeMirror visitAnnotation(AnnotationTree node, AnnotatedTypeFactory f) {
return f.type(node);
}

@Override
public AnnotatedTypeMirror visitBinary(BinaryTree node, AnnotatedTypeFactory f) {
return f.type(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public AnnotatedTypeMirror defaultAction(Tree node, AnnotatedTypeFactory f) {
throw new BugInCF("TypeFromTree.defaultAction: null tree");
}
throw new BugInCF(
"TypeFromTree.defaultAction: conversion undefined for tree type " + node.getKind());
this.getClass().getCanonicalName()
+ ": conversion undefined for tree type "
+ node.getKind());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.ArrayInitializerExpr;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.StringLiteralExpr;
Expand Down Expand Up @@ -266,6 +267,11 @@ public void defaultAction(Node node) {
}
}
}

@Override
public void visit(ArrayInitializerExpr node, Void p) {
// Do not remove annotations that are array elements.
}
}

/**
Expand Down