@Override
 public void visitMethodInvocation(MethodInvocationTree syntaxNode) {
   final ExpressionTree methodSelect = syntaxNode.methodSelect();
   if (isClosingResource(syntaxNode.symbol())) {
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       final ExpressionTree targetExpression =
           ((MemberSelectExpressionTree) methodSelect).expression();
       if (targetExpression.is(Tree.Kind.IDENTIFIER)) {
         final IdentifierTree identifier = (IdentifierTree) targetExpression;
         programState = closeResource(programState, programState.getValue(identifier.symbol()));
       } else {
         programState = closeResource(programState, programState.peekValue());
       }
     }
   } else if (syntaxNode.methodSelect().is(Tree.Kind.MEMBER_SELECT)
       && isOpeningResultSet(syntaxNode.symbol())) {
     final SymbolicValue value = getTargetValue(syntaxNode);
     constraintManager.setValueFactory(new WrappedValueFactory(value));
   } else if (isClosingResultSets(syntaxNode.symbol())) {
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       final SymbolicValue value = getTargetValue(syntaxNode);
       closeResultSetsRelatedTo(value);
     }
   } else {
     closeArguments(syntaxNode.arguments(), 1);
   }
 }
 @Override
 public void visitIdentifier(IdentifierTree tree) {
   if (methodSymbol.equals(tree.symbol().owner())) {
     used.add(tree.symbol());
   }
   super.visitIdentifier(tree);
 }
 private void addBoxingIssue(Tree tree, Symbol classSymbol, Tree boxingArg) {
   if (boxingArg.is(Tree.Kind.IDENTIFIER)) {
     IdentifierTree identifier = (IdentifierTree) boxingArg;
     reportIssue(tree, "Remove the boxing of \"" + identifier.name() + "\".");
   } else {
     reportIssue(tree, "Remove the boxing to \"" + classSymbol.name() + "\".");
   }
 }
 private void addUnboxingIssue(ExpressionTree expressionTree, ExpressionTree expression) {
   if (expression.is(Tree.Kind.IDENTIFIER)) {
     IdentifierTree identifier = (IdentifierTree) expression;
     reportIssue(expressionTree, "Remove the unboxing of \"" + identifier.name() + "\".");
   } else {
     String name = expression.symbolType().name();
     reportIssue(expressionTree, "Remove the unboxing from \"" + name + "\".");
   }
 }
Ejemplo n.º 5
0
 public boolean isAnnotatedOverride() {
   for (AnnotationTree annotationTree : modifiers.annotations()) {
     if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
       IdentifierTree identifier = (IdentifierTree) annotationTree.annotationType();
       if (Override.class.getSimpleName().equals(identifier.name())) {
         return true;
       }
     }
   }
   return false;
 }
Ejemplo n.º 6
0
 private SymbolicValue createIdentifierSymbolicValue(IdentifierTree identifier) {
   final Type type = identifier.symbol().type();
   if (type != null && type.is("java.lang.Boolean")) {
     if ("TRUE".equals(identifier.name())) {
       return SymbolicValue.TRUE_LITERAL;
     } else if ("FALSE".equals(identifier.name())) {
       return SymbolicValue.FALSE_LITERAL;
     }
   }
   return createDefaultSymbolicValue();
 }
 private SymbolicValue getTargetValue(MethodInvocationTree syntaxNode) {
   final ExpressionTree targetExpression =
       ((MemberSelectExpressionTree) syntaxNode.methodSelect()).expression();
   final SymbolicValue value;
   if (targetExpression.is(Tree.Kind.IDENTIFIER)) {
     final IdentifierTree identifier = (IdentifierTree) targetExpression;
     value = programState.getValue(identifier.symbol());
   } else {
     value = programState.peekValue();
   }
   return value;
 }
Ejemplo n.º 8
0
 @CheckForNull
 private static Tree getAssignedVariable(ExpressionTree expression) {
   if (expression.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
     return getAssignedVariable(((ArrayAccessExpressionTree) expression).expression());
   }
   IdentifierTree identifier;
   if (expression.is(Tree.Kind.IDENTIFIER)) {
     identifier = (IdentifierTree) expression;
   } else {
     identifier = ((MemberSelectExpressionTree) expression).identifier();
   }
   return identifier.symbol().declaration();
 }
 @Override
 public void visitReturnStatement(ReturnStatementTree syntaxNode) {
   SymbolicValue currentVal = programState.peekValue();
   if (currentVal != null) {
     final ExpressionTree expression = syntaxNode.expression();
     if (expression != null) {
       if (expression.is(Tree.Kind.IDENTIFIER)) {
         final IdentifierTree identifier = (IdentifierTree) expression;
         currentVal = programState.getValue(identifier.symbol());
       } else {
         currentVal = programState.peekValue();
       }
       programState = closeResource(programState, currentVal);
     }
   }
 }
 private void checkIdentifier(IdentifierTree identifierTree) {
   if (loopCounters.contains(identifierTree.name())) {
     context.addIssue(
         identifierTree,
         ruleKey,
         "Refactor the code in order to not assign to this loop counter from within the loop body.");
   }
 }
Ejemplo n.º 11
0
 @Override
 public void visitClass(ClassTree tree) {
   String className = "";
   IdentifierTree simpleName = tree.simpleName();
   if (simpleName != null) {
     className = simpleName.name();
   }
   String key = getClassKey(className);
   currentClassKey.push(key);
   parent.push(tree);
   anonymousInnerClassCounter.push(0);
   resourcesCache.put(key, currentFile);
   handleSuppressWarning(tree);
   super.visitClass(tree);
   currentClassKey.pop();
   parent.pop();
   anonymousInnerClassCounter.pop();
 }
Ejemplo n.º 12
0
  private static String concatenate(ExpressionTree tree) {
    Deque<String> pieces = new LinkedList<>();
    ExpressionTree expr = tree;
    while (expr.is(Tree.Kind.MEMBER_SELECT)) {
      MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr;
      pieces.push(mse.identifier().name());
      pieces.push(".");
      expr = mse.expression();
    }
    if (expr.is(Tree.Kind.IDENTIFIER)) {
      IdentifierTree idt = (IdentifierTree) expr;
      pieces.push(idt.name());
    }

    StringBuilder sb = new StringBuilder();
    for (String piece : pieces) {
      sb.append(piece);
    }
    return sb.toString();
  }
  private boolean isQualifiedExcludedType(Tree tree) {
    if (!tree.is(Kind.MEMBER_SELECT)) {
      return false;
    }
    Deque<String> pieces = new LinkedList<>();
    ExpressionTree expr = (MemberSelectExpressionTree) tree;
    while (expr.is(Tree.Kind.MEMBER_SELECT)) {
      MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr;
      pieces.push(mse.identifier().name());
      pieces.push(".");
      expr = mse.expression();
    }
    if (expr.is(Tree.Kind.IDENTIFIER)) {
      IdentifierTree idt = (IdentifierTree) expr;
      pieces.push(idt.name());
    }

    StringBuilder sb = new StringBuilder();
    for (String piece : pieces) {
      sb.append(piece);
    }
    return Iterables.contains(exceptions, sb.toString());
  }
 private void addAssignment(IdentifierTree identifier) {
   Symbol reference = identifier.symbol();
   if (!reference.isUnknown()) {
     assignments.put(reference, identifier);
   }
 }
Ejemplo n.º 15
0
 @Override
 public void visitIdentifier(IdentifierTree tree) {
   site = resolve.findIdent(env, tree.name(), Symbol.TYP | Symbol.PCK);
   associateReference(tree, site);
 }
Ejemplo n.º 16
0
 @Override
 public void visitIdentifier(IdentifierTree tree) {
   super.visitIdentifier(tree);
   checkSymbol(tree.symbol());
 }