Skip to content

Commit

Permalink
Handle annotations in TypeFromExpressionVisitor (typetools#5442)
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst authored and wmdietl committed Feb 7, 2023
1 parent 9a65ffd commit ad95747
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 7 deletions.
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 @@ -460,8 +460,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 @@ -470,6 +471,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 @@ -131,10 +131,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 @@ -2249,8 +2249,16 @@ public abstract void processCompoundAssignment(
* @param javacTrees list of trees
* @param javaParserNodes list of corresponding JavaParser nodes
*/
protected void visitLists(List<? extends Tree> javacTrees, List<? extends Node> javaParserNodes) {
assert javacTrees.size() == javaParserNodes.size();
private void visitLists(List<? extends Tree> javacTrees, List<? extends Node> javaParserNodes) {
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 @@ -77,6 +78,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 @@ -28,6 +28,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 @@ -269,6 +270,11 @@ public void defaultAction(Node node) {
}
}
}

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

/**
Expand Down

0 comments on commit ad95747

Please sign in to comment.