Search in sources :

Example 51 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project flux 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 52 with AbstractTypeDeclaration

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

the class PromoteTempToFieldRefactoring method addInitializersToConstructors.

private void addInitializersToConstructors(ASTRewrite rewrite) throws CoreException {
    Assert.isTrue(!isDeclaredInAnonymousClass());
    final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) getMethodDeclaration().getParent();
    final MethodDeclaration[] constructors = getAllConstructors(declaration);
    if (constructors.length == 0) {
        AST ast = rewrite.getAST();
        MethodDeclaration newConstructor = ast.newMethodDeclaration();
        newConstructor.setConstructor(true);
        newConstructor.modifiers().addAll(ast.newModifiers(declaration.getModifiers() & ModifierRewrite.VISIBILITY_MODIFIERS));
        newConstructor.setName(ast.newSimpleName(declaration.getName().getIdentifier()));
        newConstructor.setJavadoc(getNewConstructorComment(rewrite));
        newConstructor.setBody(ast.newBlock());
        addFieldInitializationToConstructor(rewrite, newConstructor);
        int insertionIndex = computeInsertIndexForNewConstructor(declaration);
        rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newConstructor, insertionIndex, null);
    } else {
        for (int index = 0; index < constructors.length; index++) {
            if (shouldInsertTempInitialization(constructors[index]))
                addFieldInitializationToConstructor(rewrite, constructors[index]);
        }
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 53 with AbstractTypeDeclaration

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

the class IntroduceIndirectionRefactoring method typeToBodyDeclarationProperty.

private ChildListPropertyDescriptor typeToBodyDeclarationProperty(IType type, CompilationUnit root) throws JavaModelException {
    ASTNode typeDeclaration = typeToDeclaration(type, root);
    if (typeDeclaration instanceof AbstractTypeDeclaration)
        return ((AbstractTypeDeclaration) typeDeclaration).getBodyDeclarationsProperty();
    else if (typeDeclaration 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 54 with AbstractTypeDeclaration

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

the class PromoteTempToFieldRefactoring method getAllConstructors.

private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) {
    if (typeDeclaration instanceof TypeDeclaration) {
        MethodDeclaration[] allMethods = ((TypeDeclaration) typeDeclaration).getMethods();
        List<MethodDeclaration> result = new ArrayList<MethodDeclaration>(Math.min(allMethods.length, 1));
        for (int i = 0; i < allMethods.length; i++) {
            MethodDeclaration declaration = allMethods[i];
            if (declaration.isConstructor())
                result.add(declaration);
        }
        return result.toArray(new MethodDeclaration[result.size()]);
    }
    return new MethodDeclaration[] {};
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 55 with AbstractTypeDeclaration

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

the class PromoteTempToFieldRefactoring method checkClashesInConstructors.

private RefactoringStatus checkClashesInConstructors() {
    Assert.isTrue(fInitializeIn == INITIALIZE_IN_CONSTRUCTOR);
    Assert.isTrue(!isDeclaredInAnonymousClass());
    final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) getMethodDeclaration().getParent();
    if (declaration instanceof TypeDeclaration) {
        MethodDeclaration[] methods = ((TypeDeclaration) declaration).getMethods();
        for (int i = 0; i < methods.length; i++) {
            MethodDeclaration method = methods[i];
            if (!method.isConstructor())
                continue;
            NameCollector nameCollector = new NameCollector(method) {

                @Override
                protected boolean visitNode(ASTNode node) {
                    return true;
                }
            };
            method.accept(nameCollector);
            List<String> names = nameCollector.getNames();
            if (names.contains(fFieldName)) {
                String[] keys = { BasicElementLabels.getJavaElementName(fFieldName), BindingLabelProvider.getBindingLabel(method.resolveBinding(), JavaElementLabels.ALL_FULLY_QUALIFIED) };
                String msg = Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict, keys);
                return RefactoringStatus.createFatalErrorStatus(msg);
            }
        }
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)62 ASTNode (org.eclipse.jdt.core.dom.ASTNode)37 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)25 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)18 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)17 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)17 AST (org.eclipse.jdt.core.dom.AST)15 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)11 Type (org.eclipse.jdt.core.dom.Type)10 ArrayList (java.util.ArrayList)9 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)9 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)9 IType (org.eclipse.jdt.core.IType)8 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)8 Expression (org.eclipse.jdt.core.dom.Expression)7 Javadoc (org.eclipse.jdt.core.dom.Javadoc)7