Java Code Examples for com.sun.source.tree.AssignmentTree#getExpression()

The following examples show how to use com.sun.source.tree.AssignmentTree#getExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static ExpressionTree findValue(AnnotationTree m, String name) {
    for (ExpressionTree et : m.getArguments()) {
        if (et.getKind() == Kind.ASSIGNMENT) {
            AssignmentTree at = (AssignmentTree) et;
            String varName = ((IdentifierTree) at.getVariable()).getName().toString();

            if (varName.equals(name)) {
                return at.getExpression();
            }
        }

        if (et instanceof LiteralTree/*XXX*/ && "value".equals(name)) {
            return et;
        }
    }

    return null;
}
 
Example 2
Source File: TreeBackedAnnotationValue.java    From buck with Apache License 2.0 6 votes vote down vote up
TreeBackedAnnotationValue(
    AnnotationValue underlyingAnnotationValue,
    TreePath treePath,
    PostEnterCanonicalizer canonicalizer) {
  this.underlyingAnnotationValue = underlyingAnnotationValue;
  Tree tree = treePath.getLeaf();
  if (tree instanceof AssignmentTree) {
    AssignmentTree assignmentTree = (AssignmentTree) tree;
    valueTree = assignmentTree.getExpression();
    this.treePath = new TreePath(treePath, valueTree);
  } else {
    valueTree = tree;
    this.treePath = treePath;
  }
  this.canonicalizer = canonicalizer;
}
 
Example 3
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static List<? extends TypeMirror> computeAssignment(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    AssignmentTree at = (AssignmentTree) parent.getLeaf();
    TypeMirror     type = null;
    
    if (at.getVariable() == error) {
        type = info.getTrees().getTypeMirror(new TreePath(parent, at.getExpression()));

        if (type != null) {
            //anonymous class?
            type = JavaPluginUtils.convertIfAnonymous(type);

            if (type.getKind() == TypeKind.EXECUTABLE) {
                //TODO: does not actualy work, attempt to solve situations like:
                //t = Collections.emptyList()
                //t = Collections.<String>emptyList();
                //see also testCreateFieldMethod1 and testCreateFieldMethod2 tests:
                type = ((ExecutableType) type).getReturnType();
            }
        }
    }
    
    if (at.getExpression() == error) {
        type = info.getTrees().getTypeMirror(new TreePath(parent, at.getVariable()));
    }
    
    //class or field:
    if (type == null) {
        return null;
    }
    
    types.add(ElementKind.PARAMETER);
    types.add(ElementKind.LOCAL_VARIABLE);
    types.add(ElementKind.FIELD);
    
    return Collections.singletonList(type);
}
 
Example 4
Source File: NullAway.java    From NullAway with MIT License 5 votes vote down vote up
@Override
public Description matchAssignment(AssignmentTree tree, VisitorState state) {
  if (!matchWithinClass) {
    return Description.NO_MATCH;
  }
  Type lhsType = ASTHelpers.getType(tree.getVariable());
  if (lhsType != null && lhsType.isPrimitive()) {
    return doUnboxingCheck(state, tree.getExpression());
  }
  Symbol assigned = ASTHelpers.getSymbol(tree.getVariable());
  if (assigned == null || assigned.getKind() != ElementKind.FIELD) {
    // not a field of nullable type
    return Description.NO_MATCH;
  }

  if (Nullness.hasNullableAnnotation(assigned, config)) {
    // field already annotated
    return Description.NO_MATCH;
  }
  ExpressionTree expression = tree.getExpression();
  if (mayBeNullExpr(state, expression)) {
    String message = "assigning @Nullable expression to @NonNull field";
    return errorBuilder.createErrorDescriptionForNullAssignment(
        new ErrorMessage(MessageTypes.ASSIGN_FIELD_NULLABLE, message),
        expression,
        buildDescription(tree),
        state);
  }
  return Description.NO_MATCH;
}
 
Example 5
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static List<? extends TypeMirror> computeAssignment(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    AssignmentTree at = (AssignmentTree) parent.getLeaf();
    TypeMirror     type = null;
    
    types.add(ElementKind.PARAMETER);
    types.add(ElementKind.LOCAL_VARIABLE);
    types.add(ElementKind.FIELD);

    if (at.getVariable() == error) {
        type = info.getTrees().getTypeMirror(new TreePath(parent, at.getExpression()));

        if (type != null) {
            //anonymous class?
            type = org.netbeans.modules.java.hints.errors.Utilities.convertIfAnonymous(type);

            if (type.getKind() == TypeKind.EXECUTABLE) {
                //TODO: does not actualy work, attempt to solve situations like:
                //t = Collections.emptyList()
                //t = Collections.<String>emptyList();
                //see also testCreateFieldMethod1 and testCreateFieldMethod2 tests:
                type = ((ExecutableType) type).getReturnType();
            }
        }

        if (parent.getParentPath() != null && parent.getParentPath().getLeaf().getKind() == Kind.TRY) {
            types.clear();
            types.add(ElementKind.RESOURCE_VARIABLE);
        }
    }
    
    if (at.getExpression() == error) {
        type = info.getTrees().getTypeMirror(new TreePath(parent, at.getVariable()));
    }
    
    //class or field:
    if (type == null) {
        if (ErrorHintsProvider.ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
            ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "offset=" + offset);
            ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "errorTree=" + error);
            ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "type=null");
        }
        
        return null;
    }
    
    return Collections.singletonList(type);
}
 
Example 6
Source File: UseNbBundleMessages.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isAlreadyRegistered(TreePath treePath, String key) {
    ModifiersTree modifiers;
    Tree tree = treePath.getLeaf();
    switch (tree.getKind()) {
    case METHOD:
        modifiers = ((MethodTree) tree).getModifiers();
        break;
    case VARIABLE:
        modifiers = ((VariableTree) tree).getModifiers();
        break;
    case CLASS:
    case ENUM:
    case INTERFACE:
    case ANNOTATION_TYPE:
        modifiers = ((ClassTree) tree).getModifiers();
        break;
    default:
        modifiers = null;
    }
    if (modifiers != null) {
        for (AnnotationTree ann : modifiers.getAnnotations()) {
            Tree annotationType = ann.getAnnotationType();
            if (annotationType.toString().matches("((org[.]openide[.]util[.])?NbBundle[.])?Messages")) { // XXX see above
                List<? extends ExpressionTree> args = ann.getArguments();
                if (args.size() != 1) {
                    continue; // ?
                }
                AssignmentTree assign = (AssignmentTree) args.get(0);
                if (!assign.getVariable().toString().equals("value")) {
                    continue; // ?
                }
                ExpressionTree arg = assign.getExpression();
                if (arg.getKind() == Tree.Kind.STRING_LITERAL) {
                    if (isRegistered(key, arg)) {
                        return true;
                    }
                } else if (arg.getKind() == Tree.Kind.NEW_ARRAY) {
                    for (ExpressionTree elt : ((NewArrayTree) arg).getInitializers()) {
                        if (isRegistered(key, elt)) {
                            return true;
                        }
                    }
                } else {
                    // ?
                }
            }
        }
    }
    TreePath parentPath = treePath.getParentPath();
    if (parentPath == null) {
        return false;
    }
    // XXX better to check all sources in the same package
    return isAlreadyRegistered(parentPath, key);
}