Esempio 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()));
 }
 public static String getclassName(NewClassTree newClasstree) {
   if (newClasstree.identifier().is(Tree.Kind.MEMBER_SELECT)) {
     return ((MemberSelectExpressionTree) newClasstree.identifier()).identifier().name();
   } else if (newClasstree.identifier().is(Tree.Kind.IDENTIFIER)) {
     return ((IdentifierTree) newClasstree.identifier()).name();
   }
   return null;
 }
 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;
 }
 @Test
 public void getLine() {
   CompilationUnitTree tree = createTree("class A {\n" + "A a = new A() {};" + "\n}");
   ClassTree classTree = (ClassTree) tree.types().get(0);
   assertThat(((JavaTree) classTree).getLine()).isEqualTo(1);
   // get line of anonymous class
   NewClassTree newClassTree =
       (NewClassTree) ((VariableTree) classTree.members().get(0)).initializer();
   assertThat(((JavaTree) newClassTree.classBody()).getLine()).isEqualTo(2);
 }
 @Override
 public void visitNode(Tree tree) {
   NewClassTree newClassTree = (NewClassTree) tree;
   TypeTree newTypeTree = newClassTree.identifier();
   if (newClassTree.classBody() == null && isParameterizedType(newTypeTree)) {
     TypeTree type = getTypeFromExpression(tree.parent());
     if (type != null && isParameterizedType(type)) {
       reportIssue(
           ((ParameterizedTypeTree) newTypeTree).typeArguments(),
           "Replace the type specification in this constructor call with the diamond operator (\"<>\")."
               + JavaVersionHelper.java7CompatibilityMessage(context.getJavaVersion()));
     }
   }
 }
  @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("'", "\"")
                + ".");
      }
    }
  }
 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 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);
   }
 }
 @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));
       }
     }
   }
 }
 private static boolean isOpeningResource(NewClassTree syntaxNode) {
   if (isWithinTryHeader(syntaxNode)) {
     return false;
   }
   return needsClosing(syntaxNode.symbolType());
 }
Esempio n. 11
0
 @Override
 public void visitNewClass(NewClassTree tree) {
   super.visitNewClass(tree);
   checkSymbol(tree.symbolType().symbol());
 }