Search in sources :

Example 96 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project flux 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);
    ASTParser p = ASTParser.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) ASTParser(org.eclipse.jdt.core.dom.ASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 97 with AbstractTypeDeclaration

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

the class ContextSensitiveImportRewriteContext method findInContext.

@Override
public int findInContext(String qualifier, String name, int kind) {
    IBinding[] declarationsInScope = getDeclarationsInScope();
    for (int i = 0; i < declarationsInScope.length; i++) {
        if (declarationsInScope[i] instanceof ITypeBinding) {
            ITypeBinding typeBinding = (ITypeBinding) declarationsInScope[i];
            if (isSameType(typeBinding, qualifier, name)) {
                return RES_NAME_FOUND;
            } else if (isConflicting(typeBinding, name)) {
                return RES_NAME_CONFLICT;
            }
        } else if (declarationsInScope[i] != null) {
            if (isConflicting(declarationsInScope[i], name)) {
                return RES_NAME_CONFLICT;
            }
        }
    }
    Name[] names = getImportedNames();
    for (int i = 0; i < names.length; i++) {
        IBinding binding = names[i].resolveBinding();
        if (binding instanceof ITypeBinding && !binding.isRecovered()) {
            ITypeBinding typeBinding = (ITypeBinding) binding;
            if (isConflictingType(typeBinding, qualifier, name)) {
                return RES_NAME_CONFLICT;
            }
        }
    }
    List<AbstractTypeDeclaration> list = fCompilationUnit.types();
    for (Iterator<AbstractTypeDeclaration> iter = list.iterator(); iter.hasNext(); ) {
        AbstractTypeDeclaration type = iter.next();
        ITypeBinding binding = type.resolveBinding();
        if (binding != null) {
            if (isSameType(binding, qualifier, name)) {
                return RES_NAME_FOUND;
            } else {
                ITypeBinding decl = containingDeclaration(binding, qualifier, name);
                while (decl != null && !decl.equals(binding)) {
                    int modifiers = decl.getModifiers();
                    if (Modifier.isPrivate(modifiers))
                        return RES_NAME_CONFLICT;
                    decl = decl.getDeclaringClass();
                }
            }
        }
    }
    String[] addedImports = fImportRewrite.getAddedImports();
    String qualifiedName = JavaModelUtil.concatenateName(qualifier, name);
    for (int i = 0; i < addedImports.length; i++) {
        String addedImport = addedImports[i];
        if (qualifiedName.equals(addedImport)) {
            return RES_NAME_FOUND;
        } else {
            if (isConflicting(name, addedImport))
                return RES_NAME_CONFLICT;
        }
    }
    if (qualifier.equals("java.lang")) {
        // $NON-NLS-1$
        // No explicit import statement required
        ITypeRoot typeRoot = fCompilationUnit.getTypeRoot();
        if (typeRoot != null) {
            IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent();
            try {
                ICompilationUnit[] compilationUnits = packageFragment.getCompilationUnits();
                for (int i = 0; i < compilationUnits.length; i++) {
                    ICompilationUnit cu = compilationUnits[i];
                    IType[] allTypes = cu.getAllTypes();
                    for (int j = 0; j < allTypes.length; j++) {
                        IType type = allTypes[j];
                        String packageTypeName = type.getFullyQualifiedName();
                        if (isConflicting(name, packageTypeName))
                            return RES_NAME_CONFLICT;
                    }
                }
            } catch (JavaModelException e) {
            }
        }
    }
    return fImportRewrite.getDefaultImportRewriteContext().findInContext(qualifier, name, kind);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 98 with AbstractTypeDeclaration

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

the class VariableDeclarationRewrite method rewriteModifiers.

public static void rewriteModifiers(final FieldDeclaration declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) {
    final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
    AST ast = declarationNode.getAST();
    /*
 * Problem: Same declarationNode can be the subject of multiple calls to this method.
 * For the 2nd++ calls, the original declarationNode has already been rewritten, and this has to be taken into account.
 * 
 * Assumption:
 * - Modifiers for each VariableDeclarationFragment are modified at most once.
 * 
 * Solution:
 * - Maintain a map from original VariableDeclarationFragments to their new FieldDeclaration.
 * - Original modifiers in declarationNode belong to the first fragment.
 * - When a later fragment needs different modifiers, we create a new FieldDeclaration and move all successive fragments into that declaration
 * - When a fragment has been moved to a new declaration, make sure we don't create a new move target again, but instead use the already created one 
 */
    List<VariableDeclarationFragment> fragments = declarationNode.fragments();
    Iterator<VariableDeclarationFragment> iter = fragments.iterator();
    ListRewrite blockRewrite;
    if (declarationNode.getParent() instanceof AbstractTypeDeclaration) {
        blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), ((AbstractTypeDeclaration) declarationNode.getParent()).getBodyDeclarationsProperty());
    } else {
        blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
    }
    VariableDeclarationFragment lastFragment = iter.next();
    ASTNode lastStatement = declarationNode;
    if (fragmentsToChange.contains(lastFragment)) {
        ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
        modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
    }
    ListRewrite fragmentsRewrite = null;
    while (iter.hasNext()) {
        VariableDeclarationFragment currentFragment = iter.next();
        @SuppressWarnings("unchecked") Map<VariableDeclarationFragment, MovedFragment> lookup = (Map<VariableDeclarationFragment, MovedFragment>) rewrite.getProperty(MovedFragment.class.getName());
        if (lookup == null) {
            lookup = new HashMap<VariableDeclarationFragment, MovedFragment>();
            rewrite.setProperty(MovedFragment.class.getName(), lookup);
        }
        MovedFragment currentMovedFragment = lookup.get(currentFragment);
        boolean changeLast = fragmentsToChange.contains(lastFragment);
        boolean changeCurrent = fragmentsToChange.contains(currentFragment);
        if (changeLast != changeCurrent || lookup.containsKey(lastFragment)) {
            ModifierRewrite modifierRewrite = null;
            if (currentMovedFragment != null) {
                if (currentMovedFragment.fUsesOriginalModifiers) {
                    // Need to put in the right modifiers (removing any existing ones).
                    modifierRewrite = ModifierRewrite.create(rewrite, currentMovedFragment.fDeclaration);
                    ListRewrite listRewrite = rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
                    List<IExtendedModifier> extendedList = listRewrite.getRewrittenList();
                    for (int i = 0; i < extendedList.size(); i++) {
                        ASTNode curr = (ASTNode) extendedList.get(i);
                        if (curr instanceof Modifier)
                            rewrite.remove(curr, group);
                    }
                }
            // otherwise, don't need to touch the modifiers, so leave modifierRewrite null
            } else {
                // need to split an existing field declaration
                VariableDeclarationFragment moveTarget;
                moveTarget = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
                FieldDeclaration newStatement = (FieldDeclaration) ast.createInstance(FieldDeclaration.class);
                rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY).insertLast(moveTarget, group);
                lookup.put(currentFragment, new MovedFragment(moveTarget, newStatement, !changeCurrent));
                rewrite.set(newStatement, FieldDeclaration.TYPE_PROPERTY, rewrite.createCopyTarget(declarationNode.getType()), group);
                modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
                modifierRewrite.copyAllAnnotations(declarationNode, group);
                blockRewrite.insertAfter(newStatement, lastStatement, group);
                fragmentsRewrite = rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY);
                lastStatement = newStatement;
            }
            if (modifierRewrite != null) {
                if (changeCurrent) {
                    int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
                    modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
                } else {
                    int newModifiers = declarationNode.getModifiers();
                    modifierRewrite.setModifiers(newModifiers, Modifier.NONE, group);
                }
            }
        } else if (fragmentsRewrite != null) {
            VariableDeclarationFragment fragment0;
            boolean usesOriginalModifiers = true;
            if (currentMovedFragment != null) {
                fragment0 = currentMovedFragment.fMoveTarget;
                usesOriginalModifiers = currentMovedFragment.fUsesOriginalModifiers;
                rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY).remove(fragment0, group);
            } else {
                fragment0 = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
            }
            lookup.put(currentFragment, new MovedFragment(fragment0, lastStatement, usesOriginalModifiers));
            fragmentsRewrite.insertLast(fragment0, group);
        }
        lastFragment = currentFragment;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) HashMap(java.util.HashMap) Map(java.util.Map) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 99 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project sts4 by spring-projects.

the class ImportRewrite method findInImports.

/**
 * Not API, package visibility as accessed from an anonymous type
 */
/* package */
final int findInImports(String qualifier, String name, int kind) {
    boolean allowAmbiguity = (kind == ImportRewriteContext.KIND_STATIC_METHOD) || (name.length() == 1 && name.charAt(0) == '*');
    List imports = this.existingImports;
    char prefix = (kind == ImportRewriteContext.KIND_TYPE) ? NORMAL_PREFIX : STATIC_PREFIX;
    for (int i = imports.size() - 1; i >= 0; i--) {
        String curr = (String) imports.get(i);
        int res = compareImport(prefix, qualifier, name, curr);
        if (res != ImportRewriteContext.RES_NAME_UNKNOWN) {
            if (!allowAmbiguity || res == ImportRewriteContext.RES_NAME_FOUND) {
                if (prefix != STATIC_PREFIX) {
                    return res;
                }
            }
        }
    }
    String packageName = getPackageName();
    if (kind == ImportRewriteContext.KIND_TYPE) {
        if (this.filterImplicitImports && this.useContextToFilterImplicitImports) {
            if (this.astRoot != null) {
                List<AbstractTypeDeclaration> types = this.astRoot.types();
                int nTypes = types.size();
                for (int i = 0; i < nTypes; i++) {
                    AbstractTypeDeclaration type = types.get(i);
                    SimpleName simpleName = type.getName();
                    if (simpleName.getIdentifier().equals(name)) {
                        return qualifier.equals(packageName) ? ImportRewriteContext.RES_NAME_FOUND : ImportRewriteContext.RES_NAME_CONFLICT;
                    }
                }
            } else {
            // [STS4] No ICompilationUnit available as there is no class file or associated IJavaElement available for the source
            // try {
            // IType[] types = this.compilationUnit.getTypes();
            // int nTypes = types.length;
            // for (int i = 0; i < nTypes; i++) {
            // IType type = types[i];
            // String typeName = type.getElementName();
            // if (typeName.equals(name)) {
            // return qualifier.equals(packageName)
            // ? ImportRewriteContext.RES_NAME_FOUND
            // : ImportRewriteContext.RES_NAME_CONFLICT;
            // }
            // }
            // } catch (JavaModelException e) {
            // // don't want to throw an exception here
            // }
            }
        }
    }
    return ImportRewriteContext.RES_NAME_UNKNOWN;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) List(java.util.List) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 100 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project sts4 by spring-projects.

the class AbstractSourceLinks method findTypeRegion.

protected Region findTypeRegion(CompilationUnit cu, String fqName) {
    if (cu == null) {
        return null;
    }
    int[] values = new int[] { 0, -1 };
    int lastDotIndex = fqName.lastIndexOf('.');
    String packageName = fqName.substring(0, lastDotIndex);
    String typeName = fqName.substring(lastDotIndex + 1);
    if (packageName.equals(cu.getPackage().getName().getFullyQualifiedName())) {
        Stack<String> visitedType = new Stack<>();
        cu.accept(new ASTVisitor() {

            private boolean visitDeclaration(AbstractTypeDeclaration node) {
                visitedType.push(node.getName().getIdentifier());
                if (values[1] < 0) {
                    if (String.join("$", visitedType.toArray(new String[visitedType.size()])).equals(typeName)) {
                        values[0] = node.getName().getStartPosition();
                        values[1] = node.getName().getLength();
                    }
                }
                return values[1] < 0;
            }

            @Override
            public boolean visit(TypeDeclaration node) {
                return visitDeclaration(node);
            }

            @Override
            public boolean visit(AnnotationTypeDeclaration node) {
                return visitDeclaration(node);
            }

            @Override
            public void endVisit(AnnotationTypeDeclaration node) {
                visitedType.pop();
                super.endVisit(node);
            }

            @Override
            public void endVisit(TypeDeclaration node) {
                visitedType.pop();
                super.endVisit(node);
            }
        });
    }
    return values[1] < 0 ? null : new Region(values[0], values[1]);
}
Also used : AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) Region(org.springframework.ide.vscode.commons.util.text.Region) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Stack(java.util.Stack) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)233 ASTNode (org.eclipse.jdt.core.dom.ASTNode)116 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)88 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)85 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)81 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)72 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)52 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)49 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)49 AST (org.eclipse.jdt.core.dom.AST)44 ArrayList (java.util.ArrayList)43 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)41 IType (org.eclipse.jdt.core.IType)40 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)38 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)38 Type (org.eclipse.jdt.core.dom.Type)37 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)36 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)34 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)25 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)24