Example usage for org.eclipse.jdt.internal.compiler.ast MethodDeclaration isAbstract

List of usage examples for org.eclipse.jdt.internal.compiler.ast MethodDeclaration isAbstract

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast MethodDeclaration isAbstract.

Prototype

public boolean isAbstract() 

Source Link

Usage

From source file:lombok.eclipse.handlers.HandleSynchronized.java

License:Open Source License

@Override
public void preHandle(AnnotationValues<Synchronized> annotation, Annotation source,
        EclipseNode annotationNode) {//from w w w  .j  av  a 2s .  c o m
    EclipseNode methodNode = annotationNode.up();
    if (methodNode == null || methodNode.getKind() != Kind.METHOD
            || !(methodNode.get() instanceof MethodDeclaration))
        return;
    MethodDeclaration method = (MethodDeclaration) methodNode.get();
    if (method.isAbstract())
        return;

    createLockField(annotation, annotationNode, method.isStatic(), false);
}

From source file:lombok.eclipse.handlers.HandleSynchronized.java

License:Open Source License

@Override
public void handle(AnnotationValues<Synchronized> annotation, Annotation source, EclipseNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.SYNCHRONIZED_FLAG_USAGE, "@Synchronized");

    int p1 = source.sourceStart - 1;
    int p2 = source.sourceStart - 2;
    long pos = (((long) p1) << 32) | p2;
    EclipseNode methodNode = annotationNode.up();
    if (methodNode == null || methodNode.getKind() != Kind.METHOD
            || !(methodNode.get() instanceof MethodDeclaration)) {
        annotationNode.addError("@Synchronized is legal only on methods.");
        return;/*from  w ww  . j a v  a2s  .  c  om*/
    }

    MethodDeclaration method = (MethodDeclaration) methodNode.get();
    if (method.isAbstract()) {
        annotationNode.addError("@Synchronized is legal only on concrete methods.");
        return;
    }

    char[] lockName = createLockField(annotation, annotationNode, method.isStatic(), true);
    if (lockName == null)
        return;
    if (method.statements == null)
        return;

    Block block = new Block(0);
    setGeneratedBy(block, source);
    block.statements = method.statements;

    // Positions for in-method generated nodes are special
    block.sourceEnd = method.bodyEnd;
    block.sourceStart = method.bodyStart;

    Expression lockVariable;
    if (method.isStatic())
        lockVariable = new QualifiedNameReference(
                new char[][] { methodNode.up().getName().toCharArray(), lockName }, new long[] { pos, pos }, p1,
                p2);
    else {
        lockVariable = new FieldReference(lockName, pos);
        ThisReference thisReference = new ThisReference(p1, p2);
        setGeneratedBy(thisReference, source);
        ((FieldReference) lockVariable).receiver = thisReference;
    }
    setGeneratedBy(lockVariable, source);

    method.statements = new Statement[] { new SynchronizedStatement(lockVariable, block, 0, 0) };

    // Positions for in-method generated nodes are special
    method.statements[0].sourceEnd = method.bodyEnd;
    method.statements[0].sourceStart = method.bodyStart;

    setGeneratedBy(method.statements[0], source);

    methodNode.rebuild();
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

public void parse(MethodDeclaration md, CompilationUnitDeclaration unit) {
    //only parse the method body of md
    //fill out method statements

    //convert bugs into parse error

    if (md.isAbstract())
        return;// w  w  w  .  j a  v a  2s  .c om
    if (md.isNative())
        return;
    if ((md.modifiers & ExtraCompilerModifiers.AccSemicolonBody) != 0)
        return;

    boolean oldMethodRecoveryActivated = this.methodRecoveryActivated;
    if (this.options.performMethodsFullRecovery) {
        this.methodRecoveryActivated = true;
        this.rParenPos = md.sourceEnd;
    }
    initialize();
    goForBlockStatementsopt();
    this.nestedMethod[this.nestedType]++;
    pushOnRealBlockStack(0);

    this.referenceContext = md;
    this.compilationUnit = unit;

    this.scanner.resetTo(md.bodyStart, md.bodyEnd);
    // reset the scanner to parser from { down to }
    try {
        parse();
    } catch (AbortCompilation ex) {
        this.lastAct = ERROR_ACTION;
    } finally {
        this.nestedMethod[this.nestedType]--;
        if (this.options.performStatementsRecovery) {
            this.methodRecoveryActivated = oldMethodRecoveryActivated;
        }
    }

    checkNonNLSAfterBodyEnd(md.declarationSourceEnd);

    if (this.lastAct == ERROR_ACTION) {
        md.bits |= ASTNode.HasSyntaxErrors;
        return;
    }

    //refill statements
    md.explicitDeclarations = this.realBlockStack[this.realBlockPtr--];
    int length;
    if (this.astLengthPtr > -1 && (length = this.astLengthStack[this.astLengthPtr--]) != 0) {
        if (this.options.ignoreMethodBodies) {
            // ignore statements
            this.astPtr -= length;
        } else {
            System.arraycopy(this.astStack, (this.astPtr -= length) + 1, md.statements = new Statement[length],
                    0, length);
        }
    } else {
        if (!containsComment(md.bodyStart, md.bodyEnd)) {
            md.bits |= ASTNode.UndocumentedEmptyBlock;
        }
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.TransformStatementsVisitor.java

License:Open Source License

@Override
public void endVisit(MethodDeclaration methodDecl, ClassScope scope) {
    if (checkPopCallinMethod(methodDecl)) {
        // assume method had no errors (else it would not have been pushed)
        TypeBinding returnType = MethodModel.getReturnType(methodDecl.binding);
        // return type was already adjusted by SourceTypeBinding.resolveTypesFor()->MethodSignatureEnhance.generalizeReturnType()
        if (returnType == TypeBinding.VOID && !methodDecl.isGenerated && !methodDecl.isCopied
                && !methodDecl.isAbstract()) {
            AstGenerator gen = new AstGenerator(methodDecl.bodyEnd, methodDecl.bodyEnd);
            if (methodDecl.statements == null) {
                methodDecl.setStatements(
                        new Statement[] { gen.returnStatement(gen.nullLiteral(), true/*synthetic*/) });
            } else {
                // bracket body with:
                // Object _OT$result = null;
                // ... ( meanwhile a BaseCallMessageSend might assign to _OT$result )
                // return _OT$result;
                int len = methodDecl.statements.length;
                Statement[] newStatements = new Statement[len + 2];
                System.arraycopy(methodDecl.statements, 0, newStatements, 1, len);

                //save source positions from AstGenerator (ike)
                SourcePosition savePos = gen.getSourcePosition();

                try {
                    //set to first line of this method (if any)
                    if (len > 0)
                        gen.setSourcePosition((((long) methodDecl.statements[0].sourceStart) << 32)
                                + methodDecl.statements[0].sourceEnd);

                    //generate local variable (ike)
                    newStatements[0] = gen.localVariable(OT_RESULT, scope.getJavaLangObject(),
                            gen.nullLiteral());

                } finally {
                    //restore source postions (ike)
                    gen.setSourcePosition(savePos);
                }/*w ww.j  ava2 s .co  m*/

                newStatements[len + 1] = gen.returnStatement(gen.singleNameReference(OT_RESULT),
                        true/*synthetic*/);
                methodDecl.setStatements(newStatements);
            }
        }
    }
    super.endVisit(methodDecl, scope);
}

From source file:spoon.support.builder.JDTTreeBuilder.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w w  w .  j  av a  2  s . c o  m
public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
    CtMethod<?> m = factory.Core().createMethod();
    m.setSimpleName(new String(methodDeclaration.selector));
    m.setType(references.getTypeReference(methodDeclaration.returnType.resolvedType));
    m.setModifiers(getModifier(methodDeclaration.modifiers));
    if (methodDeclaration.thrownExceptions != null) {
        for (TypeReference r : methodDeclaration.thrownExceptions)
            m.getThrownTypes().add(references.getTypeReference(r.resolvedType));
    }
    for (TypeBinding b : methodDeclaration.binding.typeVariables) {
        m.getFormalTypeParameters().add(references.getBoundedTypeReference(b));
    }

    m.setDocComment(getJavaDoc(methodDeclaration.javadoc, scope.referenceCompilationUnit()));

    context.enter(m, methodDeclaration);

    if (methodDeclaration.annotations != null)
        for (Annotation a : methodDeclaration.annotations)
            a.traverse(this, methodDeclaration.scope);

    if (methodDeclaration.arguments != null)
        for (Argument a : methodDeclaration.arguments)
            a.traverse(this, methodDeclaration.scope);

    // Create block
    if (!methodDeclaration.isAbstract() && (methodDeclaration.modifiers & ClassFileConstants.AccNative) == 0) {
        CtBlock<?> b = factory.Core().createBlock();
        context.enter(b, methodDeclaration);
    }

    if (methodDeclaration.statements != null) {
        for (Statement s : methodDeclaration.statements)
            s.traverse(this, methodDeclaration.scope);
    }
    return false;
}

From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
    CtMethod<Object> m = factory.Core().createMethod();
    m.setSimpleName(new String(methodDeclaration.selector));
    m.setType(references.getTypeReference(methodDeclaration.returnType.resolvedType));
    m.setModifiers(getModifiers(methodDeclaration.modifiers));
    m.setDefaultMethod(methodDeclaration.isDefaultMethod());
    if (methodDeclaration.thrownExceptions != null) {
        for (TypeReference r : methodDeclaration.thrownExceptions) {
            CtTypeReference<? extends Throwable> tr = references.getTypeReference(r.resolvedType);
            m.addThrownType(tr);//from w w  w.  j a  v a 2 s.  c o  m
        }
    }

    // this may happen when working
    if (methodDeclaration.binding != null) {
        // with incomplete classpath
        for (TypeBinding b : methodDeclaration.binding.typeVariables) {
            m.addFormalTypeParameter(references.getBoundedTypeReference(b));
        }
    }

    if (scope != null) {
        m.setDocComment(getJavaDoc(methodDeclaration.javadoc, scope.referenceCompilationUnit()));
    } else if (methodDeclaration.scope != null) {
        m.setDocComment(
                getJavaDoc(methodDeclaration.javadoc, methodDeclaration.scope.referenceCompilationUnit()));
    } else {
        // null scope for "+methodDeclaration
    }

    context.enter(m, methodDeclaration);

    if (methodDeclaration.annotations != null) {
        for (Annotation a : methodDeclaration.annotations) {
            // TODO Sorry for that but there is a bug in JDT : https://bugs.eclipse.org/bugs/show_bug.cgi?id=459528
            if (isContainsInTypeAnnotation(methodDeclaration.returnType.resolvedType, a)) {
                a.traverse(this, methodDeclaration.scope);
            }
        }
    }

    if (methodDeclaration.arguments != null) {
        for (Argument a : methodDeclaration.arguments) {
            a.traverse(this, methodDeclaration.scope);
        }
    }

    // Create block
    if (!methodDeclaration.isAbstract() && (methodDeclaration.modifiers & ClassFileConstants.AccNative) == 0) {
        CtBlock<?> b = factory.Core().createBlock();
        context.enter(b, methodDeclaration);
    }

    if (methodDeclaration.statements != null) {
        for (Statement s : methodDeclaration.statements) {
            s.traverse(this, methodDeclaration.scope);
        }
    }
    return false;
}