Search in sources :

Example 16 with AnonymousClassDeclaration

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

the class TypeContextChecker method fillWithTypeStubs.

private static void fillWithTypeStubs(final StringBuffer bufBefore, final StringBuffer bufAfter, final int focalPosition, List<? extends BodyDeclaration> types) {
    StringBuffer buf;
    for (Iterator<? extends BodyDeclaration> iter = types.iterator(); iter.hasNext(); ) {
        BodyDeclaration bodyDeclaration = iter.next();
        if (!(bodyDeclaration instanceof AbstractTypeDeclaration)) {
            //account for local classes:
            if (!(bodyDeclaration instanceof MethodDeclaration))
                continue;
            int bodyStart = bodyDeclaration.getStartPosition();
            int bodyEnd = bodyDeclaration.getStartPosition() + bodyDeclaration.getLength();
            if (!(bodyStart < focalPosition && focalPosition < bodyEnd))
                continue;
            MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
            buf = bufBefore;
            appendModifiers(buf, methodDeclaration.modifiers());
            appendTypeParameters(buf, methodDeclaration.typeParameters());
            //$NON-NLS-1$
            buf.append(" void ");
            buf.append(methodDeclaration.getName().getIdentifier());
            //$NON-NLS-1$
            buf.append("(){\n");
            Block body = methodDeclaration.getBody();
            body.accept(new HierarchicalASTVisitor() {

                @Override
                public boolean visit(AbstractTypeDeclaration node) {
                    fillWithTypeStubs(bufBefore, bufAfter, focalPosition, Collections.singletonList(node));
                    return false;
                }

                @Override
                public boolean visit(ClassInstanceCreation node) {
                    AnonymousClassDeclaration anonDecl = node.getAnonymousClassDeclaration();
                    if (anonDecl == null)
                        // could be in CIC parameter list
                        return true;
                    int anonStart = anonDecl.getStartPosition();
                    int anonEnd = anonDecl.getStartPosition() + anonDecl.getLength();
                    if (!(anonStart < focalPosition && focalPosition < anonEnd))
                        return false;
                    //$NON-NLS-1$
                    bufBefore.append(" new ");
                    bufBefore.append(node.getType().toString());
                    //$NON-NLS-1$
                    bufBefore.append("(){\n");
                    fillWithTypeStubs(bufBefore, bufAfter, focalPosition, anonDecl.bodyDeclarations());
                    //$NON-NLS-1$
                    bufAfter.append("};\n");
                    return false;
                }
            });
            buf = bufAfter;
            //$NON-NLS-1$
            buf.append("}\n");
            continue;
        }
        AbstractTypeDeclaration decl = (AbstractTypeDeclaration) bodyDeclaration;
        buf = decl.getStartPosition() < focalPosition ? bufBefore : bufAfter;
        appendModifiers(buf, decl.modifiers());
        if (decl instanceof TypeDeclaration) {
            TypeDeclaration type = (TypeDeclaration) decl;
            //$NON-NLS-1$//$NON-NLS-2$
            buf.append(type.isInterface() ? "interface " : "class ");
            buf.append(type.getName().getIdentifier());
            appendTypeParameters(buf, type.typeParameters());
            if (type.getSuperclassType() != null) {
                //$NON-NLS-1$
                buf.append(" extends ");
                buf.append(ASTNodes.asString(type.getSuperclassType()));
            }
            List<Type> superInterfaces = type.superInterfaceTypes();
            appendSuperInterfaces(buf, superInterfaces);
        } else if (decl instanceof AnnotationTypeDeclaration) {
            AnnotationTypeDeclaration annotation = (AnnotationTypeDeclaration) decl;
            //$NON-NLS-1$
            buf.append("@interface ");
            buf.append(annotation.getName().getIdentifier());
        } else if (decl instanceof EnumDeclaration) {
            EnumDeclaration enumDecl = (EnumDeclaration) decl;
            //$NON-NLS-1$
            buf.append("enum ");
            buf.append(enumDecl.getName().getIdentifier());
            List<Type> superInterfaces = enumDecl.superInterfaceTypes();
            appendSuperInterfaces(buf, superInterfaces);
        }
        //$NON-NLS-1$
        buf.append("{\n");
        if (decl instanceof EnumDeclaration)
            //$NON-NLS-1$
            buf.append(";\n");
        fillWithTypeStubs(bufBefore, bufAfter, focalPosition, decl.bodyDeclarations());
        buf = decl.getStartPosition() + decl.getLength() < focalPosition ? bufBefore : bufAfter;
        //$NON-NLS-1$
        buf.append("}\n");
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) HierarchicalASTVisitor(org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 17 with AnonymousClassDeclaration

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

the class ConvertAnonymousToNestedRefactoring method mustInnerClassBeStatic.

public boolean mustInnerClassBeStatic() {
    ITypeBinding typeBinding = ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
    ASTNode current = fAnonymousInnerClassNode.getParent();
    boolean ans = false;
    while (current != null) {
        switch(current.getNodeType()) {
            case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            case ASTNode.CONSTRUCTOR_INVOCATION:
                return true;
            case ASTNode.ANONYMOUS_CLASS_DECLARATION:
                {
                    AnonymousClassDeclaration enclosingAnonymousClassDeclaration = (AnonymousClassDeclaration) current;
                    ITypeBinding binding = enclosingAnonymousClassDeclaration.resolveBinding();
                    if (binding != null && Bindings.isSuperType(typeBinding, binding.getSuperclass())) {
                        return false;
                    }
                    break;
                }
            case ASTNode.FIELD_DECLARATION:
                {
                    FieldDeclaration enclosingFieldDeclaration = (FieldDeclaration) current;
                    if (Modifier.isStatic(enclosingFieldDeclaration.getModifiers())) {
                        ans = true;
                    }
                    break;
                }
            case ASTNode.METHOD_DECLARATION:
                {
                    MethodDeclaration enclosingMethodDeclaration = (MethodDeclaration) current;
                    if (Modifier.isStatic(enclosingMethodDeclaration.getModifiers())) {
                        ans = true;
                    }
                    break;
                }
            case ASTNode.TYPE_DECLARATION:
                {
                    return ans;
                }
        }
        current = current.getParent();
    }
    return ans;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 18 with AnonymousClassDeclaration

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

the class ConvertAnonymousToNestedRefactoring method getAllEnclosingAnonymousTypesField.

private List<IVariableBinding> getAllEnclosingAnonymousTypesField() {
    final List<IVariableBinding> ans = new ArrayList<IVariableBinding>();
    final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class);
    AnonymousClassDeclaration anonymous = (AnonymousClassDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, ASTNode.ANONYMOUS_CLASS_DECLARATION);
    while (anonymous != null) {
        if (ASTNodes.isParent(anonymous, declaration)) {
            ITypeBinding binding = anonymous.resolveBinding();
            if (binding != null) {
                ans.addAll(Arrays.asList(binding.getDeclaredFields()));
            }
        } else {
            break;
        }
        anonymous = (AnonymousClassDeclaration) ASTNodes.getParent(anonymous, ASTNode.ANONYMOUS_CLASS_DECLARATION);
    }
    return ans;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 19 with AnonymousClassDeclaration

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

the class ASTResolving method findParentType.

/**
	 * Finds the ancestor type of <code>node</code> (includes <code>node</code> in the search).
	 *
	 * @param node the node to start the search from, can be <code>null</code>
	 * @param treatModifiersOutside if set, modifiers are not part of their type, but of the type's
	 *            parent
	 * @return returns the ancestor type of <code>node</code> (AbstractTypeDeclaration or
	 *         AnonymousTypeDeclaration) if any (including <code>node</code>), <code>null</code>
	 *         otherwise
	 */
public static ASTNode findParentType(ASTNode node, boolean treatModifiersOutside) {
    StructuralPropertyDescriptor lastLocation = null;
    while (node != null) {
        if (node instanceof AbstractTypeDeclaration) {
            AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node;
            if (!treatModifiersOutside || lastLocation != decl.getModifiersProperty()) {
                return decl;
            }
        } else if (node instanceof AnonymousClassDeclaration) {
            return node;
        }
        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)

Example 20 with AnonymousClassDeclaration

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

the class QuickAssistProcessor method getConvertAnonymousToNestedProposal.

private static boolean getConvertAnonymousToNestedProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException {
    if (!(node instanceof Name))
        return false;
    ASTNode normalized = ASTNodes.getNormalizedNode(node);
    if (normalized.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY)
        return false;
    final AnonymousClassDeclaration anonymTypeDecl = ((ClassInstanceCreation) normalized.getParent()).getAnonymousClassDeclaration();
    if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
        return false;
    }
    if (proposals == null) {
        return true;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    //final ConvertAnonymousToNestedRefactoring refactoring= new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
    String extTypeName = ASTNodes.getSimpleNameIdentifier((Name) node);
    ITypeBinding anonymTypeBinding = anonymTypeDecl.resolveBinding();
    String className;
    if (anonymTypeBinding.getInterfaces().length == 0) {
        className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
    } else {
        className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
    }
    String[][] existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
    int i = 1;
    while (existingTypes != null) {
        i++;
        existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
    }
    //		}
    return false;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType)

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