Search in sources :

Example 1 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project che by eclipse.

the class Bindings method getBindingOfParentTypeContext.

/**
     * Returns the type binding of the node's type context or null if the node is inside
     * an annotation, type parameter, super type declaration, or Javadoc of a top level type.
     * The result of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in the type's body.
     *
     * @param node an AST node
     * @return the type binding of the node's parent type context, or <code>null</code>
     */
public static ITypeBinding getBindingOfParentTypeContext(ASTNode node) {
    StructuralPropertyDescriptor lastLocation = null;
    while (node != null) {
        if (node instanceof AbstractTypeDeclaration) {
            AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node;
            if (lastLocation == decl.getBodyDeclarationsProperty() || lastLocation == decl.getJavadocProperty()) {
                return decl.resolveBinding();
            } else if (decl instanceof EnumDeclaration && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) {
                return decl.resolveBinding();
            }
        } else if (node instanceof AnonymousClassDeclaration) {
            return ((AnonymousClassDeclaration) node).resolveBinding();
        }
        lastLocation = node.getLocationInParent();
        node = node.getParent();
    }
    return null;
}
Also used : AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration)

Example 2 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project che by eclipse.

the class NewMethodCorrectionProposal method evaluateModifiers.

private int evaluateModifiers(ASTNode targetTypeDecl) {
    if (getSenderBinding().isAnnotation()) {
        return 0;
    }
    if (getSenderBinding().isInterface()) {
        // for interface and annotation members copy the modifiers from an existing field
        MethodDeclaration[] methodDecls = ((TypeDeclaration) targetTypeDecl).getMethods();
        if (methodDecls.length > 0) {
            return methodDecls[0].getModifiers();
        }
        return 0;
    }
    ASTNode invocationNode = getInvocationNode();
    if (invocationNode instanceof MethodInvocation) {
        int modifiers = 0;
        Expression expression = ((MethodInvocation) invocationNode).getExpression();
        if (expression != null) {
            if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
                modifiers |= Modifier.STATIC;
            }
        } else if (ASTResolving.isInStaticContext(invocationNode)) {
            modifiers |= Modifier.STATIC;
        }
        ASTNode node = ASTResolving.findParentType(invocationNode);
        if (targetTypeDecl.equals(node)) {
            modifiers |= Modifier.PRIVATE;
        } else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
            modifiers |= Modifier.PROTECTED;
            if (ASTResolving.isInStaticContext(node) && expression == null) {
                modifiers |= Modifier.STATIC;
            }
        } else {
            modifiers |= Modifier.PUBLIC;
        }
        return modifiers;
    }
    return Modifier.PUBLIC;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 3 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project che by eclipse.

the class ExtractMethodRefactoring method getRefactoringDescriptor.

private ExtractMethodDescriptor getRefactoringDescriptor() {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fCUnit.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    ITypeBinding type = null;
    if (fDestination instanceof AbstractTypeDeclaration) {
        final AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fDestination;
        type = decl.resolveBinding();
    } else if (fDestination instanceof AnonymousClassDeclaration) {
        final AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fDestination;
        type = decl.resolveBinding();
    }
    IMethodBinding method = null;
    final BodyDeclaration enclosing = fAnalyzer.getEnclosingBodyDeclaration();
    if (enclosing instanceof MethodDeclaration) {
        final MethodDeclaration node = (MethodDeclaration) enclosing;
        method = node.resolveBinding();
    }
    final int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    final String description = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethodName));
    final String label = method != null ? BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED) : '{' + JavaElementLabels.ELLIPSIS_STRING + '}';
    final String header = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(getSignature()), label, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_name_pattern, BasicElementLabels.getJavaElementName(fMethodName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_destination_pattern, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    String visibility = JdtFlags.getVisibilityString(fVisibility);
    if (//$NON-NLS-1$
    "".equals(visibility))
        visibility = RefactoringCoreMessages.ExtractMethodRefactoring_default_visibility;
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_visibility_pattern, visibility));
    if (fThrowRuntimeExceptions)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_declare_thrown_exceptions);
    if (fReplaceDuplicates)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_replace_occurrences);
    if (fGenerateJavadoc)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_generate_comment);
    final ExtractMethodDescriptor descriptor = RefactoringSignatureDescriptorFactory.createExtractMethodDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCUnit));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fMethodName);
    //$NON-NLS-1$
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
    arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
    arguments.put(ATTRIBUTE_DESTINATION, new Integer(fDestinationIndex).toString());
    arguments.put(ATTRIBUTE_EXCEPTIONS, Boolean.valueOf(fThrowRuntimeExceptions).toString());
    arguments.put(ATTRIBUTE_COMMENTS, Boolean.valueOf(fGenerateJavadoc).toString());
    arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceDuplicates).toString());
    return descriptor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) HashMap(java.util.HashMap) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) ExtractMethodDescriptor(org.eclipse.jdt.core.refactoring.descriptors.ExtractMethodDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 4 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project che by eclipse.

the class DelegateCreator method getTypeBodyDeclarationsProperty.

private ChildListPropertyDescriptor getTypeBodyDeclarationsProperty() {
    ASTNode parent = fDeclaration.getParent();
    if (parent instanceof AbstractTypeDeclaration)
        return ((AbstractTypeDeclaration) parent).getBodyDeclarationsProperty();
    else if (parent instanceof AnonymousClassDeclaration)
        return AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY;
    Assert.isTrue(false);
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 5 with AnonymousClassDeclaration

use of org.eclipse.jdt.core.dom.AnonymousClassDeclaration in project che by eclipse.

the class AddUnimplementedMethodsOperation method rewriteAST.

/**
	 * {@inheritDoc}
	 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
    IMethodBinding[] unimplementedMethods = getUnimplementedMethods(fTypeNode);
    if (unimplementedMethods.length == 0)
        return;
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), cuRewrite.getImportRewrite());
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ICompilationUnit unit = cuRewrite.getCu();
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(unit.getJavaProject());
    ListRewrite listRewrite;
    if (fTypeNode instanceof AnonymousClassDeclaration) {
        AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fTypeNode;
        listRewrite = rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
        settings.createComments = false;
    } else if (fTypeNode instanceof AbstractTypeDeclaration) {
        AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fTypeNode;
        listRewrite = rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty());
    } else if (fTypeNode instanceof EnumConstantDeclaration) {
        EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) fTypeNode;
        AnonymousClassDeclaration anonymousClassDeclaration = enumConstantDeclaration.getAnonymousClassDeclaration();
        if (anonymousClassDeclaration == null) {
            anonymousClassDeclaration = rewrite.getAST().newAnonymousClassDeclaration();
            rewrite.set(enumConstantDeclaration, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, anonymousClassDeclaration, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
        }
        listRewrite = rewrite.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
        settings.createComments = false;
    } else {
        //$NON-NLS-1$
        Assert.isTrue(false, "Unknown type node");
        return;
    }
    ImportRewrite imports = cuRewrite.getImportRewrite();
    for (int i = 0; i < unimplementedMethods.length; i++) {
        IMethodBinding curr = unimplementedMethods[i];
        MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(unit, rewrite, imports, context, curr, curr.getDeclaringClass().getName(), settings, false);
        listRewrite.insertLast(newMethodDecl, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)22 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)17 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)8 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)7 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 NotImplementedException (org.autorefactor.util.NotImplementedException)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 EnumDeclaration (org.eclipse.jdt.core.dom.EnumDeclaration)3 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)3 StructuralPropertyDescriptor (org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)3 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)3 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)3 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)3 CodeGenerationSettings (org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)3 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)3