Search in sources :

Example 46 with MethodDeclaration

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

the class CodeRefactoringUtil method checkMethodSyntaxErrors.

public static RefactoringStatus checkMethodSyntaxErrors(int selectionStart, int selectionLength, CompilationUnit cuNode, String invalidSelectionMessage) {
    SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(selectionStart, selectionLength), true);
    cuNode.accept(analyzer);
    ASTNode coveringNode = analyzer.getLastCoveringNode();
    if (!(coveringNode instanceof Block) || !(coveringNode.getParent() instanceof MethodDeclaration))
        return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage);
    if (ASTNodes.getMessages(coveringNode, ASTNodes.NODE_ONLY).length == 0)
        return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage);
    MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
    String message = Messages.format(RefactoringCoreMessages.CodeRefactoringUtil_error_message, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier()));
    return RefactoringStatus.createFatalErrorStatus(message);
}
Also used : SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block)

Example 47 with MethodDeclaration

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

the class ConvertAnonymousToNestedRefactoring method getTypeParameters.

private ITypeBinding[] getTypeParameters() {
    final List<ITypeBinding> parameters = new ArrayList<ITypeBinding>(4);
    final ClassInstanceCreation creation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
    if (fDeclareStatic) {
        final TypeVariableFinder finder = new TypeVariableFinder();
        creation.accept(finder);
        return finder.getResult();
    } else {
        final MethodDeclaration declaration = getEnclosingMethodDeclaration(creation);
        if (declaration != null) {
            ITypeBinding binding = null;
            TypeParameter parameter = null;
            for (final Iterator<TypeParameter> iterator = declaration.typeParameters().iterator(); iterator.hasNext(); ) {
                parameter = iterator.next();
                binding = parameter.resolveBinding();
                if (binding != null)
                    parameters.add(binding);
            }
        }
    }
    final TypeVariableFinder finder = new TypeVariableFinder();
    creation.accept(finder);
    final ITypeBinding[] variables = finder.getResult();
    final List<ITypeBinding> remove = new ArrayList<ITypeBinding>(4);
    boolean match = false;
    ITypeBinding binding = null;
    ITypeBinding variable = null;
    for (final Iterator<ITypeBinding> iterator = parameters.iterator(); iterator.hasNext(); ) {
        match = false;
        binding = iterator.next();
        for (int index = 0; index < variables.length; index++) {
            variable = variables[index];
            if (variable.equals(binding))
                match = true;
        }
        if (!match)
            remove.add(binding);
    }
    parameters.removeAll(remove);
    final ITypeBinding[] result = new ITypeBinding[parameters.size()];
    parameters.toArray(result);
    return result;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList)

Example 48 with MethodDeclaration

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

the class SurroundWithAnalyzer method endVisit.

@Override
public void endVisit(CompilationUnit node) {
    postProcessSelectedNodes(internalGetSelectedNodes());
    BodyDeclaration enclosingNode = null;
    superCall: {
        if (getStatus().hasFatalError())
            break superCall;
        if (!hasSelectedNodes()) {
            ASTNode coveringNode = getLastCoveringNode();
            if (coveringNode instanceof Block) {
                Block block = (Block) coveringNode;
                Message[] messages = ASTNodes.getMessages(block, ASTNodes.NODE_ONLY);
                if (messages.length > 0) {
                    invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_compile_errors, JavaStatusContext.create(getCompilationUnit(), block));
                    break superCall;
                }
            }
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotCover);
            break superCall;
        }
        enclosingNode = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
        if (!(enclosingNode instanceof MethodDeclaration) && !(enclosingNode instanceof Initializer)) {
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
            break superCall;
        }
        if (!onlyStatements()) {
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_onlyStatements);
        }
        fLocals = LocalDeclarationAnalyzer.perform(enclosingNode, getSelection());
    }
    super.endVisit(node);
}
Also used : Message(org.eclipse.jdt.core.dom.Message) Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 49 with MethodDeclaration

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

the class IntroduceFactoryRefactoring method getTargetNode.

/**
	 * Finds and returns the <code>ASTNode</code> for the given source text
	 * selection, if it is an entire constructor call or the class name portion
	 * of a constructor call or constructor declaration, or null otherwise.
	 * @param unit The compilation unit in which the selection was made
	 * @param offset The textual offset of the start of the selection
	 * @param length The length of the selection in characters
	 * @return ClassInstanceCreation or MethodDeclaration
	 */
private ASTNode getTargetNode(ICompilationUnit unit, int offset, int length) {
    ASTNode node = ASTNodes.getNormalizedNode(NodeFinder.perform(fCU, offset, length));
    if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
        return node;
    if (node.getNodeType() == ASTNode.METHOD_DECLARATION && ((MethodDeclaration) node).isConstructor())
        return node;
    // we have some sub node. Make sure its the right child of the parent
    StructuralPropertyDescriptor location = node.getLocationInParent();
    ASTNode parent = node.getParent();
    if (location == ClassInstanceCreation.TYPE_PROPERTY) {
        return parent;
    } else if (location == MethodDeclaration.NAME_PROPERTY && ((MethodDeclaration) parent).isConstructor()) {
        return parent;
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 50 with MethodDeclaration

use of org.eclipse.jdt.core.dom.MethodDeclaration 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)

Aggregations

MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)139 ASTNode (org.eclipse.jdt.core.dom.ASTNode)83 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)48 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)40 AST (org.eclipse.jdt.core.dom.AST)39 Type (org.eclipse.jdt.core.dom.Type)37 Block (org.eclipse.jdt.core.dom.Block)35 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)35 Expression (org.eclipse.jdt.core.dom.Expression)34 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)33 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)29 SimpleName (org.eclipse.jdt.core.dom.SimpleName)29 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)28 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)24 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)24 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)22 Javadoc (org.eclipse.jdt.core.dom.Javadoc)20 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)19 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)18