Search in sources :

Example 81 with MethodDeclaration

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

the class ExtractMethodRefactoring method createNewMethod.

private MethodDeclaration createNewMethod(ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute) throws CoreException {
    MethodDeclaration result = createNewMethodDeclaration();
    result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers()));
    if (fGenerateJavadoc) {
        AbstractTypeDeclaration enclosingType = (AbstractTypeDeclaration) ASTNodes.getParent(fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class);
        String string = CodeGeneration.getMethodComment(fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 82 with MethodDeclaration

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

the class ExtractMethodRefactoring method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration() {
    MethodDeclaration result = fAST.newMethodDeclaration();
    int modifiers = fVisibility;
    BodyDeclaration enclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
    boolean isDestinationInterface = isDestinationInterface();
    if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && enclosingBodyDeclaration.getParent() == fDestination && Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) {
        modifiers = Modifier.NONE;
    }
    boolean shouldBeStatic = false;
    ASTNode currentParent = enclosingBodyDeclaration;
    do {
        if (currentParent instanceof BodyDeclaration) {
            shouldBeStatic = shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent);
        }
        currentParent = currentParent.getParent();
    } while (!shouldBeStatic && currentParent != null && currentParent != fDestination);
    if (shouldBeStatic || fAnalyzer.getForceStatic() || forceStatic()) {
        modifiers |= Modifier.STATIC;
    } else if (isDestinationInterface) {
        modifiers |= Modifier.DEFAULT;
    }
    ITypeBinding[] typeVariables = computeLocalTypeVariables(modifiers);
    List<TypeParameter> typeParameters = result.typeParameters();
    for (int i = 0; i < typeVariables.length; i++) {
        TypeParameter parameter = fAST.newTypeParameter();
        parameter.setName(fAST.newSimpleName(typeVariables[i].getName()));
        ITypeBinding[] bounds = typeVariables[i].getTypeBounds();
        for (int j = 0; j < bounds.length; j++) if (//$NON-NLS-1$
        !"java.lang.Object".equals(bounds[j].getQualifiedName()))
            parameter.typeBounds().add(fImportRewriter.addImport(bounds[j], fAST));
        typeParameters.add(parameter);
    }
    result.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, modifiers));
    result.setReturnType2((Type) ASTNode.copySubtree(fAST, fAnalyzer.getReturnType()));
    result.setName(fAST.newSimpleName(fMethodName));
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(enclosingBodyDeclaration, fImportRewriter);
    List<SingleVariableDeclaration> parameters = result.parameters();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo info = fParameterInfos.get(i);
        VariableDeclaration infoDecl = getVariableDeclaration(info);
        SingleVariableDeclaration parameter = fAST.newSingleVariableDeclaration();
        parameter.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, ASTNodes.getModifiers(infoDecl)));
        parameter.setType(ASTNodeFactory.newType(fAST, infoDecl, fImportRewriter, context));
        parameter.setName(fAST.newSimpleName(info.getNewName()));
        parameter.setVarargs(info.isNewVarargs());
        parameters.add(parameter);
    }
    List<Type> exceptions = result.thrownExceptionTypes();
    ITypeBinding[] exceptionTypes = fAnalyzer.getExceptions(fThrowRuntimeExceptions);
    for (int i = 0; i < exceptionTypes.length; i++) {
        ITypeBinding exceptionType = exceptionTypes[i];
        exceptions.add(fImportRewriter.addImport(exceptionType, fAST, context));
    }
    return result;
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 83 with MethodDeclaration

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

the class ExtractMethodRefactoring method initializeDestinations.

private void initializeDestinations() {
    List<ASTNode> result = new ArrayList<ASTNode>();
    BodyDeclaration decl = fAnalyzer.getEnclosingBodyDeclaration();
    ASTNode current = ASTResolving.findParentType(decl.getParent());
    if (fAnalyzer.isValidDestination(current)) {
        result.add(current);
    }
    if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
        ITypeBinding binding = ASTNodes.getEnclosingType(current);
        ASTNode next = ASTResolving.findParentType(current.getParent());
        while (next != null && binding != null && binding.isNested()) {
            if (fAnalyzer.isValidDestination(next)) {
                result.add(next);
            }
            current = next;
            binding = ASTNodes.getEnclosingType(current);
            next = ASTResolving.findParentType(next.getParent());
        }
    }
    fDestinations = result.toArray(new ASTNode[result.size()]);
    fDestination = fDestinations[fDestinationIndex];
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 84 with MethodDeclaration

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

the class ExtractMethodAnalyzer method computeTypeVariables.

private ITypeBinding[] computeTypeVariables(ITypeBinding[] bindings) {
    Selection selection = getSelection();
    Set<ITypeBinding> result = new HashSet<ITypeBinding>();
    // first remove all type variables that come from outside of the method
    // or are covered by the selection
    CompilationUnit compilationUnit = (CompilationUnit) fEnclosingBodyDeclaration.getRoot();
    for (int i = 0; i < bindings.length; i++) {
        ASTNode decl = compilationUnit.findDeclaringNode(bindings[i]);
        if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration))
            result.add(bindings[i]);
    }
    // all all type variables which are needed since a local variable uses it
    for (int i = 0; i < fArguments.length; i++) {
        IVariableBinding arg = fArguments[i];
        ITypeBinding type = arg.getType();
        if (type != null && type.isTypeVariable()) {
            ASTNode decl = compilationUnit.findDeclaringNode(type);
            if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration))
                result.add(type);
        }
    }
    return result.toArray(new ITypeBinding[result.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Selection(org.eclipse.jdt.internal.corext.dom.Selection) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) HashSet(java.util.HashSet)

Example 85 with MethodDeclaration

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

the class ConvertAnonymousToNestedRefactoring method mustInnerClassBeStatic.

public boolean mustInnerClassBeStatic() {
    ITypeBinding typeBinding = ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
    ASTNode current = fAnonymousInnerClassNode.getParent();
    boolean ans = false;
    while (current != null) {
        switch(current.getNodeType()) {
            case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            case ASTNode.CONSTRUCTOR_INVOCATION:
                return true;
            case ASTNode.ANONYMOUS_CLASS_DECLARATION:
                {
                    AnonymousClassDeclaration enclosingAnonymousClassDeclaration = (AnonymousClassDeclaration) current;
                    ITypeBinding binding = enclosingAnonymousClassDeclaration.resolveBinding();
                    if (binding != null && Bindings.isSuperType(typeBinding, binding.getSuperclass())) {
                        return false;
                    }
                    break;
                }
            case ASTNode.FIELD_DECLARATION:
                {
                    FieldDeclaration enclosingFieldDeclaration = (FieldDeclaration) current;
                    if (Modifier.isStatic(enclosingFieldDeclaration.getModifiers())) {
                        ans = true;
                    }
                    break;
                }
            case ASTNode.METHOD_DECLARATION:
                {
                    MethodDeclaration enclosingMethodDeclaration = (MethodDeclaration) current;
                    if (Modifier.isStatic(enclosingMethodDeclaration.getModifiers())) {
                        ans = true;
                    }
                    break;
                }
            case ASTNode.TYPE_DECLARATION:
                {
                    return ans;
                }
        }
        current = current.getParent();
    }
    return ans;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) 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