Example usage for org.eclipse.jdt.core.dom.rewrite ASTRewrite getAST

List of usage examples for org.eclipse.jdt.core.dom.rewrite ASTRewrite getAST

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom.rewrite ASTRewrite getAST.

Prototype

public final AST getAST() 

Source Link

Document

Returns the AST the rewrite was set up on.

Usage

From source file:com.google.gwt.eclipse.core.clientbundle.ClientBundleResource.java

License:Open Source License

public MethodDeclaration createMethodDeclaration(IType clientBundle, ASTRewrite astRewrite,
        ImportRewrite importRewrite, boolean addComments) throws CoreException {
    AST ast = astRewrite.getAST();
    MethodDeclaration methodDecl = ast.newMethodDeclaration();

    // Method is named after the resource it accesses
    methodDecl.setName(ast.newSimpleName(getMethodName()));

    // Method return type is a ResourcePrototype subtype
    ITypeBinding resourceTypeBinding = JavaASTUtils.resolveType(clientBundle.getJavaProject(),
            getReturnTypeName());//from   www  .jav a 2  s.  co m
    Type resourceType = importRewrite.addImport(resourceTypeBinding, ast);
    methodDecl.setReturnType2(resourceType);

    // Add @Source annotation if necessary
    String sourceAnnotationValue = getSourceAnnotationValue(clientBundle);
    if (sourceAnnotationValue != null) {
        // Build the annotation
        SingleMemberAnnotation sourceAnnotation = ast.newSingleMemberAnnotation();
        sourceAnnotation.setTypeName(ast.newName("Source"));
        StringLiteral annotationValue = ast.newStringLiteral();
        annotationValue.setLiteralValue(sourceAnnotationValue);
        sourceAnnotation.setValue(annotationValue);

        // Add the annotation to the method
        ChildListPropertyDescriptor modifiers = methodDecl.getModifiersProperty();
        ListRewrite modifiersRewriter = astRewrite.getListRewrite(methodDecl, modifiers);
        modifiersRewriter.insertFirst(sourceAnnotation, null);
    }

    return methodDecl;
}

From source file:com.google.gwt.eclipse.core.markers.quickfixes.AbstractCreateMethodProposal.java

License:Open Source License

@Override
protected ASTRewrite getRewrite() {
    CompilationUnit targetAstRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
    createImportRewrite(targetAstRoot);//  w ww .  j av a  2  s . co  m

    // Find the target type declaration
    TypeDeclaration typeDecl = JavaASTUtils.findTypeDeclaration(targetAstRoot, targetQualifiedTypeName);
    if (typeDecl == null) {
        return null;
    }

    ASTRewrite rewrite = ASTRewrite.create(targetAstRoot.getAST());

    // Generate the new method declaration
    MethodDeclaration methodDecl = createMethodDeclaration(rewrite.getAST());

    // Add the new method declaration to the interface
    ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(typeDecl);
    ListRewrite listRewriter = rewrite.getListRewrite(typeDecl, property);
    listRewriter.insertLast(methodDecl, null);

    return rewrite;
}

From source file:com.google.gwt.eclipse.core.markers.quickfixes.AbstractUpdateSignatureProposal.java

License:Open Source License

@Override
protected ASTRewrite getRewrite() {
    MethodDeclaration dstMethod = findBestUpdateMatch(rpcPair);

    CompilationUnit astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
    createImportRewrite(astRoot);//from  w w  w  . ja v  a2 s . co m

    ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());

    MethodDeclaration rewriterDstMethod = JavaASTUtils.findMethodDeclaration(astRoot,
            dstMethod.resolveBinding().getKey());
    if (rewriterDstMethod == null) {
        return null;
    }

    MethodDeclaration newSignature = computeMethodSignature(rewrite.getAST(), rpcPair, rewriterDstMethod);

    rewrite.replace(rewriterDstMethod, newSignature, null);

    return rewrite;
}

From source file:edu.illinois.compositerefactorings.refactorings.copymembertosubtype.CopyMemberToSubtypeRefactoringProcessor.java

License:Open Source License

/**
 * {@link org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor#createNewFieldDeclarationNode(MemberActionInfo, CompilationUnit, TypeVariableMaplet[], ASTRewrite, VariableDeclarationFragment)}
 *//*from w  w w  .  j a v a 2  s .c  o  m*/
private FieldDeclaration createNewFieldDeclarationNode(MemberActionInfo info, CompilationUnit declaringCuNode,
        TypeVariableMaplet[] mapping, ASTRewrite rewrite, VariableDeclarationFragment oldFieldFragment)
        throws JavaModelException {
    Assert.isTrue(info.isFieldInfo());
    IField field = (IField) info.getMember();
    AST ast = rewrite.getAST();
    VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment();
    newFragment.setExtraDimensions(oldFieldFragment.getExtraDimensions());
    Expression initializer = oldFieldFragment.getInitializer();
    if (initializer != null) {
        Expression newInitializer = null;
        if (mapping.length > 0)
            newInitializer = createPlaceholderForExpression(initializer, field.getCompilationUnit(), mapping,
                    rewrite);
        else
            newInitializer = createPlaceholderForExpression(initializer, field.getCompilationUnit(), rewrite);
        newFragment.setInitializer(newInitializer);
    }
    newFragment.setName(ast.newSimpleName(oldFieldFragment.getName().getIdentifier()));
    FieldDeclaration newField = ast.newFieldDeclaration(newFragment);
    FieldDeclaration oldField = ASTNodeSearchUtil.getFieldDeclarationNode(field, declaringCuNode);
    if (info.copyJavadocToCopiesInSubclasses())
        copyJavadocNode(rewrite, oldField, newField);
    copyAnnotations(oldField, newField);
    newField.modifiers().addAll(
            ASTNodeFactory.newModifiers(ast, info.getNewModifiersForCopyInSubclass(oldField.getModifiers())));
    Type oldType = oldField.getType();
    ICompilationUnit cu = field.getCompilationUnit();
    Type newType = null;
    if (mapping.length > 0) {
        newType = createPlaceholderForType(oldType, cu, mapping, rewrite);
    } else
        newType = createPlaceholderForType(oldType, cu, rewrite);
    newField.setType(newType);
    return newField;
}

From source file:edu.illinois.compositerefactorings.refactorings.copymembertosubtype.CopyMemberToSubtypeRefactoringProcessor.java

License:Open Source License

/**
 * {@link org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor#createNewMethodDeclarationNode(MemberActionInfo, TypeVariableMaplet[], CompilationUnitRewrite, MethodDeclaration)}
 *//*w  w  w .  ja v  a 2 s .c  om*/
private MethodDeclaration createNewMethodDeclarationNode(MemberActionInfo info, TypeVariableMaplet[] mapping,
        CompilationUnitRewrite rewriter, MethodDeclaration oldMethod) throws JavaModelException {
    Assert.isTrue(!info.isFieldInfo());
    IMethod method = (IMethod) info.getMember();
    ASTRewrite rewrite = rewriter.getASTRewrite();
    AST ast = rewrite.getAST();
    MethodDeclaration newMethod = ast.newMethodDeclaration();
    copyBodyOfPushedDownMethod(rewrite, method, oldMethod, newMethod, mapping);
    newMethod.setConstructor(oldMethod.isConstructor());
    newMethod.setExtraDimensions(oldMethod.getExtraDimensions());
    if (info.copyJavadocToCopiesInSubclasses())
        copyJavadocNode(rewrite, oldMethod, newMethod);
    final IJavaProject project = rewriter.getCu().getJavaProject();
    if (info.isNewMethodToBeDeclaredAbstract() && JavaModelUtil.is50OrHigher(project)
            && JavaPreferencesSettings.getCodeGenerationSettings(project).overrideAnnotation) {
        final MarkerAnnotation annotation = ast.newMarkerAnnotation();
        annotation.setTypeName(ast.newSimpleName("Override")); //$NON-NLS-1$
        newMethod.modifiers().add(annotation);
    }
    copyAnnotations(oldMethod, newMethod);
    newMethod.modifiers().addAll(
            ASTNodeFactory.newModifiers(ast, info.getNewModifiersForCopyInSubclass(oldMethod.getModifiers())));
    newMethod.setName(ast.newSimpleName(oldMethod.getName().getIdentifier()));
    copyReturnType(rewrite, method.getCompilationUnit(), oldMethod, newMethod, mapping);
    copyParameters(rewrite, method.getCompilationUnit(), oldMethod, newMethod, mapping);
    copyThrownExceptions(oldMethod, newMethod);
    copyTypeParameters(oldMethod, newMethod);
    return newMethod;
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateAndOddnessCheckResolution.java

License:Open Source License

/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>(x & 1) == 1</CODE>.
 *//*from w  w w .jav a 2 s . co  m*/
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(numberExpression);

    final AST ast = rewrite.getAST();
    InfixExpression andOddnessCheck = ast.newInfixExpression();
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    InfixExpression andExpression = ast.newInfixExpression();

    andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression));
    andExpression.setOperator(AND);
    andExpression.setRightOperand(ast.newNumberLiteral("1"));
    parenthesizedExpression.setExpression(andExpression);
    andOddnessCheck.setLeftOperand(parenthesizedExpression);
    andOddnessCheck.setOperator(EQUALS);
    andOddnessCheck.setRightOperand(ast.newNumberLiteral("1"));

    return andOddnessCheck;

}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateDoPrivilegedBlockResolution.java

License:Open Source License

protected void updateMethodParams(ASTRewrite rewrite, Set<String> variables, List<?> params) {
    Assert.isNotNull(rewrite);/*from ww w .j a v  a  2s  .  c  o m*/
    Assert.isNotNull(variables);
    Assert.isNotNull(params);

    for (Object paramObj : params) {
        SingleVariableDeclaration param = (SingleVariableDeclaration) paramObj;
        if (variables.contains(param.getName().getFullyQualifiedName())) {
            ListRewrite listRewrite = rewrite.getListRewrite(param,
                    SingleVariableDeclaration.MODIFIERS2_PROPERTY);
            listRewrite.insertLast(rewrite.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD), null);
        }
    }
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateDoPrivilegedBlockResolution.java

License:Open Source License

protected void updateLocalVariableDeclarations(final ASTRewrite rewrite, final Set<String> variables,
        Block block) {//  w  ww  .  j  av  a  2 s.c om
    Assert.isNotNull(rewrite);
    Assert.isNotNull(block);
    Assert.isNotNull(variables);

    final AST ast = rewrite.getAST();
    block.accept(new ASTVisitor() {

        @Override
        public boolean visit(VariableDeclarationFragment fragment) {
            if (variables.contains(fragment.getName().getFullyQualifiedName())) {
                ASTNode parent = fragment.getParent();
                if (parent instanceof VariableDeclarationStatement) {
                    ListRewrite listRewrite = rewrite.getListRewrite(parent,
                            VariableDeclarationStatement.MODIFIERS2_PROPERTY);
                    listRewrite.insertLast(ast.newModifier(ModifierKeyword.FINAL_KEYWORD), null);
                }
            }
            return true;
        }

    });

}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateDoPrivilegedBlockResolution.java

License:Open Source License

protected void updateImportDeclarations(ASTRewrite rewrite, CompilationUnit compilationUnit) {
    Assert.isNotNull(rewrite);/*from  ww  w  . j a v  a2 s. c  om*/
    Assert.isNotNull(compilationUnit);

    if (isUpdateImports()) {
        final AST ast = rewrite.getAST();
        SortedSet<ImportDeclaration> imports = new TreeSet<ImportDeclaration>(importComparator);
        imports.add(createImportDeclaration(ast, PrivilegedAction.class));
        if (!isStaticImport()) {
            imports.add(createImportDeclaration(ast, AccessController.class));
        } else {
            imports.add(createImportDeclaration(ast, AccessController.class, DO_PRIVILEGED_METHOD_NAME));
        }
        addImports(rewrite, compilationUnit, imports);
    }
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateDoPrivilegedBlockResolution.java

License:Open Source License

protected MethodInvocation createDoPrivilegedInvocation(ASTRewrite rewrite,
        ClassInstanceCreation classLoaderCreation) {
    AST ast = rewrite.getAST();

    MethodInvocation doPrivilegedInvocation = ast.newMethodInvocation();
    ClassInstanceCreation privilegedActionCreation = createPrivilegedActionCreation(rewrite,
            classLoaderCreation);/*from   w w  w . j  a  v  a  2s.  c om*/
    List<Expression> arguments = checkedList(doPrivilegedInvocation.arguments());

    if (!isStaticImport()) {
        Name accessControllerName;
        if (isUpdateImports()) {
            accessControllerName = ast.newSimpleName(AccessController.class.getSimpleName());
        } else {
            accessControllerName = ast.newName(AccessController.class.getName());
        }
        doPrivilegedInvocation.setExpression(accessControllerName);
    }
    doPrivilegedInvocation.setName(ast.newSimpleName(DO_PRIVILEGED_METHOD_NAME));
    arguments.add(privilegedActionCreation);

    return doPrivilegedInvocation;
}