Example usage for org.eclipse.jdt.internal.compiler.ast AbstractMethodDeclaration isConstructor

List of usage examples for org.eclipse.jdt.internal.compiler.ast AbstractMethodDeclaration isConstructor

Introduction

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

Prototype

public boolean isConstructor() 

Source Link

Usage

From source file:com.android.tools.lint.psi.EcjPsiBuilder.java

License:Apache License

@Nullable
private EcjPsiMethod toMethod(EcjPsiClass cls, AbstractMethodDeclaration method) {
    if (method instanceof ConstructorDeclaration) {
        if ((method.bits & ASTNode.IsDefaultConstructor) != 0) {
            return null;
        }/*from   w  w w .  j av  a  2  s .c  o  m*/
    }

    boolean isAnnotation = method instanceof AnnotationMethodDeclaration;
    EcjPsiMethod psiMethod;
    if (!isAnnotation) {
        psiMethod = new EcjPsiMethod(mManager, cls, method);
    } else {
        psiMethod = new EcjPsiAnnotationMethod(mManager, cls, method);
    }

    cls.adoptChild(psiMethod);
    EcjPsiModifierList modifierList = toModifierList(psiMethod, method);
    psiMethod.setModifierList(modifierList);
    if (cls.isInterface()) {
        boolean hasDefaultMethod = method.statements != null && method.statements.length > 0;
        int modifiers = modifierList.getModifiers();
        modifiers |= (hasDefaultMethod ? 0 : Modifier.ABSTRACT) | Modifier.PUBLIC;
        modifiers &= ~(Modifier.PROTECTED | Modifier.PRIVATE);
        modifierList.setModifiers(modifiers);
    } else if (cls.isAnnotationType()) {
        int modifiers = modifierList.getModifiers();
        modifiers |= Modifier.ABSTRACT | Modifier.PUBLIC;
        modifiers &= ~(Modifier.PROTECTED | Modifier.PRIVATE);
        modifierList.setModifiers(modifiers);
    } else if (cls.isEnum() && method.isConstructor()) {
        int modifiers = modifierList.getModifiers();
        modifiers |= Modifier.PRIVATE;
        modifiers &= ~(Modifier.PUBLIC | Modifier.PROTECTED);
        modifierList.setModifiers(modifiers);
    }

    TypeParameter[] typeParameters = method.typeParameters();
    if (typeParameters != null) {
        psiMethod.setTypeParameters(toTypeParameterList(psiMethod, typeParameters));
    }

    if (method instanceof MethodDeclaration && !method.isConstructor()) {
        TypeReference returnType = ((MethodDeclaration) method).returnType;
        if (returnType != null) {
            psiMethod.setReturnTypeElement(toTypeElement(psiMethod, returnType));
        }
    }

    psiMethod.setNameIdentifier(toIdentifier(cls, method.selector,
            toRange(method.sourceStart, method.sourceStart + method.selector.length)));

    psiMethod.setArguments(toParameterList(psiMethod, method.arguments));
    PsiReferenceList psiReferenceList = toTypeReferenceList(psiMethod, method.thrownExceptions,
            Role.THROWS_LIST);
    psiMethod.setThrownExceptions(psiReferenceList);

    PsiCodeBlock body;
    if (method instanceof ConstructorDeclaration) {
        ExplicitConstructorCall constructorCall = ((ConstructorDeclaration) method).constructorCall;
        body = toBlock(psiMethod, method.statements, constructorCall, method.bodyStart - 1, method.bodyEnd + 1);
    } else if (method instanceof MethodDeclaration) {
        boolean semiColonBody = ((method.modifiers & ExtraCompilerModifiers.AccSemicolonBody) != 0);
        if (!method.isAbstract() && !method.isNative() && !semiColonBody) {
            body = toBlock(psiMethod, method.statements, null, method.bodyStart - 1, method.bodyEnd + 1);
        } else {
            body = null;
        }
        if (isAnnotation) {
            //noinspection CastConflictsWithInstanceof
            AnnotationMethodDeclaration amd = (AnnotationMethodDeclaration) method;
            if (amd.defaultValue != null) {
                PsiExpression defaultValue = toExpression(psiMethod, amd.defaultValue);
                ((EcjPsiAnnotationMethod) psiMethod).setValue(defaultValue);
            }
        }
    } else {
        body = toBlock(psiMethod, method.statements, null, method.bodyStart - 1, method.bodyEnd + 1);
    }
    psiMethod.setBody(body);

    return psiMethod;
}

From source file:com.android.tools.lint.psi.EcjPsiManager.java

License:Apache License

@SuppressWarnings("MethodMayBeStatic")
@Nullable//from  w  w w  .j a  va 2s. c  om
public PsiType findType(@NonNull AbstractMethodDeclaration declaration) {
    if (declaration.isConstructor()) {
        MethodBinding binding = ((ConstructorDeclaration) declaration).binding;
        if (binding != null) {
            return findType(binding.declaringClass);
        }
        return null;
    } else if (declaration instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) declaration;
        TypeReference type = methodDeclaration.returnType;
        return findType(type);
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

private boolean selectDeclaration(TypeDeclaration typeDeclaration, char[] assistIdentifier,
        char[] packageName) {

    if (typeDeclaration.name == assistIdentifier) {
        char[] qualifiedSourceName = null;

        TypeDeclaration enclosingType = typeDeclaration;
        while (enclosingType != null) {
            qualifiedSourceName = CharOperation.concat(enclosingType.name, qualifiedSourceName, '.');
            enclosingType = enclosingType.enclosingType;
        }/*from   w ww .  j  av  a 2  s .co  m*/
        char[] uniqueKey = typeDeclaration.binding != null ? typeDeclaration.binding.computeUniqueKey() : null;

        this.requestor.acceptType(packageName, qualifiedSourceName, typeDeclaration.modifiers, true, uniqueKey,
                this.actualSelectionStart, this.actualSelectionEnd);

        this.noProposal = false;
        return true;
    }
    TypeDeclaration[] memberTypes = typeDeclaration.memberTypes;
    for (int i = 0, length = memberTypes == null ? 0 : memberTypes.length; i < length; i++) {
        if (selectDeclaration(memberTypes[i], assistIdentifier, packageName))
            return true;
    }
    FieldDeclaration[] fields = typeDeclaration.fields;
    for (int i = 0, length = fields == null ? 0 : fields.length; i < length; i++) {
        if (fields[i].name == assistIdentifier) {
            char[] qualifiedSourceName = null;

            TypeDeclaration enclosingType = typeDeclaration;
            while (enclosingType != null) {
                qualifiedSourceName = CharOperation.concat(enclosingType.name, qualifiedSourceName, '.');
                enclosingType = enclosingType.enclosingType;
            }
            FieldDeclaration field = fields[i];
            this.requestor.acceptField(packageName, qualifiedSourceName, field.name, true,
                    field.binding != null ? field.binding.computeUniqueKey() : null, this.actualSelectionStart,
                    this.actualSelectionEnd);

            this.noProposal = false;
            return true;
        }
    }
    AbstractMethodDeclaration[] methods = typeDeclaration.methods;
    for (int i = 0, length = methods == null ? 0 : methods.length; i < length; i++) {
        AbstractMethodDeclaration method = methods[i];

        if (method.selector == assistIdentifier) {
            char[] qualifiedSourceName = null;

            TypeDeclaration enclosingType = typeDeclaration;
            while (enclosingType != null) {
                qualifiedSourceName = CharOperation.concat(enclosingType.name, qualifiedSourceName, '.');
                enclosingType = enclosingType.enclosingType;
            }

            this.requestor.acceptMethod(packageName, qualifiedSourceName, null, // SelectionRequestor does not need of declaring type signature for method declaration
                    method.selector, null, // SelectionRequestor does not need of parameters type for method declaration
                    null, // SelectionRequestor does not need of parameters type for method declaration
                    null, // SelectionRequestor does not need of parameters type for method declaration
                    null, // SelectionRequestor does not need of type parameters name for method declaration
                    null, // SelectionRequestor does not need of type parameters bounds for method declaration
                    method.isConstructor(), true,
                    method.binding != null ? method.binding.computeUniqueKey() : null,
                    this.actualSelectionStart, this.actualSelectionEnd);

            this.noProposal = false;
            return true;
        }

        TypeParameter[] methodTypeParameters = method.typeParameters();
        for (int j = 0, length2 = methodTypeParameters == null ? 0
                : methodTypeParameters.length; j < length2; j++) {
            TypeParameter methodTypeParameter = methodTypeParameters[j];

            if (methodTypeParameter.name == assistIdentifier) {
                char[] qualifiedSourceName = null;

                TypeDeclaration enclosingType = typeDeclaration;
                while (enclosingType != null) {
                    qualifiedSourceName = CharOperation.concat(enclosingType.name, qualifiedSourceName, '.');
                    enclosingType = enclosingType.enclosingType;
                }

                this.requestor.acceptMethodTypeParameter(packageName, qualifiedSourceName, method.selector,
                        method.sourceStart, method.sourceEnd, methodTypeParameter.name, true,
                        this.actualSelectionStart, this.actualSelectionEnd);

                this.noProposal = false;
                return true;
            }
        }
    }

    TypeParameter[] typeParameters = typeDeclaration.typeParameters;
    for (int i = 0, length = typeParameters == null ? 0 : typeParameters.length; i < length; i++) {
        TypeParameter typeParameter = typeParameters[i];
        if (typeParameter.name == assistIdentifier) {
            char[] qualifiedSourceName = null;

            TypeDeclaration enclosingType = typeDeclaration;
            while (enclosingType != null) {
                qualifiedSourceName = CharOperation.concat(enclosingType.name, qualifiedSourceName, '.');
                enclosingType = enclosingType.enclosingType;
            }

            this.requestor.acceptTypeParameter(packageName, qualifiedSourceName, typeParameter.name, true,
                    this.actualSelectionStart, this.actualSelectionEnd);

            this.noProposal = false;
            return true;
        }
    }

    return false;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.SourceIndexerRequestor.java

License:Open Source License

private void addDefaultConstructorIfNecessary(TypeInfo typeInfo) {
    boolean hasConstructor = false;

    TypeDeclaration typeDeclaration = typeInfo.node;
    AbstractMethodDeclaration[] methods = typeDeclaration.methods;
    int methodCounter = methods == null ? 0 : methods.length;
    done: for (int i = 0; i < methodCounter; i++) {
        AbstractMethodDeclaration method = methods[i];
        if (method.isConstructor() && !method.isDefaultConstructor()) {
            hasConstructor = true;//w w w. j  a  va 2s  .  co m
            break done;
        }
    }

    if (!hasConstructor) {
        this.indexer.addDefaultConstructorDeclaration(typeInfo.name,
                this.packageName == null ? CharOperation.NO_CHAR : this.packageName, typeInfo.modifiers,
                getMoreExtraFlags(typeInfo.extraFlags));
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.ConstructorLocator.java

License:Open Source License

public SearchMatch newDeclarationMatch(ASTNode reference, IJavaElement element, Binding binding, int accuracy,
        int length, MatchLocator locator) {
    this.match = null;
    int offset = reference.sourceStart;
    if (this.pattern.findReferences) {
        if (reference instanceof TypeDeclaration) {
            TypeDeclaration type = (TypeDeclaration) reference;
            AbstractMethodDeclaration[] methods = type.methods;
            if (methods != null) {
                for (int i = 0, max = methods.length; i < max; i++) {
                    AbstractMethodDeclaration method = methods[i];
                    boolean synthetic = method.isDefaultConstructor() && method.sourceStart < type.bodyStart;
                    this.match = locator.newMethodReferenceMatch(element, binding, accuracy, offset, length,
                            method.isConstructor(), synthetic, method);
                }//  www  .j  a  v  a2  s.  co  m
            }
        } else if (reference instanceof ConstructorDeclaration) {
            ConstructorDeclaration constructor = (ConstructorDeclaration) reference;
            ExplicitConstructorCall call = constructor.constructorCall;
            boolean synthetic = call != null && call.isImplicitSuper();
            this.match = locator.newMethodReferenceMatch(element, binding, accuracy, offset, length,
                    constructor.isConstructor(), synthetic, constructor);
        }
    }
    if (this.match != null) {
        return this.match;
    }
    // super implementation...
    return locator.newDeclarationMatch(element, binding, accuracy, reference.sourceStart, length);
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Creates an IMethod from the given method declaration and type.
 *///from ww w .  j  a  va2 s. c o  m
protected IJavaElement createHandle(AbstractMethodDeclaration method, IJavaElement parent) {
    if (!(parent instanceof IType))
        return parent;

    IType type = (IType) parent;
    Argument[] arguments = method.arguments;
    int argCount = arguments == null ? 0 : arguments.length;
    if (type.isBinary()) {
        // don't cache the methods of the binary type
        // fall thru if its a constructor with a synthetic argument... find it the slower way
        ClassFileReader reader = classFileReader(type);
        if (reader != null) {
            // build arguments names
            boolean firstIsSynthetic = false;
            if (reader.isMember() && method.isConstructor() && !Flags.isStatic(reader.getModifiers())) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=48261
                firstIsSynthetic = true;
                argCount++;
            }
            char[][] argumentTypeNames = new char[argCount][];
            for (int i = 0; i < argCount; i++) {
                char[] typeName = null;
                if (i == 0 && firstIsSynthetic) {
                    typeName = type.getDeclaringType().getFullyQualifiedName().toCharArray();
                } else if (arguments != null) {
                    TypeReference typeRef = arguments[firstIsSynthetic ? i - 1 : i].type;
                    typeName = CharOperation.concatWith(typeRef.getTypeName(), '.');
                    for (int k = 0, dim = typeRef.dimensions(); k < dim; k++)
                        typeName = CharOperation.concat(typeName, new char[] { '[', ']' });
                }
                if (typeName == null) {
                    // invalid type name
                    return null;
                }
                argumentTypeNames[i] = typeName;
            }
            // return binary method
            IMethod binaryMethod = createBinaryMethodHandle(type, method.selector, argumentTypeNames);
            if (binaryMethod == null) {
                // when first attempt fails, try with similar matches if any...
                PossibleMatch similarMatch = this.currentPossibleMatch.getSimilarMatch();
                while (similarMatch != null) {
                    type = ((ClassFile) similarMatch.openable).getType();
                    binaryMethod = createBinaryMethodHandle(type, method.selector, argumentTypeNames);
                    if (binaryMethod != null) {
                        return binaryMethod;
                    }
                    similarMatch = similarMatch.getSimilarMatch();
                }
            }
            return binaryMethod;
        }
        if (BasicSearchEngine.VERBOSE) {
            System.out.println("Not able to createHandle for the method " + //$NON-NLS-1$
                    CharOperation.charToString(method.selector) + " May miss some results"); //$NON-NLS-1$
        }
        return null;
    }

    String[] parameterTypeSignatures = new String[argCount];
    if (arguments != null) {
        for (int i = 0; i < argCount; i++) {
            TypeReference typeRef = arguments[i].type;
            char[] typeName = CharOperation.concatWith(typeRef.getParameterizedTypeName(), '.');
            parameterTypeSignatures[i] = Signature.createTypeSignature(typeName, false);
        }
    }

    return createMethodHandle(type, new String(method.selector), parameterTypeSignatures);
}

From source file:com.google.gwt.dev.javac.JavaSourceParser.java

License:Open Source License

/**
 * Find all methods which have the requested name.
 * /*from w  w  w  .ja v a  2 s  . c o m*/
 * <p>{@code <clinit>} is not supported.
 * @param type JDT type declaration
 * @param name name of methods to find
 * @return list of matching methods
 */
private static List<AbstractMethodDeclaration> findNamedMethods(TypeDeclaration type, String name) {
    List<AbstractMethodDeclaration> matching = new ArrayList<AbstractMethodDeclaration>();
    boolean isCtor = "<init>".equals(name);
    char[] nameArray = name.toCharArray();
    for (AbstractMethodDeclaration method : type.methods) {
        if ((isCtor && method.isConstructor()) || (!isCtor && !method.isConstructor() && !method.isClinit()
                && Arrays.equals(method.selector, nameArray))) {
            matching.add(method);
        }
    }
    return matching;
}

From source file:com.google.gwt.dev.jjs.impl.GwtAstBuilder.java

License:Apache License

private void createMethod(AbstractMethodDeclaration x) {
    if (x instanceof Clinit) {
        return;//from  ww w .ja  v  a  2  s  .c o  m
    }
    SourceInfo info = makeSourceInfo(x);
    MethodBinding b = x.binding;
    ReferenceBinding declaringClass = (ReferenceBinding) b.declaringClass.erasure();
    Set<String> alreadyNamedVariables = Sets.newHashSet();
    JDeclaredType enclosingType = (JDeclaredType) typeMap.get(declaringClass);
    assert !enclosingType.isExternal();
    JMethod method;
    boolean isNested = JdtUtil.isInnerClass(declaringClass);
    if (x.isConstructor()) {
        method = new JConstructor(info, (JClassType) enclosingType);
        if (x.isDefaultConstructor()) {
            ((JConstructor) method).setDefaultConstructor();
        }
        if (x.binding.declaringClass.isEnum()) {
            // Enums have hidden arguments for name and value
            method.addParam(new JParameter(info, "enum$name", typeMap.get(x.scope.getJavaLangString()), true,
                    false, method));
            method.addParam(new JParameter(info, "enum$ordinal", JPrimitiveType.INT, true, false, method));
        }
        // add synthetic args for outer this
        if (isNested) {
            NestedTypeBinding nestedBinding = (NestedTypeBinding) declaringClass;
            if (nestedBinding.enclosingInstances != null) {
                for (int i = 0; i < nestedBinding.enclosingInstances.length; ++i) {
                    SyntheticArgumentBinding arg = nestedBinding.enclosingInstances[i];
                    String argName = String.valueOf(arg.name);
                    if (alreadyNamedVariables.contains(argName)) {
                        argName += "_" + i;
                    }
                    createParameter(info, arg, argName, method);
                    alreadyNamedVariables.add(argName);
                }
            }
        }
    } else {
        method = new JMethod(info, intern(b.selector), enclosingType, typeMap.get(b.returnType), b.isAbstract(),
                b.isStatic(), b.isFinal(), AccessModifier.fromMethodBinding(b));
    }

    // User args.
    createParameters(method, x);

    if (x.isConstructor()) {
        if (isNested) {
            // add synthetic args for locals
            NestedTypeBinding nestedBinding = (NestedTypeBinding) declaringClass;
            // add synthetic args for outer this and locals
            if (nestedBinding.outerLocalVariables != null) {
                for (int i = 0; i < nestedBinding.outerLocalVariables.length; ++i) {
                    SyntheticArgumentBinding arg = nestedBinding.outerLocalVariables[i];
                    String argName = String.valueOf(arg.name);
                    if (alreadyNamedVariables.contains(argName)) {
                        argName += "_" + i;
                    }
                    createParameter(info, arg, argName, method);
                    alreadyNamedVariables.add(argName);
                }
            }
        }
    }

    mapExceptions(method, b);

    if (b.isSynthetic()) {
        method.setSynthetic();
    }

    if (b.isDefaultMethod()) {
        method.setDefaultMethod();
    }

    enclosingType.addMethod(method);
    JsInteropUtil.maybeSetJsinteropMethodProperties(x, method);
    processAnnotations(x, method);
    typeMap.setMethod(b, method);
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementNotifier.java

License:Open Source License

protected void notifySourceElementRequestor(AbstractMethodDeclaration methodDeclaration) {

    // range check
    boolean isInRange = initialPosition <= methodDeclaration.declarationSourceStart
            && eofPosition >= methodDeclaration.declarationSourceEnd;

    if (methodDeclaration.isClinit()) {
        this.visitIfNeeded(methodDeclaration);
        return;//ww w.  j  a v  a  2  s.c o m
    }

    if (methodDeclaration.isDefaultConstructor()) {
        if (reportReferenceInfo) {
            ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) methodDeclaration;
            ExplicitConstructorCall constructorCall = constructorDeclaration.constructorCall;
            if (constructorCall != null) {
                switch (constructorCall.accessMode) {
                case ExplicitConstructorCall.This:
                    requestor.acceptConstructorReference(typeNames[nestedTypeIndex - 1],
                            constructorCall.arguments == null ? 0 : constructorCall.arguments.length,
                            constructorCall.sourceStart);
                    break;
                case ExplicitConstructorCall.Super:
                case ExplicitConstructorCall.ImplicitSuper:
                    requestor.acceptConstructorReference(superTypeNames[nestedTypeIndex - 1],
                            constructorCall.arguments == null ? 0 : constructorCall.arguments.length,
                            constructorCall.sourceStart);
                    break;
                }
            }
        }
        return;
    }
    char[][] argumentTypes = null;
    char[][] argumentNames = null;
    boolean isVarArgs = false;
    Argument[] arguments = methodDeclaration.arguments;
    if (arguments != null) {
        char[][][] argumentTypesAndNames = this.getArguments(arguments);
        argumentTypes = argumentTypesAndNames[0];
        argumentNames = argumentTypesAndNames[1];

        isVarArgs = arguments[arguments.length - 1].isVarArgs();
    }
    char[][] thrownExceptionTypes = getThrownExceptions(methodDeclaration);
    // by default no selector end position
    int selectorSourceEnd = -1;
    if (methodDeclaration.isConstructor()) {
        selectorSourceEnd = this.sourceEnds.get(methodDeclaration);
        if (isInRange) {
            int currentModifiers = methodDeclaration.modifiers;
            if (isVarArgs)
                currentModifiers |= ClassFileConstants.AccVarargs;

            // remember deprecation so as to not lose it below
            boolean deprecated = (currentModifiers & ClassFileConstants.AccDeprecated) != 0
                    || hasDeprecatedAnnotation(methodDeclaration.annotations);

            ISourceElementRequestor.MethodInfo methodInfo = new ISourceElementRequestor.MethodInfo();
            methodInfo.isConstructor = true;
            methodInfo.declarationStart = methodDeclaration.declarationSourceStart;
            methodInfo.modifiers = deprecated
                    ? (currentModifiers & ExtraCompilerModifiers.AccJustFlag) | ClassFileConstants.AccDeprecated
                    : currentModifiers & ExtraCompilerModifiers.AccJustFlag;
            methodInfo.name = methodDeclaration.selector;
            methodInfo.nameSourceStart = methodDeclaration.sourceStart;
            methodInfo.nameSourceEnd = selectorSourceEnd;
            methodInfo.parameterTypes = argumentTypes;
            methodInfo.parameterNames = argumentNames;
            methodInfo.exceptionTypes = thrownExceptionTypes;
            methodInfo.typeParameters = getTypeParameterInfos(methodDeclaration.typeParameters());
            methodInfo.categories = (char[][]) this.nodesToCategories.get(methodDeclaration);
            methodInfo.annotations = methodDeclaration.annotations;
            methodInfo.node = methodDeclaration;
            requestor.enterConstructor(methodInfo);
        }
        if (reportReferenceInfo) {
            ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) methodDeclaration;
            ExplicitConstructorCall constructorCall = constructorDeclaration.constructorCall;
            if (constructorCall != null) {
                switch (constructorCall.accessMode) {
                case ExplicitConstructorCall.This:
                    requestor.acceptConstructorReference(typeNames[nestedTypeIndex - 1],
                            constructorCall.arguments == null ? 0 : constructorCall.arguments.length,
                            constructorCall.sourceStart);
                    break;
                case ExplicitConstructorCall.Super:
                case ExplicitConstructorCall.ImplicitSuper:
                    requestor.acceptConstructorReference(superTypeNames[nestedTypeIndex - 1],
                            constructorCall.arguments == null ? 0 : constructorCall.arguments.length,
                            constructorCall.sourceStart);
                    break;
                }
            }
        }
        this.visitIfNeeded(methodDeclaration);
        if (isInRange) {
            requestor.exitConstructor(methodDeclaration.declarationSourceEnd);
        }
        return;
    }
    selectorSourceEnd = this.sourceEnds.get(methodDeclaration);

    // AspectJ Change Begin
    // recreate source locations for Pointcuts and Advice
    org.aspectj.org.eclipse.jdt.internal.compiler.ast.MethodDeclaration ajmDec = new org.aspectj.org.eclipse.jdt.internal.compiler.ast.MethodDeclaration(
            new org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult(
                    methodDeclaration.compilationResult.fileName, methodDeclaration.compilationResult.unitIndex,
                    methodDeclaration.compilationResult.totalUnitsKnown, 500));
    if (ajmDec instanceof PointcutDeclaration) {
        selectorSourceEnd = methodDeclaration.sourceStart + methodDeclaration.selector.length - 1;
    }
    if (ajmDec instanceof AdviceDeclaration) {
        selectorSourceEnd = methodDeclaration.sourceStart + ((AdviceDeclaration) ajmDec).kind.getName().length()
                - 1;
    }
    // AspectJ Change End

    if (isInRange) {
        int currentModifiers = methodDeclaration.modifiers;
        if (isVarArgs)
            currentModifiers |= ClassFileConstants.AccVarargs;

        // remember deprecation so as to not lose it below
        boolean deprecated = (currentModifiers & ClassFileConstants.AccDeprecated) != 0
                || hasDeprecatedAnnotation(methodDeclaration.annotations);

        TypeReference returnType = methodDeclaration instanceof MethodDeclaration
                ? ((MethodDeclaration) methodDeclaration).returnType
                : null;
        ISourceElementRequestor.MethodInfo methodInfo = new ISourceElementRequestor.MethodInfo();
        methodInfo.isAnnotation = methodDeclaration instanceof AnnotationMethodDeclaration;
        methodInfo.declarationStart = methodDeclaration.declarationSourceStart;
        methodInfo.modifiers = deprecated
                ? (currentModifiers & ExtraCompilerModifiers.AccJustFlag) | ClassFileConstants.AccDeprecated
                : currentModifiers & ExtraCompilerModifiers.AccJustFlag;
        methodInfo.returnType = returnType == null ? null
                : CharOperation.concatWith(returnType.getParameterizedTypeName(), '.');
        methodInfo.name = methodDeclaration.selector;
        methodInfo.nameSourceStart = methodDeclaration.sourceStart;
        methodInfo.nameSourceEnd = selectorSourceEnd;
        methodInfo.parameterTypes = argumentTypes;
        methodInfo.parameterNames = argumentNames;
        methodInfo.exceptionTypes = thrownExceptionTypes;
        methodInfo.typeParameters = getTypeParameterInfos(methodDeclaration.typeParameters());
        methodInfo.categories = (char[][]) this.nodesToCategories.get(methodDeclaration);
        methodInfo.annotations = methodDeclaration.annotations;
        methodInfo.node = methodDeclaration;
        requestor.enterMethod(methodInfo);
    }

    this.visitIfNeeded(methodDeclaration);

    if (isInRange) {
        if (methodDeclaration instanceof AnnotationMethodDeclaration) {
            AnnotationMethodDeclaration annotationMethodDeclaration = (AnnotationMethodDeclaration) methodDeclaration;
            Expression expression = annotationMethodDeclaration.defaultValue;
            if (expression != null) {
                requestor.exitMethod(methodDeclaration.declarationSourceEnd, expression);
                return;
            }
        }
        requestor.exitMethod(methodDeclaration.declarationSourceEnd,
                (org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression) null);
    }
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public ASTNode convert(boolean isInterface,
        org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration methodDeclaration) {
    checkCanceled();/*from  ww  w.java  2s .c  o  m*/
    if (methodDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) {
        return convert((org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) methodDeclaration);
    }
    MethodDeclaration methodDecl = new MethodDeclaration(this.ast);
    setModifiers(methodDecl, methodDeclaration);
    boolean isConstructor = methodDeclaration.isConstructor();
    methodDecl.setConstructor(isConstructor);
    final SimpleName methodName = new SimpleName(this.ast);
    methodName.internalSetIdentifier(new String(methodDeclaration.selector));
    int start = methodDeclaration.sourceStart;
    // GROOVY start
    // why does this do what it does?
    /* old {
    int end = retrieveIdentifierEndPosition(start, methodDeclaration.sourceEnd);
    } new */
    int end = (scannerAvailable(methodDeclaration.scope)
            ? retrieveIdentifierEndPosition(start, methodDeclaration.sourceEnd)
            : methodDeclaration.sourceEnd);
    // GROOVY end
    methodName.setSourceRange(start, end - start + 1);
    methodDecl.setName(methodName);
    org.eclipse.jdt.internal.compiler.ast.TypeReference[] thrownExceptions = methodDeclaration.thrownExceptions;
    int methodHeaderEnd = methodDeclaration.sourceEnd;
    int thrownExceptionsLength = thrownExceptions == null ? 0 : thrownExceptions.length;
    if (thrownExceptionsLength > 0) {
        Name thrownException;
        int i = 0;
        do {
            thrownException = convert(thrownExceptions[i++]);
            methodDecl.thrownExceptions().add(thrownException);
        } while (i < thrownExceptionsLength);
        methodHeaderEnd = thrownException.getStartPosition() + thrownException.getLength();
    }
    org.eclipse.jdt.internal.compiler.ast.Argument[] parameters = methodDeclaration.arguments;
    int parametersLength = parameters == null ? 0 : parameters.length;
    if (parametersLength > 0) {
        SingleVariableDeclaration parameter;
        int i = 0;
        do {
            // GROOVY start
            // make sure the scope is available just in case it is necessary for varargs
            // new code
            BlockScope origScope = null;
            if (parameters[i].binding != null) {
                origScope = parameters[i].binding.declaringScope;
                parameters[i].binding.declaringScope = methodDeclaration.scope;
            }
            // GROOVY end

            parameter = convert(parameters[i++]);

            // GROOVY start
            // unset the scope
            // new code
            if (parameters[i - 1].binding != null) {
                parameters[i - 1].binding.declaringScope = origScope;
            }
            // GROOVY end
            methodDecl.parameters().add(parameter);
        } while (i < parametersLength);
        if (thrownExceptionsLength == 0) {
            methodHeaderEnd = parameter.getStartPosition() + parameter.getLength();
        }
    }
    org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall explicitConstructorCall = null;
    if (isConstructor) {
        if (isInterface) {
            // interface cannot have a constructor
            methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED);
        }
        org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration constructorDeclaration = (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) methodDeclaration;
        explicitConstructorCall = constructorDeclaration.constructorCall;
        switch (this.ast.apiLevel) {
        case AST.JLS2_INTERNAL:
            // set the return type to VOID
            PrimitiveType returnType = new PrimitiveType(this.ast);
            returnType.setPrimitiveTypeCode(PrimitiveType.VOID);
            returnType.setSourceRange(methodDeclaration.sourceStart, 0);
            methodDecl.internalSetReturnType(returnType);
            break;
        default:
            methodDecl.setReturnType2(null);
        }
    } else if (methodDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) {
        org.eclipse.jdt.internal.compiler.ast.MethodDeclaration method = (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) methodDeclaration;
        org.eclipse.jdt.internal.compiler.ast.TypeReference typeReference = method.returnType;
        if (typeReference != null) {
            Type returnType = convertType(typeReference);
            // get the positions of the right parenthesis
            int rightParenthesisPosition = retrieveEndOfRightParenthesisPosition(end, method.bodyEnd);
            int extraDimensions = retrieveExtraDimension(rightParenthesisPosition, method.bodyEnd);
            methodDecl.setExtraDimensions(extraDimensions);
            setTypeForMethodDeclaration(methodDecl, returnType, extraDimensions);
        } else {
            // no return type for a method that is not a constructor
            methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED);
            switch (this.ast.apiLevel) {
            case AST.JLS2_INTERNAL:
                break;
            default:
                methodDecl.setReturnType2(null);
            }
        }
    }
    int declarationSourceStart = methodDeclaration.declarationSourceStart;
    int bodyEnd = methodDeclaration.bodyEnd;
    methodDecl.setSourceRange(declarationSourceStart, bodyEnd - declarationSourceStart + 1);
    int declarationSourceEnd = methodDeclaration.declarationSourceEnd;
    int rightBraceOrSemiColonPositionStart = bodyEnd == declarationSourceEnd ? bodyEnd : bodyEnd + 1;
    int closingPosition = retrieveRightBraceOrSemiColonPosition(rightBraceOrSemiColonPositionStart,
            declarationSourceEnd);
    if (closingPosition != -1) {
        int startPosition = methodDecl.getStartPosition();
        methodDecl.setSourceRange(startPosition, closingPosition - startPosition + 1);

        org.eclipse.jdt.internal.compiler.ast.Statement[] statements = methodDeclaration.statements;

        start = retrieveStartBlockPosition(methodHeaderEnd, methodDeclaration.bodyStart);
        if (start == -1)
            start = methodDeclaration.bodyStart; // use recovery position for body start
        end = retrieveRightBrace(methodDeclaration.bodyEnd, declarationSourceEnd);
        Block block = null;
        if (start != -1 && end != -1) {
            /*
             * start or end can be equal to -1 if we have an interface's method.
             */
            block = new Block(this.ast);
            block.setSourceRange(start, closingPosition - start + 1);
            methodDecl.setBody(block);
        }
        if (block != null && (statements != null || explicitConstructorCall != null)) {
            if (explicitConstructorCall != null
                    && explicitConstructorCall.accessMode != org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall.ImplicitSuper) {
                block.statements().add(convert(explicitConstructorCall));
            }
            int statementsLength = statements == null ? 0 : statements.length;
            for (int i = 0; i < statementsLength; i++) {
                if (statements[i] instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) {
                    checkAndAddMultipleLocalDeclaration(statements, i, block.statements());
                } else {
                    final Statement statement = convert(statements[i]);
                    if (statement != null) {
                        block.statements().add(statement);
                    }
                }
            }
        }
        if (block != null && (Modifier.isAbstract(methodDecl.getModifiers())
                || Modifier.isNative(methodDecl.getModifiers()) || isInterface)) {
            methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED);
        }
    } else {
        // syntax error in this method declaration
        methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED);
        if (!methodDeclaration.isNative() && !methodDeclaration.isAbstract()) {
            start = retrieveStartBlockPosition(methodHeaderEnd, bodyEnd);
            if (start == -1)
                start = methodDeclaration.bodyStart; // use recovery position for body start
            end = methodDeclaration.bodyEnd;
            // try to get the best end position
            CategorizedProblem[] problems = methodDeclaration.compilationResult().problems;
            if (problems != null) {
                for (int i = 0, max = methodDeclaration.compilationResult().problemCount; i < max; i++) {
                    CategorizedProblem currentProblem = problems[i];
                    if (currentProblem.getSourceStart() == start
                            && currentProblem.getID() == IProblem.ParsingErrorInsertToComplete) {
                        end = currentProblem.getSourceEnd();
                        break;
                    }
                }
            }
            int startPosition = methodDecl.getStartPosition();
            methodDecl.setSourceRange(startPosition, end - startPosition + 1);
            if (start != -1 && end != -1) {
                /*
                 * start or end can be equal to -1 if we have an interface's method.
                 */
                Block block = new Block(this.ast);
                block.setSourceRange(start, end - start + 1);
                methodDecl.setBody(block);
            }
        }
    }

    org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParameters = methodDeclaration.typeParameters();
    if (typeParameters != null) {
        switch (this.ast.apiLevel) {
        case AST.JLS2_INTERNAL:
            methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED);
            break;
        default:
            for (int i = 0, max = typeParameters.length; i < max; i++) {
                methodDecl.typeParameters().add(convert(typeParameters[i]));
            }
        }
    }

    // The javadoc comment is now got from list store in compilation unit declaration
    convert(methodDeclaration.javadoc, methodDecl);
    if (this.resolveBindings) {
        recordNodes(methodDecl, methodDeclaration);
        recordNodes(methodName, methodDeclaration);
        methodDecl.resolveBinding();
    }
    return methodDecl;
}