/* * @see ASTVisitor#visit(VariableDeclarationStatement) */ public boolean visit(VariableDeclarationStatement node) { if (node.getAST().apiLevel() >= AST.JLS3) { printModifiers(node.modifiers()); } node.getType().accept(this); this.fBuffer.append(" "); // $NON-NLS-1$ for (Iterator it = node.fragments().iterator(); it.hasNext(); ) { VariableDeclarationFragment f = (VariableDeclarationFragment) it.next(); f.accept(this); if (it.hasNext()) { this.fBuffer.append(", "); // $NON-NLS-1$ } } this.fBuffer.append(";"); // $NON-NLS-1$ return false; }
// generate a variable declaration with an initializer specified by the given expression // e.g. given x.f(a,b) ==> int y = x.f(a,b); public static List<Statement> genVarDeclStatement(Expression exp) { List<Statement> result = new ArrayList<Statement>(); VariableDeclarationFragment fragment = AST.newAST(AST.JLS8).newVariableDeclarationFragment(); ExpressionStatement assignmentStmt = genAssignmentStatement(exp); // The type of the generated variable Type varType = AST.newAST(AST.JLS8).newWildcardType(); if (exp.resolveTypeBinding() != null) { if (exp.resolveTypeBinding().isPrimitive()) { switch (exp.resolveTypeBinding().getName()) { case "void": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.VOID); break; case "int": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.INT); break; case "char": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.CHAR); break; case "long": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.LONG); break; case "boolean": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.BOOLEAN); break; case "float": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.FLOAT); break; case "short": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.SHORT); break; case "byte": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.BYTE); break; case "double": varType = AST.newAST(AST.JLS8).newPrimitiveType(PrimitiveType.DOUBLE); break; } } else { // Option 1: only use the simplename /* SimpleName typeName = AST.newAST(AST.JLS8).newSimpleName(exp.resolveTypeBinding().getName()); AST tempAST = AST.newAST(AST.JLS8); varType = tempAST.newSimpleType((Name) ASTNode.copySubtree(tempAST, typeName)); */ varType = resolveQualifiedType(exp.resolveTypeBinding().getQualifiedName()); } } // Declaration Fragment fragment.setName( (SimpleName) ASTNode.copySubtree( fragment.getAST(), ((SimpleName) ((Assignment) assignmentStmt.getExpression()).getLeftHandSide()))); // fragment.setInitializer((Expression) ASTNode.copySubtree(fragment.getAST(), exp)); AST varDeclFragAST = AST.newAST(AST.JLS8); VariableDeclarationStatement decl = varDeclFragAST.newVariableDeclarationStatement( (VariableDeclarationFragment) ASTNode.copySubtree(varDeclFragAST, fragment)); decl.setType((Type) ASTNode.copySubtree(decl.getAST(), varType)); result.add(decl); // initializer is defined here as a separate statement Assignment assign = varDeclFragAST.newAssignment(); assign.setLeftHandSide((Expression) ASTNode.copySubtree(varDeclFragAST, fragment.getName())); assign.setRightHandSide((Expression) ASTNode.copySubtree(varDeclFragAST, exp)); ExpressionStatement assignStmt = varDeclFragAST.newExpressionStatement(assign); result.add(assignStmt); return result; }
public static void addFinalAndValAnnotationToVariableDeclarationStatement( Object converter, VariableDeclarationStatement out, LocalDeclaration in) { @SuppressWarnings("unchecked") List<IExtendedModifier> modifiers = out.modifiers(); addFinalAndValAnnotationToModifierList(converter, modifiers, out.getAST(), in); }