Ejemplo n.º 1
0
 private void buildNewClass(NewClassTree tree) {
   currentBlock.elements.add(tree);
   ExpressionTree enclosingExpression = tree.enclosingExpression();
   if (enclosingExpression != null) {
     build(enclosingExpression);
   }
   build(Lists.reverse(tree.arguments()));
 }
 private static Symbol.TypeSymbol wrapperClassSymbol(NewClassTree newClassTree) {
   Symbol.TypeSymbol classSymbol = newClassTree.symbolType().symbol();
   if (PRIMITIVE_TYPES_BY_WRAPPER.containsKey(newClassTree.symbolType().fullyQualifiedName())
       && !newClassTree.arguments().isEmpty()) {
     return classSymbol;
   }
   return null;
 }
  @Override
  public void visitNewClass(NewClassTree tree) {
    if (TARGETED_CLASS.contains(getclassName(tree)) && tree.arguments().size() == 1) {
      ExpressionTree argument = tree.arguments().get(0);

      if (argument.is(Tree.Kind.CHAR_LITERAL)) {
        String character = ((LiteralTree) argument).value();
        context.reportIssue(
            this,
            argument,
            "Replace the constructor character parameter "
                + character
                + " with string parameter "
                + character.replace("'", "\"")
                + ".");
      }
    }
  }
 @Override
 public void visitNewClass(NewClassTree syntaxNode) {
   final List<SymbolicValue> arguments = programState.peekValues(syntaxNode.arguments().size());
   if (isOpeningResource(syntaxNode)) {
     Iterator<SymbolicValue> iterator = arguments.iterator();
     for (ExpressionTree argument : syntaxNode.arguments()) {
       if (!iterator.hasNext()) {
         throw new IllegalStateException(
             "Mismatch between declared constructor arguments and argument values!");
       }
       final Type type = argument.symbolType();
       final SymbolicValue value = iterator.next();
       if (isCloseable(type)) {
         constraintManager.setValueFactory(new WrappedValueFactory(value));
         break;
       }
     }
   } else {
     closeArguments(syntaxNode.arguments(), 0);
   }
 }
 private void checkForBoxing(ExpressionTree expression) {
   if (expression.is(Tree.Kind.NEW_CLASS)) {
     NewClassTree newClassTree = (NewClassTree) expression;
     Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
     if (classSymbol != null) {
       ExpressionTree boxingArg = newClassTree.arguments().get(0);
       if (boxingArg.symbolType().isPrimitive()) {
         addBoxingIssue(newClassTree, classSymbol, boxingArg);
       }
     }
   } else if (expression.is(Tree.Kind.METHOD_INVOCATION)) {
     MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expression;
     if (isValueOfInvocation(methodInvocationTree)) {
       ExpressionTree boxingArg = methodInvocationTree.arguments().get(0);
       addBoxingIssue(expression, methodInvocationTree.symbol().owner(), boxingArg);
     }
   }
 }
 @Override
 public void visitNode(Tree tree) {
   if (hasSemantic()) {
     if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
       visitMethodInvocationTree((MethodInvocationTree) tree);
     } else if (tree.is(Tree.Kind.VARIABLE)) {
       VariableTree variableTree = (VariableTree) tree;
       ExpressionTree initializer = variableTree.initializer();
       if (initializer != null) {
         checkExpression(initializer, variableTree.type().symbolType());
       }
     } else if (tree.is(Tree.Kind.ASSIGNMENT)) {
       AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) tree;
       checkExpression(assignmentTree.expression(), assignmentTree.symbolType());
     } else {
       NewClassTree newClassTree = (NewClassTree) tree;
       Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
       if (classSymbol != null) {
         checkForUnboxing(newClassTree.arguments().get(0));
       }
     }
   }
 }