Search in sources :

Example 1 with AbstractMethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project che by eclipse.

the class HandleFactory method createElement.

/**
	 * Create handle by adding child to parent obtained by recursing into parent scopes.
	 */
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit, HashSet existingElements, HashMap knownScopes) {
    IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
    if (newElement != null)
        return newElement;
    switch(scope.kind) {
        case Scope.COMPILATION_UNIT_SCOPE:
            newElement = unit;
            break;
        case Scope.CLASS_SCOPE:
            IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            switch(parentElement.getElementType()) {
                case IJavaElement.COMPILATION_UNIT:
                    newElement = ((ICompilationUnit) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
                    break;
                case IJavaElement.TYPE:
                    newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
                    break;
                case IJavaElement.FIELD:
                case IJavaElement.INITIALIZER:
                case IJavaElement.METHOD:
                    IMember member = (IMember) parentElement;
                    if (member.isBinary()) {
                        return null;
                    } else {
                        newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
                        // increment occurrence count if collision is detected
                        if (newElement != null) {
                            while (!existingElements.add(newElement)) ((SourceRefElement) newElement).occurrenceCount++;
                        }
                    }
                    break;
            }
            if (newElement != null) {
                knownScopes.put(scope, newElement);
            }
            break;
        case Scope.METHOD_SCOPE:
            if (scope.isLambdaScope()) {
                parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
                LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
                if (expression.resolvedType != null && expression.resolvedType.isValidBinding() && !(expression.descriptor instanceof ProblemMethodBinding)) {
                    // chain in lambda element only if resolved properly.
                    //newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression).getMethod();
                    newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression).getMethod();
                    knownScopes.put(scope, newElement);
                    return newElement;
                }
                return parentElement;
            }
            IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            MethodScope methodScope = (MethodScope) scope;
            if (methodScope.isInsideInitializer()) {
                // inside field or initializer, must find proper one
                TypeDeclaration type = methodScope.referenceType();
                int occurenceCount = 1;
                int length = type.fields == null ? 0 : type.fields.length;
                for (int i = 0; i < length; i++) {
                    FieldDeclaration field = type.fields[i];
                    if (field.declarationSourceStart <= elementPosition && elementPosition <= field.declarationSourceEnd) {
                        switch(field.getKind()) {
                            case AbstractVariableDeclaration.FIELD:
                            case AbstractVariableDeclaration.ENUM_CONSTANT:
                                newElement = parentType.getField(new String(field.name));
                                break;
                            case AbstractVariableDeclaration.INITIALIZER:
                                newElement = parentType.getInitializer(occurenceCount);
                                break;
                        }
                        break;
                    } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
                        occurenceCount++;
                    }
                }
            } else {
                // method element
                AbstractMethodDeclaration method = methodScope.referenceMethod();
                newElement = parentType.getMethod(new String(method.selector), Util.typeParameterSignatures(method));
                if (newElement != null) {
                    knownScopes.put(scope, newElement);
                }
            }
            break;
        case Scope.BLOCK_SCOPE:
            // standard block, no element per se
            newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            break;
    }
    return newElement;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) MethodScope(org.eclipse.jdt.internal.compiler.lookup.MethodScope) IMember(org.eclipse.jdt.core.IMember) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) SourceRefElement(org.eclipse.jdt.internal.core.SourceRefElement) IType(org.eclipse.jdt.core.IType)

Example 2 with AbstractMethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project che by eclipse.

the class CheCompilationUnitResolver method resolve.

private CompilationUnitDeclaration resolve(CompilationUnitDeclaration unit, org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, NodeSearcher nodeSearcher, boolean verifyMethods, boolean analyzeCode, boolean generateCode) {
    try {
        if (unit == null) {
            // build and record parsed units
            // will request a full parse
            this.parseThreshold = 0;
            beginToCompile(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] { sourceUnit });
            // find the right unit from what was injected via accept(ICompilationUnit,..):
            for (int i = 0, max = this.totalUnits; i < max; i++) {
                CompilationUnitDeclaration currentCompilationUnitDeclaration = this.unitsToProcess[i];
                if (currentCompilationUnitDeclaration != null && currentCompilationUnitDeclaration.compilationResult.compilationUnit == sourceUnit) {
                    unit = currentCompilationUnitDeclaration;
                    break;
                }
            }
            if (unit == null) {
                // fall back to old behavior
                unit = this.unitsToProcess[0];
            }
        } else {
            // initial type binding creation
            this.lookupEnvironment.buildTypeBindings(unit, null);
            // binding resolution
            this.lookupEnvironment.completeTypeBindings();
        }
        if (nodeSearcher == null) {
            // no-op if method bodies have already been parsed
            this.parser.getMethodBodies(unit);
        } else {
            int searchPosition = nodeSearcher.position;
            char[] source = sourceUnit.getContents();
            int length = source.length;
            if (searchPosition >= 0 && searchPosition <= length) {
                unit.traverse(nodeSearcher, unit.scope);
                org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodeSearcher.found;
                if (node != null) {
                    // save existing values to restore them at the end of the parsing process
                    // see bug 47079 for more details
                    int[] oldLineEnds = this.parser.scanner.lineEnds;
                    int oldLinePtr = this.parser.scanner.linePtr;
                    this.parser.scanner.setSource(source, unit.compilationResult);
                    org.eclipse.jdt.internal.compiler.ast.TypeDeclaration enclosingTypeDeclaration = nodeSearcher.enclosingType;
                    if (node instanceof AbstractMethodDeclaration) {
                        ((AbstractMethodDeclaration) node).parseStatements(this.parser, unit);
                    } else if (enclosingTypeDeclaration != null) {
                        if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) {
                            ((org.eclipse.jdt.internal.compiler.ast.Initializer) node).parseStatements(this.parser, enclosingTypeDeclaration, unit);
                        } else if (node instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) {
                            ((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) node).parseMethods(this.parser, unit);
                        }
                    }
                    // this is done to prevent any side effects on the compilation unit result
                    // line separator positions array.
                    this.parser.scanner.lineEnds = oldLineEnds;
                    this.parser.scanner.linePtr = oldLinePtr;
                }
            }
        }
        if (unit.scope != null) {
            // fault in fields & methods
            unit.scope.faultInTypes();
            if (unit.scope != null && verifyMethods) {
                // http://dev.eclipse.org/bugs/show_bug.cgi?id=23117
                // verify inherited methods
                unit.scope.verifyMethods(this.lookupEnvironment.methodVerifier());
            }
            // type checking
            unit.resolve();
            // flow analysis
            if (analyzeCode)
                unit.analyseCode();
            // code generation
            if (generateCode)
                unit.generateCode();
            // finalize problems (suppressWarnings)
            unit.finalizeProblems();
        }
        // release reference to processed unit declaration
        if (this.unitsToProcess != null)
            this.unitsToProcess[0] = null;
        this.requestor.acceptResult(unit.compilationResult.tagAsAccepted());
        return unit;
    } catch (org.eclipse.jdt.internal.compiler.problem.AbortCompilation e) {
        this.handleInternalException(e, unit);
        return unit == null ? this.unitsToProcess[0] : unit;
    } catch (Error e) {
        this.handleInternalException(e, unit, null);
        // rethrow
        throw e;
    } catch (RuntimeException e) {
        this.handleInternalException(e, unit, null);
        // rethrow
        throw e;
    } finally {
    // No reset is performed there anymore since,
    // within the CodeAssist (or related tools),
    // the compiler may be called *after* a call
    // to this resolve(...) method. And such a call
    // needs to have a compiler with a non-empty
    // environment.
    // this.reset();
    }
}
Also used : CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Example 3 with AbstractMethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project che by eclipse.

the class CodenvyCompilationUnitResolver method resolve.

private CompilationUnitDeclaration resolve(CompilationUnitDeclaration unit, org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, NodeSearcher nodeSearcher, boolean verifyMethods, boolean analyzeCode, boolean generateCode) {
    try {
        if (unit == null) {
            // build and record parsed units
            // will request a full parse
            this.parseThreshold = 0;
            beginToCompile(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] { sourceUnit });
            // find the right unit from what was injected via accept(ICompilationUnit,..):
            for (int i = 0, max = this.totalUnits; i < max; i++) {
                CompilationUnitDeclaration currentCompilationUnitDeclaration = this.unitsToProcess[i];
                if (currentCompilationUnitDeclaration != null && currentCompilationUnitDeclaration.compilationResult.compilationUnit == sourceUnit) {
                    unit = currentCompilationUnitDeclaration;
                    break;
                }
            }
            if (unit == null) {
                // fall back to old behavior
                unit = this.unitsToProcess[0];
            }
        } else {
            // initial type binding creation
            this.lookupEnvironment.buildTypeBindings(unit, null);
            // binding resolution
            this.lookupEnvironment.completeTypeBindings();
        }
        if (nodeSearcher == null) {
            // no-op if method bodies have already been parsed
            this.parser.getMethodBodies(unit);
        } else {
            int searchPosition = nodeSearcher.position;
            char[] source = sourceUnit.getContents();
            int length = source.length;
            if (searchPosition >= 0 && searchPosition <= length) {
                unit.traverse(nodeSearcher, unit.scope);
                org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodeSearcher.found;
                if (node != null) {
                    // save existing values to restore them at the end of the parsing process
                    // see bug 47079 for more details
                    int[] oldLineEnds = this.parser.scanner.lineEnds;
                    int oldLinePtr = this.parser.scanner.linePtr;
                    this.parser.scanner.setSource(source, unit.compilationResult);
                    org.eclipse.jdt.internal.compiler.ast.TypeDeclaration enclosingTypeDeclaration = nodeSearcher.enclosingType;
                    if (node instanceof AbstractMethodDeclaration) {
                        ((AbstractMethodDeclaration) node).parseStatements(this.parser, unit);
                    } else if (enclosingTypeDeclaration != null) {
                        if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) {
                            ((org.eclipse.jdt.internal.compiler.ast.Initializer) node).parseStatements(this.parser, enclosingTypeDeclaration, unit);
                        } else if (node instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) {
                            ((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) node).parseMethods(this.parser, unit);
                        }
                    }
                    // this is done to prevent any side effects on the compilation unit result
                    // line separator positions array.
                    this.parser.scanner.lineEnds = oldLineEnds;
                    this.parser.scanner.linePtr = oldLinePtr;
                }
            }
        }
        if (unit.scope != null) {
            // fault in fields & methods
            unit.scope.faultInTypes();
            if (unit.scope != null && verifyMethods) {
                // http://dev.eclipse.org/bugs/show_bug.cgi?id=23117
                // verify inherited methods
                unit.scope.verifyMethods(this.lookupEnvironment.methodVerifier());
            }
            // type checking
            unit.resolve();
            // flow analysis
            if (analyzeCode)
                unit.analyseCode();
            // code generation
            if (generateCode)
                unit.generateCode();
            // finalize problems (suppressWarnings)
            unit.finalizeProblems();
        }
        // release reference to processed unit declaration
        if (this.unitsToProcess != null)
            this.unitsToProcess[0] = null;
        this.requestor.acceptResult(unit.compilationResult.tagAsAccepted());
        return unit;
    } catch (org.eclipse.jdt.internal.compiler.problem.AbortCompilation e) {
        this.handleInternalException(e, unit);
        return unit == null ? this.unitsToProcess[0] : unit;
    } catch (Error e) {
        this.handleInternalException(e, unit, null);
        // rethrow
        throw e;
    } catch (RuntimeException e) {
        this.handleInternalException(e, unit, null);
        // rethrow
        throw e;
    } finally {
    // No reset is performed there anymore since,
    // within the CodeAssist (or related tools),
    // the compiler may be called *after* a call
    // to this resolve(...) method. And such a call
    // needs to have a compiler with a non-empty
    // environment.
    // this.reset();
    }
}
Also used : CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Example 4 with AbstractMethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project lombok by rzwitserloot.

the class HandleUtilityClass method changeModifiersAndGenerateConstructor.

private void changeModifiersAndGenerateConstructor(EclipseNode typeNode, EclipseNode annotationNode) {
    TypeDeclaration classDecl = (TypeDeclaration) typeNode.get();
    boolean makeConstructor = true;
    classDecl.modifiers |= ClassFileConstants.AccFinal;
    boolean markStatic = true;
    boolean requiresClInit = false;
    boolean alreadyHasClinit = false;
    if (typeNode.up().getKind() == Kind.COMPILATION_UNIT)
        markStatic = false;
    if (markStatic && typeNode.up().getKind() == Kind.TYPE) {
        TypeDeclaration typeDecl = (TypeDeclaration) typeNode.up().get();
        if ((typeDecl.modifiers & ClassFileConstants.AccInterface) != 0)
            markStatic = false;
    }
    if (markStatic)
        classDecl.modifiers |= ClassFileConstants.AccStatic;
    for (EclipseNode element : typeNode.down()) {
        if (element.getKind() == Kind.FIELD) {
            FieldDeclaration fieldDecl = (FieldDeclaration) element.get();
            if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) == 0) {
                requiresClInit = true;
                fieldDecl.modifiers |= ClassFileConstants.AccStatic;
            }
        } else if (element.getKind() == Kind.METHOD) {
            AbstractMethodDeclaration amd = (AbstractMethodDeclaration) element.get();
            if (amd instanceof ConstructorDeclaration) {
                ConstructorDeclaration constrDecl = (ConstructorDeclaration) element.get();
                if (getGeneratedBy(constrDecl) == null && (constrDecl.bits & ASTNode.IsDefaultConstructor) == 0) {
                    element.addError("@UtilityClasses cannot have declared constructors.");
                    makeConstructor = false;
                    continue;
                }
            } else if (amd instanceof MethodDeclaration) {
                amd.modifiers |= ClassFileConstants.AccStatic;
            } else if (amd instanceof Clinit) {
                alreadyHasClinit = true;
            }
        } else if (element.getKind() == Kind.TYPE) {
            ((TypeDeclaration) element.get()).modifiers |= ClassFileConstants.AccStatic;
        }
    }
    if (makeConstructor)
        createPrivateDefaultConstructor(typeNode, annotationNode);
    if (requiresClInit && !alreadyHasClinit)
        classDecl.addClinit();
}
Also used : Clinit(org.eclipse.jdt.internal.compiler.ast.Clinit) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 5 with AbstractMethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration in project lombok by rzwitserloot.

the class EclipseNode method traverse.

/**
 * Visits this node and all child nodes depth-first, calling the provided visitor's visit methods.
 */
public void traverse(EclipseASTVisitor visitor) {
    if (!this.isCompleteParse() && visitor.getClass().isAnnotationPresent(DeferUntilPostDiet.class))
        return;
    switch(getKind()) {
        case COMPILATION_UNIT:
            visitor.visitCompilationUnit(this, (CompilationUnitDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitCompilationUnit(this, (CompilationUnitDeclaration) get());
            break;
        case TYPE:
            visitor.visitType(this, (TypeDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitType(this, (TypeDeclaration) get());
            break;
        case FIELD:
            visitor.visitField(this, (FieldDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitField(this, (FieldDeclaration) get());
            break;
        case INITIALIZER:
            visitor.visitInitializer(this, (Initializer) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitInitializer(this, (Initializer) get());
            break;
        case METHOD:
            if (get() instanceof Clinit)
                return;
            visitor.visitMethod(this, (AbstractMethodDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitMethod(this, (AbstractMethodDeclaration) get());
            break;
        case ARGUMENT:
            AbstractMethodDeclaration method = (AbstractMethodDeclaration) up().get();
            visitor.visitMethodArgument(this, (Argument) get(), method);
            ast.traverseChildren(visitor, this);
            visitor.endVisitMethodArgument(this, (Argument) get(), method);
            break;
        case LOCAL:
            visitor.visitLocal(this, (LocalDeclaration) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitLocal(this, (LocalDeclaration) get());
            break;
        case ANNOTATION:
            switch(up().getKind()) {
                case TYPE:
                    visitor.visitAnnotationOnType((TypeDeclaration) up().get(), this, (Annotation) get());
                    break;
                case FIELD:
                    visitor.visitAnnotationOnField((FieldDeclaration) up().get(), this, (Annotation) get());
                    break;
                case METHOD:
                    visitor.visitAnnotationOnMethod((AbstractMethodDeclaration) up().get(), this, (Annotation) get());
                    break;
                case ARGUMENT:
                    visitor.visitAnnotationOnMethodArgument((Argument) parent.get(), (AbstractMethodDeclaration) parent.directUp().get(), this, (Annotation) get());
                    break;
                case LOCAL:
                    visitor.visitAnnotationOnLocal((LocalDeclaration) parent.get(), this, (Annotation) get());
                    break;
                default:
                    throw new AssertionError("Annotation not expected as child of a " + up().getKind());
            }
            break;
        case STATEMENT:
            visitor.visitStatement(this, (Statement) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitStatement(this, (Statement) get());
            break;
        default:
            throw new AssertionError("Unexpected kind during node traversal: " + getKind());
    }
}
Also used : Clinit(org.eclipse.jdt.internal.compiler.ast.Clinit) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Aggregations

AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)20 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)12 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)11 EclipseNode (lombok.eclipse.EclipseNode)6 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)5 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)5 ArrayList (java.util.ArrayList)4 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)4 ConstructorDeclaration (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration)4 TypeParameter (org.eclipse.jdt.internal.compiler.ast.TypeParameter)4 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)3 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)3 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)3 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)2 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)2 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)2 Clinit (org.eclipse.jdt.internal.compiler.ast.Clinit)2 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)2