Search in sources :

Example 1 with ConstructorInvocation

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

the class ASTNodeSearchUtil method getAstNode.

public static ASTNode getAstNode(CompilationUnit cuNode, int start, int length) {
    SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(start, length), true);
    cuNode.accept(analyzer);
    //XXX workaround for jdt core feature 23527
    ASTNode node = analyzer.getFirstSelectedNode();
    if (node == null && analyzer.getLastCoveringNode() instanceof SuperConstructorInvocation)
        node = analyzer.getLastCoveringNode().getParent();
    else if (node == null && analyzer.getLastCoveringNode() instanceof ConstructorInvocation)
        node = analyzer.getLastCoveringNode().getParent();
    if (node == null)
        return null;
    ASTNode parentNode = node.getParent();
    if (parentNode instanceof MethodDeclaration) {
        MethodDeclaration md = (MethodDeclaration) parentNode;
        if (!(node instanceof SimpleName) && md.isConstructor() && md.getBody() != null && md.getBody().statements().size() > 0 && (md.getBody().statements().get(0) instanceof ConstructorInvocation || md.getBody().statements().get(0) instanceof SuperConstructorInvocation) && ((ASTNode) md.getBody().statements().get(0)).getLength() == length + 1)
            return (ASTNode) md.getBody().statements().get(0);
    }
    if (parentNode instanceof SuperConstructorInvocation) {
        if (parentNode.getLength() == length + 1)
            return parentNode;
    }
    if (parentNode instanceof ConstructorInvocation) {
        if (parentNode.getLength() == length + 1)
            return parentNode;
    }
    return node;
}
Also used : SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 2 with ConstructorInvocation

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

the class JavadocFinder method resolveBinding.

private static IBinding resolveBinding(ASTNode node) {
    if (node instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) node;
        // workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
        ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
        if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
            ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
            IMethodBinding constructorBinding = cic.resolveConstructorBinding();
            if (constructorBinding == null)
                return null;
            ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
            if (!declaringClass.isAnonymous())
                return constructorBinding;
            ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
            return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
        }
        return simpleName.resolveBinding();
    } else if (node instanceof SuperConstructorInvocation) {
        return ((SuperConstructorInvocation) node).resolveConstructorBinding();
    } else if (node instanceof ConstructorInvocation) {
        return ((ConstructorInvocation) node).resolveConstructorBinding();
    } else {
        return null;
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 3 with ConstructorInvocation

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

the class IntroduceFactoryRefactoring method getCtorCallAt.

/**
	 * Look "in the vicinity" of the given range to find the <code>ClassInstanceCreation</code>
	 * node that this search hit identified. Necessary because the <code>SearchEngine</code>
	 * doesn't always cough up text extents that <code>NodeFinder.perform()</code> agrees with.
	 * @param start
	 * @param length
	 * @param unitAST
	 * @return return a {@link ClassInstanceCreation} or a {@link MethodRef} or <code>null</code> if this is really a constructor->constructor call (e.g. "this(...)")
	 * @throws CoreException
	 */
private ASTNode getCtorCallAt(int start, int length, CompilationUnit unitAST) throws CoreException {
    ICompilationUnit unitHandle = ASTCreator.getCu(unitAST);
    ASTNode node = NodeFinder.perform(unitAST, start, length);
    if (node == null)
        throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noASTNodeForConstructorSearchHit, new Object[] { Integer.toString(start), Integer.toString(start + length), BasicElementLabels.getJavaCodeString(unitHandle.getSource().substring(start, start + length)), BasicElementLabels.getFileName(unitHandle) }), null));
    if (node instanceof ClassInstanceCreation) {
        if (((ClassInstanceCreation) node).getAnonymousClassDeclaration() != null) {
            // Cannot replace anonymous inner class, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=250660
            fConstructorVisibility = Modifier.PROTECTED;
            return null;
        }
        return node;
    } else if (node instanceof VariableDeclaration) {
        Expression init = ((VariableDeclaration) node).getInitializer();
        if (init instanceof ClassInstanceCreation) {
            return init;
        } else if (init != null)
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedInitializerNodeType, new Object[] { BasicElementLabels.getJavaCodeString(init.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
        else
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noConstructorCallNodeInsideFoundVarbleDecl, BasicElementLabels.getJavaCodeString(node.toString())), null));
    } else if (node instanceof ConstructorInvocation) {
        // to another flavor on the same class.
        return null;
    } else if (node instanceof SuperConstructorInvocation) {
        // This is a call we can bypass; it's from one constructor flavor
        // to another flavor on the same class.
        fConstructorVisibility = Modifier.PROTECTED;
        return null;
    } else if (node instanceof ExpressionStatement) {
        Expression expr = ((ExpressionStatement) node).getExpression();
        if (expr instanceof ClassInstanceCreation)
            return expr;
        else
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaCodeString(expr.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
    } else if (node instanceof SimpleName && (node.getParent() instanceof MethodDeclaration || node.getParent() instanceof AbstractTypeDeclaration)) {
        // We seem to have been given a hit for an implicit call to the base-class constructor.
        // Do nothing with this (implicit) call, but have to make sure we make the derived class
        // doesn't lose access to the base-class constructor (so make it 'protected', not 'private').
        fConstructorVisibility = Modifier.PROTECTED;
        return null;
    } else if (node instanceof MethodRef) {
        return node;
    } else
        throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaElementName(node.getClass().getName() + "('" + node.toString() + "')"), BasicElementLabels.getFileName(unitHandle) }), //$NON-NLS-1$ //$NON-NLS-2$
        null));
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodRef(org.eclipse.jdt.core.dom.MethodRef) CoreException(org.eclipse.core.runtime.CoreException) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) Expression(org.eclipse.jdt.core.dom.Expression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 4 with ConstructorInvocation

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

the class InlineMethodRefactoring method setCurrentMode.

public RefactoringStatus setCurrentMode(Mode mode) throws JavaModelException {
    if (fCurrentMode == mode)
        return new RefactoringStatus();
    Assert.isTrue(getInitialMode() == Mode.INLINE_SINGLE);
    fCurrentMode = mode;
    if (mode == Mode.INLINE_SINGLE) {
        if (fInitialNode instanceof MethodInvocation)
            fTargetProvider = TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (MethodInvocation) fInitialNode);
        else if (fInitialNode instanceof SuperMethodInvocation)
            fTargetProvider = TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (SuperMethodInvocation) fInitialNode);
        else if (fInitialNode instanceof ConstructorInvocation)
            fTargetProvider = TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (ConstructorInvocation) fInitialNode);
        else
            throw new IllegalStateException(String.valueOf(fInitialNode));
    } else {
        fTargetProvider = TargetProvider.create(fSourceProvider.getDeclaration());
    }
    return fTargetProvider.checkActivation();
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 5 with ConstructorInvocation

use of org.eclipse.jdt.core.dom.ConstructorInvocation in project whole by wholeplatform.

the class CompilationUnitBuilder method newConstructorInvocation.

public ConstructorInvocation newConstructorInvocation(Expression arg0) {
    ConstructorInvocation callExp = newConstructorInvocation();
    callExp.arguments().add(arg0);
    return callExp;
}
Also used : ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Aggregations

ConstructorInvocation (org.eclipse.jdt.core.dom.ConstructorInvocation)11 SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)8 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 Block (org.eclipse.jdt.core.dom.Block)4 Expression (org.eclipse.jdt.core.dom.Expression)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)4 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3 SimpleType (org.eclipse.jdt.core.dom.SimpleType)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 Assignment (org.eclipse.jdt.core.dom.Assignment)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2