use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.
the class QualifierDefaults method applyDefaults.
/**
* Applies default annotations to a type. A {@link com.sun.source.tree.Tree} determines the
* appropriate scope for defaults.
*
* <p>For instance, if the tree is associated with a declaration (e.g., it's the use of a field,
* or a method invocation), defaults in the scope of the <i>declaration</i> are used; if the tree
* is not associated with a declaration (e.g., a typecast), defaults in the scope of the tree are
* used.
*
* @param tree the tree associated with the type
* @param type the type to which defaults will be applied
* @see #applyDefaultsElement(javax.lang.model.element.Element,
* org.checkerframework.framework.type.AnnotatedTypeMirror)
*/
private void applyDefaults(Tree tree, AnnotatedTypeMirror type) {
// The location to take defaults from.
Element elt;
switch(tree.getKind()) {
case MEMBER_SELECT:
elt = TreeUtils.elementFromUse((MemberSelectTree) tree);
break;
case IDENTIFIER:
elt = TreeUtils.elementFromUse((IdentifierTree) tree);
if (ElementUtils.isTypeDeclaration(elt)) {
// If the Idenitifer is a type, then use the scope of the tree.
elt = nearestEnclosingExceptLocal(tree);
}
break;
case METHOD_INVOCATION:
elt = TreeUtils.elementFromUse((MethodInvocationTree) tree);
break;
default:
// If no associated symbol was found, use the tree's (lexical) scope.
elt = nearestEnclosingExceptLocal(tree);
}
// System.out.println("applyDefaults on tree " + tree +
// " gives elt: " + elt + "(" + elt.getKind() + ")");
boolean defaultTypeVarLocals = (atypeFactory instanceof GenericAnnotatedTypeFactory<?, ?, ?, ?>) && ((GenericAnnotatedTypeFactory<?, ?, ?, ?>) atypeFactory).getShouldDefaultTypeVarLocals();
applyToTypeVar = defaultTypeVarLocals && elt != null && elt.getKind() == ElementKind.LOCAL_VARIABLE && type.getKind() == TypeKind.TYPEVAR;
applyDefaultsElement(elt, type);
applyToTypeVar = false;
}
use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method isDefaultSuperConstructorCall.
/**
* Returns whether a javac statement represents a method call {@code super()}.
*
* @param statement the javac statement to check
* @return true if statement is a method invocation named "super" with no arguments, false
* otherwise
*/
public static boolean isDefaultSuperConstructorCall(StatementTree statement) {
if (statement.getKind() != Tree.Kind.EXPRESSION_STATEMENT) {
return false;
}
ExpressionStatementTree expressionStatement = (ExpressionStatementTree) statement;
if (expressionStatement.getExpression().getKind() != Tree.Kind.METHOD_INVOCATION) {
return false;
}
MethodInvocationTree invocation = (MethodInvocationTree) expressionStatement.getExpression();
if (invocation.getMethodSelect().getKind() != Tree.Kind.IDENTIFIER) {
return false;
}
if (!((IdentifierTree) invocation.getMethodSelect()).getName().contentEquals("super")) {
return false;
}
return invocation.getArguments().isEmpty();
}
use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitAnnotation.
@Override
public Void visitAnnotation(AnnotationTree javacTree, Node javaParserNode) {
// as @MyAnno(value="myArg") which has a single element argument list with an assignment.
if (javaParserNode instanceof MarkerAnnotationExpr) {
processAnnotation(javacTree, (MarkerAnnotationExpr) javaParserNode);
} else if (javaParserNode instanceof SingleMemberAnnotationExpr) {
SingleMemberAnnotationExpr node = (SingleMemberAnnotationExpr) javaParserNode;
processAnnotation(javacTree, node);
assert javacTree.getArguments().size() == 1;
ExpressionTree value = javacTree.getArguments().get(0);
assert value instanceof AssignmentTree;
AssignmentTree assignment = (AssignmentTree) value;
assert assignment.getVariable().getKind() == Tree.Kind.IDENTIFIER;
assert ((IdentifierTree) assignment.getVariable()).getName().contentEquals("value");
assignment.getExpression().accept(this, node.getMemberValue());
} else if (javaParserNode instanceof NormalAnnotationExpr) {
NormalAnnotationExpr node = (NormalAnnotationExpr) javaParserNode;
processAnnotation(javacTree, node);
assert javacTree.getArguments().size() == node.getPairs().size();
Iterator<MemberValuePair> argIter = node.getPairs().iterator();
for (ExpressionTree arg : javacTree.getArguments()) {
assert arg instanceof AssignmentTree;
AssignmentTree assignment = (AssignmentTree) arg;
IdentifierTree memberName = (IdentifierTree) assignment.getVariable();
MemberValuePair javaParserArg = argIter.next();
assert memberName.getName().contentEquals(javaParserArg.getNameAsString());
assignment.getExpression().accept(this, javaParserArg.getValue());
}
} else {
throwUnexpectedNodeType(javacTree, javaParserNode);
}
return null;
}
use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.
the class ExpectedTreesVisitor method visitClass.
@Override
public Void visitClass(ClassTree tree, Void p) {
defaultAction(tree);
scan(tree.getModifiers(), p);
scan(tree.getTypeParameters(), p);
scan(tree.getExtendsClause(), p);
scan(tree.getImplementsClause(), p);
if (tree.getKind() == Tree.Kind.ENUM) {
// instance of an enum.
for (Tree member : tree.getMembers()) {
member.accept(this, p);
if (member.getKind() != Tree.Kind.VARIABLE) {
continue;
}
VariableTree variable = (VariableTree) member;
ExpressionTree initializer = variable.getInitializer();
if (initializer == null || initializer.getKind() != Tree.Kind.NEW_CLASS) {
continue;
}
NewClassTree constructor = (NewClassTree) initializer;
if (constructor.getIdentifier().getKind() != Tree.Kind.IDENTIFIER) {
continue;
}
IdentifierTree name = (IdentifierTree) constructor.getIdentifier();
if (name.getName().contentEquals(tree.getSimpleName())) {
trees.remove(variable.getType());
trees.remove(constructor);
trees.remove(constructor.getIdentifier());
}
}
// RECORD was added in Java 14, so use string comparison to be JDK 8,11 compatible:
} else if (tree.getKind().name().equals("RECORD")) {
// A record like:
// record MyRec(String myField) {}
// will be expanded by javac to:
// class MyRec {
// MyRec(String myField) {
// super();
// }
// private final String myField;
// }
// So the constructor and the field declarations have no matching trees in the JavaParser
// node, and we must remove those trees (and their subtrees) from the `trees` field.
TreeScannerWithDefaults removeAllVisitor = new TreeScannerWithDefaults() {
@Override
public void defaultAction(Tree node) {
trees.remove(node);
}
};
for (Tree member : tree.getMembers()) {
scan(member, p);
if (TreeUtils.isAutoGeneratedRecordMember(member)) {
member.accept(removeAllVisitor, null);
} else {
// the parameters. These trees also don't have a match:
if (member.getKind() == Tree.Kind.METHOD) {
MethodTree methodTree = (MethodTree) member;
if (TreeUtils.isCompactCanonicalRecordConstructor(methodTree)) {
for (VariableTree canonicalParameter : methodTree.getParameters()) {
canonicalParameter.accept(removeAllVisitor, null);
}
}
}
}
}
} else {
scan(tree.getMembers(), p);
}
return null;
}
use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.
the class LockVisitor method visitIdentifier.
@Override
// AST node comparison
@SuppressWarnings("interning:not.interned")
public Void visitIdentifier(IdentifierTree tree, Void p) {
// (All other field accesses are checked in visitMemberSelect.)
if (TreeUtils.isFieldAccess(tree)) {
Tree parent = getCurrentPath().getParentPath().getLeaf();
// then the field is accessed via an implicit this.
if ((parent.getKind() != Tree.Kind.MEMBER_SELECT || ((MemberSelectTree) parent).getExpression() == tree) && !ElementUtils.isStatic(TreeUtils.elementFromUse(tree))) {
AnnotationMirror guardedBy = atypeFactory.getSelfType(tree).getAnnotationInHierarchy(atypeFactory.GUARDEDBY);
checkLockOfImplicitThis(tree, guardedBy);
}
}
return super.visitIdentifier(tree, p);
}
Aggregations