コード例 #1
0
ファイル: CFG.java プロジェクト: SonarSource/ruling_java
 private void buildMemberSelect(MemberSelectExpressionTree tree) {
   MemberSelectExpressionTree mse = tree;
   currentBlock.elements.add(mse);
   // int.class or String[].class are memberSelectExpression which expression part is not an
   // expression.
   if (!"class".equals(mse.identifier().name())) {
     build(mse.expression());
   }
 }
コード例 #2
0
 private static boolean isEqualsMethod(MethodInvocationTree syntaxNode) {
   if (syntaxNode.arguments().size() == 1) {
     ExpressionTree methodSelect = syntaxNode.methodSelect();
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       MemberSelectExpressionTree expression = (MemberSelectExpressionTree) methodSelect;
       if ("equals".equals(expression.identifier().name())
           && syntaxNode.symbol().isMethodSymbol()) {
         Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) syntaxNode.symbol();
         return symbol.parameterTypes().get(0).is("java.lang.Object");
       }
     }
   }
   return false;
 }
コード例 #3
0
ファイル: SecondPass.java プロジェクト: jalagars/sonar-java
 @Override
 public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
   scan(tree.expression());
   if (site.kind >= Symbol.ERRONEOUS) {
     return;
   }
   String name = tree.identifier().name();
   if (site.kind == Symbol.PCK) {
     env.packge = (Symbol.PackageSymbol) site;
     site = resolve.findIdentInPackage(env, site, name, Symbol.TYP | Symbol.PCK);
   } else {
     env.enclosingClass = (Symbol.TypeSymbol) site;
     site =
         resolve.findMemberType(env, (Symbol.TypeSymbol) site, name, (Symbol.TypeSymbol) site);
   }
   associateReference(tree.identifier(), site);
 }
コード例 #4
0
 private void visitMethodInvocationTree(MethodInvocationTree methodInvocationTree) {
   if (isValueOfInvocation(methodInvocationTree)) {
     checkForUnboxing(methodInvocationTree.arguments().get(0));
   } else if (isUnboxingMethodInvocation(methodInvocationTree)) {
     ExpressionTree methodSelect = methodInvocationTree.methodSelect();
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       MemberSelectExpressionTree memberSelectExpressionTree =
           (MemberSelectExpressionTree) methodSelect;
       checkForBoxing(memberSelectExpressionTree.expression());
     }
   } else {
     Symbol symbol = methodInvocationTree.symbol();
     if (symbol.isMethodSymbol()) {
       List<Type> parametersTypes = ((Symbol.MethodSymbol) symbol).parameterTypes();
       checkMethodInvocationArguments(methodInvocationTree, parametersTypes);
     }
   }
 }
コード例 #5
0
 private void checkForUnboxing(ExpressionTree expressionTree) {
   if (!expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
     return;
   }
   MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expressionTree;
   if (isUnboxingMethodInvocation(methodInvocationTree)) {
     ExpressionTree methodSelect = methodInvocationTree.methodSelect();
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       MemberSelectExpressionTree memberSelectExpressionTree =
           (MemberSelectExpressionTree) methodSelect;
       ExpressionTree unboxedExpression = memberSelectExpressionTree.expression();
       String unboxingResultTypeName = methodInvocationTree.symbolType().fullyQualifiedName();
       if (unboxingResultTypeName.equals(
           PRIMITIVE_TYPES_BY_WRAPPER.get(unboxedExpression.symbolType().fullyQualifiedName()))) {
         addUnboxingIssue(expressionTree, unboxedExpression);
       }
     }
   }
 }
コード例 #6
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();
  }
コード例 #7
0
  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());
  }
コード例 #8
0
 @Override
 public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
   IdentifierTree identifier = null;
   ExpressionTree expression = tree.expression();
   if (expression.is(Kind.IDENTIFIER)) {
     identifier = (IdentifierTree) expression;
   } else if (expression.is(Kind.PARENTHESIZED_EXPRESSION)
       && ((ParenthesizedTree) expression).expression().is(Kind.IDENTIFIER)) {
     identifier = (IdentifierTree) ((ParenthesizedTree) expression).expression();
   }
   if (!validUsagesStack.isEmpty() && identifier != null) {
     Iterator<Collection<IdentifierTree>> iterator = validUsagesStack.iterator();
     while (iterator.hasNext()) {
       iterator.next().remove(identifier);
     }
   }
   super.visitMemberSelectExpression(tree);
 }