Search in sources :

Example 51 with ASTNode

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

the class NewCUUsingWizardProposal method getPossibleSuperTypeBinding.

//	private NewTypeWizardPage getPage(NewElementWizard wizard) {
//		IWizardPage[] pages= wizard.getPages();
//		Assert.isTrue(pages.length > 0 && pages[0] instanceof NewTypeWizardPage);
//		return (NewTypeWizardPage) pages[0];
//	}
//
//	private NewElementWizard createWizard(StructuredSelection selection) {
//		switch (fTypeKind) {
//			case K_CLASS: {
//				NewClassWizardPage page=  new NewClassWizardPage();
//				page.init(selection);
//				configureWizardPage(page);
//				return new NewClassCreationWizard(page, true);
//			}
//			case K_INTERFACE: {
//				NewInterfaceWizardPage page=  new NewInterfaceWizardPage();
//				page.init(selection);
//				configureWizardPage(page);
//				return new NewInterfaceCreationWizard(page, true);
//			}
//			case K_ENUM: {
//				NewEnumWizardPage page=  new NewEnumWizardPage();
//				page.init(selection);
//				configureWizardPage(page);
//				return new NewEnumCreationWizard(page, true);
//			}
//			case K_ANNOTATION: {
//				NewAnnotationWizardPage page=  new NewAnnotationWizardPage();
//				page.init(selection);
//				configureWizardPage(page);
//				return new NewAnnotationCreationWizard(page, true);
//			}
//		}
//		throw new IllegalArgumentException();
//	}
//
//	private void configureWizardPage(NewTypeWizardPage page) {
//		fillInWizardPageName(page);
//		fillInWizardPageSuperTypes(page);
//	}
//
//	/**
//	 * Fill-in the "Package" and "Name" fields.
//	 * @param page the wizard page.
//	 */
//	private void fillInWizardPageName(NewTypeWizardPage page) {
//		// allow to edit when there are type parameters
//		page.setTypeName(fTypeNameWithParameters, fTypeNameWithParameters.indexOf('<') != -1);
//
//		boolean isInEnclosingType= fTypeContainer instanceof IType;
//		if (isInEnclosingType) {
//			page.setEnclosingType((IType) fTypeContainer, true);
//		} else {
//			page.setPackageFragment((IPackageFragment) fTypeContainer, true);
//		}
//		page.setEnclosingTypeSelection(isInEnclosingType, true);
//	}
//
//	/**
//	 * Fill-in the "Super Class" and "Super Interfaces" fields.
//	 * @param page the wizard page.
//	 */
//	private void fillInWizardPageSuperTypes(NewTypeWizardPage page) {
//		ITypeBinding type= getPossibleSuperTypeBinding(fNode);
//		type= Bindings.normalizeTypeBinding(type);
//		if (type != null) {
//			if (type.isArray()) {
//				type= type.getElementType();
//			}
//			if (type.isTopLevel() || type.isMember()) {
//				if (type.isClass() && (fTypeKind == K_CLASS)) {
//					page.setSuperClass(type.getQualifiedName(), true);
//				} else if (type.isInterface()) {
//					List<String> superInterfaces= new ArrayList<String>();
//					superInterfaces.add(type.getQualifiedName());
//					page.setSuperInterfaces(superInterfaces, true);
//				}
//			}
//		}
//	}
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
    if (fTypeKind == K_ANNOTATION) {
        return null;
    }
    AST ast = node.getAST();
    node = ASTNodes.getNormalizedNode(node);
    ASTNode parent = node.getParent();
    switch(parent.getNodeType()) {
        case ASTNode.METHOD_DECLARATION:
            if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
                //$NON-NLS-1$
                return ast.resolveWellKnownType("java.lang.Exception");
            }
            break;
        case ASTNode.THROW_STATEMENT:
            //$NON-NLS-1$
            return ast.resolveWellKnownType("java.lang.Exception");
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) {
                //$NON-NLS-1$
                return ast.resolveWellKnownType("java.lang.Exception");
            }
            break;
        case ASTNode.VARIABLE_DECLARATION_STATEMENT:
        case ASTNode.FIELD_DECLARATION:
            // no guessing for LHS types, cannot be a supertype of a known type
            return null;
        case ASTNode.PARAMETERIZED_TYPE:
            // Inheritance doesn't help: A<X> z= new A<String>(); ->
            return null;
    }
    ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
    if (binding != null && !binding.isRecovered()) {
        return binding;
    }
    return null;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 52 with ASTNode

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

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

the class NewMethodCorrectionProposal method getNewMethodType.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
	 */
@Override
protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
    ASTNode node = getInvocationNode();
    AST ast = rewrite.getAST();
    Type newTypeNode = null;
    ITypeBinding[] otherProposals = null;
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, getImportRewrite());
    if (node.getParent() instanceof MethodInvocation) {
        MethodInvocation parent = (MethodInvocation) node.getParent();
        if (parent.getExpression() == node) {
            ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), parent.getName().getIdentifier(), parent.arguments(), getSenderBinding());
            if (bindings.length > 0) {
                newTypeNode = getImportRewrite().addImport(bindings[0], ast, importRewriteContext);
                otherProposals = bindings;
            }
        }
    }
    if (newTypeNode == null) {
        ITypeBinding binding = ASTResolving.guessBindingForReference(node);
        if (binding != null && binding.isWildcardType()) {
            binding = ASTResolving.normalizeWildcardType(binding, false, ast);
        }
        if (binding != null) {
            newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
        } else {
            ASTNode parent = node.getParent();
            if (parent instanceof ExpressionStatement) {
                newTypeNode = ast.newPrimitiveType(PrimitiveType.VOID);
            } else {
                newTypeNode = ASTResolving.guessTypeForReference(ast, node);
                if (newTypeNode == null) {
                    //$NON-NLS-1$
                    newTypeNode = ast.newSimpleType(ast.newSimpleName("Object"));
                }
            }
        }
    }
    addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
    if (otherProposals != null) {
        for (int i = 0; i < otherProposals.length; i++) {
            addLinkedPositionProposal(KEY_TYPE, otherProposals[i]);
        }
    }
    return newTypeNode;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 54 with ASTNode

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

the class NewMethodCorrectionProposal method getNewName.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewName(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
	 */
@Override
protected SimpleName getNewName(ASTRewrite rewrite) {
    ASTNode invocationNode = getInvocationNode();
    String name;
    if (invocationNode instanceof MethodInvocation) {
        name = ((MethodInvocation) invocationNode).getName().getIdentifier();
    } else if (invocationNode instanceof SuperMethodInvocation) {
        name = ((SuperMethodInvocation) invocationNode).getName().getIdentifier();
    } else {
        // name of the class
        name = getSenderBinding().getName();
    }
    AST ast = rewrite.getAST();
    SimpleName newNameNode = ast.newSimpleName(name);
    addLinkedPosition(rewrite.track(newNameNode), false, KEY_NAME);
    ASTNode invocationName = getInvocationNameNode();
    if (invocationName != null && invocationName.getAST() == ast) {
        // in the same CU
        addLinkedPosition(rewrite.track(invocationName), true, KEY_NAME);
    }
    return newNameNode;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 55 with ASTNode

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

the class NewVariableCorrectionProposal method getDominantNode.

private ASTNode getDominantNode(SimpleName[] names) {
    //ASTResolving.findParentStatement(names[0]);
    ASTNode dominator = names[0];
    for (int i = 1; i < names.length; i++) {
        // ASTResolving.findParentStatement(names[i]);
        ASTNode curr = names[i];
        if (curr != dominator) {
            ASTNode parent = getCommonParent(curr, dominator);
            if (curr.getStartPosition() < dominator.getStartPosition()) {
                dominator = curr;
            }
            while (dominator.getParent() != parent) {
                dominator = dominator.getParent();
            }
        }
    }
    int parentKind = dominator.getParent().getNodeType();
    if (parentKind != ASTNode.BLOCK && parentKind != ASTNode.FOR_STATEMENT) {
        return dominator.getParent();
    }
    return dominator;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)432 SimpleName (org.eclipse.jdt.core.dom.SimpleName)99 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)98 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)90 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)85 Expression (org.eclipse.jdt.core.dom.Expression)81 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)73 AST (org.eclipse.jdt.core.dom.AST)70 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)69 ArrayList (java.util.ArrayList)62 Type (org.eclipse.jdt.core.dom.Type)62 Block (org.eclipse.jdt.core.dom.Block)61 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)49 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)47 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)45 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)44 CastExpression (org.eclipse.jdt.core.dom.CastExpression)42 IBinding (org.eclipse.jdt.core.dom.IBinding)41 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)37