// Função auxiliar para copiar o tipo da variável // considerando que um tipo nao pode pertencer a outra AST private Type getType(ITypeBinding typeBinding, AST newAST) { if (typeBinding.isPrimitive()) { return newAST.newPrimitiveType(PrimitiveType.toCode(typeBinding.getName())); } else if (typeBinding.isArray()) { return newAST.newArrayType( this.getType(typeBinding.getElementType(), newAST), typeBinding.getDimensions()); } else if (typeBinding.isParameterizedType()) { ParameterizedType pt = newAST.newParameterizedType(this.getType(typeBinding.getTypeDeclaration(), newAST)); for (ITypeBinding itb : typeBinding.getTypeArguments()) { pt.typeArguments().add(this.getType(itb, newAST)); } return pt; } else if (typeBinding.isWildcardType()) { WildcardType wt = newAST.newWildcardType(); wt.setBound( typeBinding.getBound() == null ? null : this.getType(typeBinding.getBound(), newAST), typeBinding.isUpperbound()); return wt; } else { try { return newAST.newSimpleType(newAST.newName(typeBinding.getQualifiedName())); } catch (Exception e) { return newAST.newSimpleType(newAST.newName(typeBinding.getName())); } } }
public static void getInvalidQualificationProposals( IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { ASTNode node = problem.getCoveringNode(context.getASTRoot()); if (!(node instanceof Name)) { return; } Name name = (Name) node; IBinding binding = name.resolveBinding(); if (!(binding instanceof ITypeBinding)) { return; } ITypeBinding typeBinding = (ITypeBinding) binding; AST ast = node.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); rewrite.replace(name, ast.newName(typeBinding.getQualifiedName()), null); String label = CorrectionMessages.JavadocTagsSubProcessor_qualifylinktoinner_description; Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal( label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_INNER_TYPE_NAME, image); proposals.add(proposal); }
/** * Builds a new {@link Name} instance. If only a single name is provided then a {@link SimpleName} * is returned, if several names are provided then a {@link QualifiedName} is built. * * @param names the qualified or simple name * @return a new name * @throws IllegalArgumentException if no names are provided */ public Name name(String... names) { if (names.length == 0) { throw new IllegalArgumentException(null, "Expected at least one name, but was given 0 names"); } if (names.length == 1) { return simpleName(names[0]); } return ast.newName(names); }
private void updatePackageStatement( CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException { boolean defaultPackage = pkgName.length == 0; AST ast = astCU.getAST(); if (defaultPackage) { // remove existing package statement PackageDeclaration pkg = astCU.getPackage(); if (pkg != null) { int pkgStart; Javadoc javadoc = pkg.getJavadoc(); if (javadoc != null) { pkgStart = javadoc.getStartPosition() + javadoc.getLength() + 1; } else { pkgStart = pkg.getStartPosition(); } int extendedStart = astCU.getExtendedStartPosition(pkg); if (pkgStart != extendedStart) { // keep the comments associated with package declaration // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 String commentSource = cu.getSource().substring(extendedStart, pkgStart); ASTNode comment = rewriter.createStringPlaceholder(commentSource, ASTNode.PACKAGE_DECLARATION); rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, comment, null); } else { rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null); } } } else { org.eclipse.jdt.core.dom.PackageDeclaration pkg = astCU.getPackage(); if (pkg != null) { // rename package statement Name name = ast.newName(pkgName); rewriter.set(pkg, PackageDeclaration.NAME_PROPERTY, name, null); } else { // create new package statement pkg = ast.newPackageDeclaration(); pkg.setName(ast.newName(pkgName)); rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, pkg, null); } } }
/** * create the update code block with dao invoked. * * @param ast the ast tree. fc the configuration. */ @SuppressWarnings("unchecked") public Block createBlock(AST ast, FastIbatisConfig fc) { Block block = ast.newBlock(); MethodInvocation methodInvocation = ast.newMethodInvocation(); methodInvocation.setExpression(ast.newName(Utils.lowFirstChar(fc.getModelName()) + "DAO")); methodInvocation.setName(ast.newSimpleName(fc.getMethodName())); methodInvocation.arguments().add(ast.newSimpleName("param")); ReturnStatement rs = ast.newReturnStatement(); rs.setExpression(methodInvocation); block.statements().add(rs); return block; }
public SingleVariableDeclaration newVariableDecl(String name, String type) { SingleVariableDeclaration exceptionDecl = ast.newSingleVariableDeclaration(); exceptionDecl.setName(ast.newSimpleName(name)); exceptionDecl.setType(ast.newSimpleType(ast.newName(type))); return exceptionDecl; }
public Type newType(String typeName) { return ast.newSimpleType(ast.newName(typeName)); }
public ASTNode newStaticCall(String className, String funName, Expression... args) { return newCall(ast.newName(className), funName, args); }
private SimpleType newSimpleType(final String typeName) { return ast.newSimpleType(ast.newName(typeName)); }
@SuppressWarnings("unchecked") @Before public void setUp() { manager = ModelManager.getInstance(); manager.clearModel(); AST ast2 = AST.newAST(AST.JLS3); CompilationUnit cu2 = ast2.newCompilationUnit(); PackageDeclaration pack2 = ast2.newPackageDeclaration(); pack2.setName(ast2.newName("be.ac.ua.test.otherpack")); cu2.setPackage(pack2); TypeDeclaration type2 = ast2.newTypeDeclaration(); type2.setName(ast2.newSimpleName("Foo")); cu2.types().add(type2); // created mock compilationunit containing package and class PackageRecorder prec2 = new PackageRecorder(pack2); prec2.storeChange(new Add()); // store the package addition ClassRecorder crec2 = new ClassRecorder(type2); declaredclassadd = new Add(); crec2.storeChange(declaredclassadd); AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); PackageDeclaration pack = ast.newPackageDeclaration(); pack.setName(ast.newName(packname)); cu.setPackage(pack); TypeDeclaration type = ast.newTypeDeclaration(); type.setName(ast.newSimpleName(classname)); cu.types().add(type); PackageRecorder prec = new PackageRecorder(pack); prec.storeChange(new Add()); // store the package addition ClassRecorder crec = new ClassRecorder(type); classadd = new Add(); crec.storeChange(classadd); // Class and package created and changes logged, now create the Field. VariableDeclarationFragment frag1 = ast.newVariableDeclarationFragment(); frag1.setName(ast.newSimpleName(intfieldname)); FieldDeclaration field = ast.newFieldDeclaration(frag1); field.setType(ast.newPrimitiveType(PrimitiveType.INT)); // field has type int type.bodyDeclarations().add(field); VariableDeclarationFragment frag2 = ast.newVariableDeclarationFragment(); frag2.setName(ast.newSimpleName(fieldname)); FieldDeclaration field2 = ast.newFieldDeclaration(frag2); field2.setType(ast.newSimpleType(ast.newName(declaredTypeName))); // field has type Foo type.bodyDeclarations().add(field2); VariableDeclarationFragment frag3 = ast.newVariableDeclarationFragment(); frag3.setName(ast.newSimpleName(field3Name)); FieldDeclaration field3 = ast.newFieldDeclaration(frag3); field3.setType(ast.newSimpleType(ast.newName("Foo"))); // field has type Foo type.bodyDeclarations().add(field3); ImportDeclaration imp = ast.newImportDeclaration(); imp.setName(ast.newName(declaredTypeName)); cu.imports().add(imp); // created mock compilationunit containing package and class recorder1 = new FieldRecorder(field); recorder2 = new FieldRecorder(field2); recorder3 = new FieldRecorder(field3); }