Search in sources :

Example 56 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration 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 57 with TypeDeclaration

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

the class ConvertAnonymousToNestedRefactoring method initAST.

private void initAST(IProgressMonitor pm) {
    if (fCompilationUnitNode == null) {
        fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, pm);
    }
    if (fAnonymousInnerClassNode == null) {
        fAnonymousInnerClassNode = getAnonymousInnerClass(NodeFinder.perform(fCompilationUnitNode, fSelectionStart, fSelectionLength));
    }
    if (fAnonymousInnerClassNode != null) {
        final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class);
        if (declaration instanceof TypeDeclaration) {
            final AbstractTypeDeclaration[] nested = ((TypeDeclaration) declaration).getTypes();
            fClassNamesUsed = new HashSet<String>(nested.length);
            for (int index = 0; index < nested.length; index++) fClassNamesUsed.add(nested[index].getName().getIdentifier());
        } else
            fClassNamesUsed = Collections.emptySet();
    }
}
Also used : AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 58 with TypeDeclaration

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

the class ImplementInterfaceProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    CompilationUnit newRoot = fAstRoot;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    ImportRewrite imports = createImportRewrite(newRoot);
    if (declNode instanceof TypeDeclaration) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, imports);
        Type newInterface = imports.addImport(fNewInterface, ast, importRewriteContext);
        ListRewrite listRewrite = rewrite.getListRewrite(declNode, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
        listRewrite.insertLast(newInterface, null);
        // set up linked mode
        //$NON-NLS-1$
        final String KEY_TYPE = "type";
        addLinkedPosition(rewrite.track(newInterface), true, KEY_TYPE);
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 59 with TypeDeclaration

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

the class NewVariableCorrectionProposal method evaluateFieldModifiers.

private int evaluateFieldModifiers(ASTNode newTypeDecl) {
    if (fSenderBinding.isAnnotation()) {
        return 0;
    }
    if (fSenderBinding.isInterface()) {
        // for interface members copy the modifiers from an existing field
        FieldDeclaration[] fieldDecls = ((TypeDeclaration) newTypeDecl).getFields();
        if (fieldDecls.length > 0) {
            return fieldDecls[0].getModifiers();
        }
        return 0;
    }
    int modifiers = 0;
    if (fVariableKind == CONST_FIELD) {
        modifiers |= Modifier.FINAL | Modifier.STATIC;
    } else {
        ASTNode parent = fOriginalNode.getParent();
        if (parent instanceof QualifiedName) {
            IBinding qualifierBinding = ((QualifiedName) parent).getQualifier().resolveBinding();
            if (qualifierBinding instanceof ITypeBinding) {
                modifiers |= Modifier.STATIC;
            }
        } else if (ASTResolving.isInStaticContext(fOriginalNode)) {
            modifiers |= Modifier.STATIC;
        }
    }
    ASTNode node = ASTResolving.findParentType(fOriginalNode, true);
    if (newTypeDecl.equals(node)) {
        modifiers |= Modifier.PRIVATE;
    } else if (node instanceof AnonymousClassDeclaration) {
        modifiers |= Modifier.PROTECTED;
    } else {
        modifiers |= Modifier.PUBLIC;
    }
    return modifiers;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 60 with TypeDeclaration

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

the class ASTNodeFactory method newTypeParameter.

public static TypeParameter newTypeParameter(AST ast, String content) {
    StringBuffer buffer = new StringBuffer(TYPEPARAM_HEADER);
    buffer.append(content);
    buffer.append(TYPEPARAM_FOOTER);
    CheASTParser p = CheASTParser.newParser(ast.apiLevel());
    p.setSource(buffer.toString().toCharArray());
    CompilationUnit root = (CompilationUnit) p.createAST(null);
    List<AbstractTypeDeclaration> list = root.types();
    TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
    MethodDeclaration methodDecl = typeDecl.getMethods()[0];
    TypeParameter tp = (TypeParameter) methodDecl.typeParameters().get(0);
    ASTNode result = ASTNode.copySubtree(ast, tp);
    result.accept(new PositionClearer());
    return (TypeParameter) result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)111 ASTNode (org.eclipse.jdt.core.dom.ASTNode)64 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)51 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)40 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)38 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)24 Type (org.eclipse.jdt.core.dom.Type)24 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)23 AST (org.eclipse.jdt.core.dom.AST)22 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)21 ArrayList (java.util.ArrayList)20 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)17 AnnotationTypeDeclaration (org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)15 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)13 Block (org.eclipse.jdt.core.dom.Block)13 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 List (java.util.List)12