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

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

Introduction

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

Prototype

public boolean isDefaultConstructor() 

Source Link

Usage

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

License:Open Source License

private ASTNode parseBlockStatements(TypeDeclaration type, CompilationUnitDeclaration unit, int position) {
    //members//  w ww. j  a  v  a  2s. co  m
    TypeDeclaration[] memberTypes = type.memberTypes;
    if (memberTypes != null) {
        int length = memberTypes.length;
        for (int i = 0; i < length; i++) {
            TypeDeclaration memberType = memberTypes[i];
            if (memberType.bodyStart > position)
                continue;
            if (memberType.declarationSourceEnd >= position) {
                return parseBlockStatements(memberType, unit, position);
            }
        }
    }
    //methods
    AbstractMethodDeclaration[] methods = type.methods;
    if (methods != null) {
        int length = methods.length;
        for (int i = 0; i < length; i++) {
            AbstractMethodDeclaration method = methods[i];
            if (method.bodyStart > position + 1)
                continue;

            if (method.isDefaultConstructor())
                continue;

            if (method.declarationSourceEnd >= position) {

                getParser().parseBlockStatements(method, unit);
                return method;
            }
        }
    }
    //initializers
    FieldDeclaration[] fields = type.fields;
    if (fields != null) {
        int length = fields.length;
        for (int i = 0; i < length; i++) {
            FieldDeclaration field = fields[i];
            if (field.sourceStart > position)
                continue;
            if (field.declarationSourceEnd >= position) {
                if (field instanceof Initializer) {
                    getParser().parseBlockStatements((Initializer) field, type, unit);
                }
                return field;
            }
        }
    }
    return null;
}

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;//from w  ww  .  j  a v a2 s  . c  om
            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);
                }//  w w w.j a v a 2  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.ConstructorLocator.java

License:Open Source License

protected int resolveLevel(TypeDeclaration type) {
    // find default constructor
    AbstractMethodDeclaration[] methods = type.methods;
    if (methods != null) {
        for (int i = 0, length = methods.length; i < length; i++) {
            AbstractMethodDeclaration method = methods[i];
            if (method.isDefaultConstructor() && method.sourceStart < type.bodyStart) // if synthetic
                return resolveLevel((ConstructorDeclaration) method, false);
        }/*from   w  w  w . j  a va  2  s .c  o m*/
    }
    return IMPOSSIBLE_MATCH;
}

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

License:Open Source License

/**
 * Visit the given method declaration and report the nodes that match exactly the
 * search pattern (i.e. the ones in the matching nodes set)
 * Note that the method declaration has already been checked.
 *///from  w w w  .java2 s. c  o m
protected void reportMatching(AbstractMethodDeclaration method, TypeDeclaration type, IJavaElement parent,
        int accuracy, boolean typeInHierarchy, MatchingNodeSet nodeSet) throws CoreException {
    IJavaElement enclosingElement = null;

    // report method declaration itself
    if (accuracy > -1) {
        enclosingElement = createHandle(method, parent);
        if (enclosingElement != null) { // skip if unable to find method
            // compute source positions of the selector
            Scanner scanner = this.parser.scanner;
            int nameSourceStart = method.sourceStart;
            scanner.setSource(this.currentPossibleMatch.getContents());
            scanner.resetTo(nameSourceStart, method.sourceEnd);
            try {
                scanner.getNextToken();
            } catch (InvalidInputException e) {
                // ignore
            }
            if (encloses(enclosingElement)) {
                SearchMatch match = null;
                if (method.isDefaultConstructor()) {
                    // Use type for match associated element as default constructor does not exist in source
                    int offset = type.sourceStart;
                    match = this.patternLocator.newDeclarationMatch(type, parent, type.binding, accuracy,
                            type.sourceEnd - offset + 1, this);
                } else {
                    int length = scanner.currentPosition - nameSourceStart;
                    match = this.patternLocator.newDeclarationMatch(method, enclosingElement, method.binding,
                            accuracy, length, this);
                }
                if (match != null) {
                    report(match);
                }
            }
        }
    }

    // handle nodes for the local type first
    if ((method.bits & ASTNode.HasLocalType) != 0) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        // Traverse method declaration to report matches both in local types declaration
        // and in local variables declaration
        ASTNode[] nodes = typeInHierarchy
                ? nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd)
                : null;
        boolean report = (this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0
                && encloses(enclosingElement);
        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(enclosingElement,
                report ? nodes : null, nodeSet, this);
        try {
            method.traverse(declarationVisitor, (ClassScope) null);
        } catch (WrappedCoreException e) {
            throw e.coreException;
        }
        // Report all nodes and remove them
        if (nodes != null) {
            int length = nodes.length;
            for (int i = 0; i < length; i++) {
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                if (report && level != null) {
                    this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                            declarationVisitor.getLocalElement(i), declarationVisitor.getOtherElements(i),
                            method.binding, level.intValue(), this);
                }
            }
        }
    }

    // report the type parameters
    TypeParameter[] typeParameters = method.typeParameters();
    if (typeParameters != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(typeParameters, enclosingElement, parent, method.binding, nodeSet);
        }
    }

    // report annotations
    if (method.annotations != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(method.annotations, enclosingElement, null, method.binding, nodeSet, true, true);
        }
    }

    // references in this method
    if (typeInHierarchy) {
        ASTNode[] nodes = nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd);
        if (nodes != null) {
            if ((this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0) {
                if (enclosingElement == null) {
                    enclosingElement = createHandle(method, parent);
                }
                if (encloses(enclosingElement)) {
                    if (this.pattern.mustResolve) {
                        // Visit only if the pattern must resolve
                        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(
                                enclosingElement, nodes, nodeSet, this);
                        method.traverse(declarationVisitor, (ClassScope) null);
                        int length = nodes.length;
                        for (int i = 0; i < length; i++) {
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                                        declarationVisitor.getLocalElement(i),
                                        declarationVisitor.getOtherElements(i), method.binding,
                                        level.intValue(), this);
                            }
                        }
                    } else {
                        for (int i = 0, l = nodes.length; i < l; i++) {
                            ASTNode node = nodes[i];
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                        method.binding, level.intValue(), this);
                            }
                        }
                    }
                    return;
                }
            }
            // Remove all remaining nodes
            for (int i = 0, l = nodes.length; i < l; i++) {
                nodeSet.matchingNodes.removeKey(nodes[i]);
            }
        }
    }
}

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

License:Open Source License

/**
 * Parses the member bodies in the given type.
 * @param type TypeDeclaration//  ww w .  j  a  v a 2 s .  c o m
 * @param unit CompilationUnitDeclaration
 */
protected void parseBodies(TypeDeclaration type, CompilationUnitDeclaration unit) {
    FieldDeclaration[] fields = type.fields;
    if (fields != null) {
        for (int i = 0; i < fields.length; i++) {
            FieldDeclaration field = fields[i];
            if (field instanceof Initializer)
                this.parse((Initializer) field, type, unit);
            field.traverse(this.localDeclarationVisitor, null);
        }
    }

    AbstractMethodDeclaration[] methods = type.methods;
    if (methods != null) {
        for (int i = 0; i < methods.length; i++) {
            AbstractMethodDeclaration method = methods[i];
            if (method.sourceStart >= type.bodyStart) { // if not synthetic
                if (method instanceof MethodDeclaration) {
                    MethodDeclaration methodDeclaration = (MethodDeclaration) method;
                    this.parse(methodDeclaration, unit);
                    methodDeclaration.traverse(this.localDeclarationVisitor, (ClassScope) null);
                } else if (method instanceof ConstructorDeclaration) {
                    ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) method;
                    this.parse(constructorDeclaration, unit, false);
                    constructorDeclaration.traverse(this.localDeclarationVisitor, (ClassScope) null);
                }
            } else if (method.isDefaultConstructor()) {
                method.parseStatements(this, unit);
            }
        }
    }

    TypeDeclaration[] memberTypes = type.memberTypes;
    if (memberTypes != null) {
        for (int i = 0; i < memberTypes.length; i++) {
            TypeDeclaration memberType = memberTypes[i];
            this.parseBodies(memberType, unit);
            memberType.traverse(this.localDeclarationVisitor, (ClassScope) null);
        }
    }
}

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   w  ww . j a va2 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;/*  w  w w .  j  a  va2  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.che.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
* Visit the given method declaration and report the nodes that match exactly the
* search pattern (i.e. the ones in the matching nodes set)
* Note that the method declaration has already been checked.
*//* w w  w .  j av a2s  .  co  m*/
protected void reportMatching(AbstractMethodDeclaration method, TypeDeclaration type, IJavaElement parent,
        int accuracy, boolean typeInHierarchy, MatchingNodeSet nodeSet) throws CoreException {
    IJavaElement enclosingElement = null;

    // report method declaration itself
    if (accuracy > -1) {
        enclosingElement = createHandle(method, parent);
        if (enclosingElement != null) { // skip if unable to find method
            // compute source positions of the selector
            Scanner scanner = this.parser.scanner;
            int nameSourceStart = method.sourceStart;
            scanner.setSource(this.currentPossibleMatch.getContents());
            scanner.resetTo(nameSourceStart, method.sourceEnd);
            try {
                scanner.getNextToken();
            } catch (InvalidInputException e) {
                // ignore
            }
            if (encloses(enclosingElement)) {
                SearchMatch match = null;
                if (method.isDefaultConstructor()) {
                    // Use type for match associated element as default constructor does not exist in source
                    int offset = type.sourceStart;
                    match = this.patternLocator.newDeclarationMatch(type, parent, type.binding, accuracy,
                            type.sourceEnd - offset + 1, this);
                } else {
                    int length = scanner.currentPosition - nameSourceStart;
                    match = this.patternLocator.newDeclarationMatch(method, enclosingElement, method.binding,
                            accuracy, length, this);
                }
                if (match != null) {
                    report(match);
                }
            }
        }
    }

    // handle nodes for the local type first
    if ((method.bits & ASTNode.HasLocalType) != 0) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        // Traverse method declaration to report matches both in local types declaration
        // and in local variables declaration
        ASTNode[] nodes = typeInHierarchy
                ? nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd)
                : null;
        boolean report = (this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0
                && encloses(enclosingElement);
        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(enclosingElement,
                report ? nodes : null, nodeSet, this, typeInHierarchy);
        try {
            method.traverse(declarationVisitor, (ClassScope) null);
        } catch (WrappedCoreException e) {
            throw e.coreException;
        }
        // Report all nodes and remove them
        if (nodes != null) {
            int length = nodes.length;
            for (int i = 0; i < length; i++) {
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                if (report && level != null) {
                    this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                            declarationVisitor.getLocalElement(i), declarationVisitor.getOtherElements(i),
                            method.binding, level.intValue(), this);
                }
            }
        }
    }

    // report the type parameters
    TypeParameter[] typeParameters = method.typeParameters();
    if (typeParameters != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(typeParameters, enclosingElement, parent, method.binding, nodeSet);
        }
    }

    // report annotations
    if (method.annotations != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(method.annotations, enclosingElement, null, method.binding, nodeSet, true, true);
        }
    }

    // references in this method
    if (typeInHierarchy) {
        ASTNode[] nodes = nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd);
        if (nodes != null) {
            if ((this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0) {
                if (enclosingElement == null) {
                    enclosingElement = createHandle(method, parent);
                }
                if (encloses(enclosingElement)) {
                    if (this.pattern.mustResolve) {
                        // Visit only if the pattern must resolve
                        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(
                                enclosingElement, nodes, nodeSet, this, typeInHierarchy);
                        method.traverse(declarationVisitor, (ClassScope) null);
                        int length = nodes.length;
                        for (int i = 0; i < length; i++) {
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                                        declarationVisitor.getLocalElement(i),
                                        declarationVisitor.getOtherElements(i), method.binding,
                                        level.intValue(), this);
                            }
                        }
                    } else {
                        for (int i = 0, l = nodes.length; i < l; i++) {
                            ASTNode node = nodes[i];
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                        method.binding, level.intValue(), this);
                            }
                        }
                    }
                    return;
                }
            }
            // Remove all remaining nodes
            for (int i = 0, l = nodes.length; i < l; i++) {
                nodeSet.matchingNodes.removeKey(nodes[i]);
            }
        }
    }
}

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

License:Open Source License

protected void buildBodyDeclarations(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration typeDeclaration,
        AbstractTypeDeclaration typeDecl, boolean isInterface) {
    // add body declaration in the lexical order
    org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] members = typeDeclaration.memberTypes;
    org.eclipse.jdt.internal.compiler.ast.FieldDeclaration[] fields = typeDeclaration.fields;
    org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration[] methods = typeDeclaration.methods;

    int fieldsLength = fields == null ? 0 : fields.length;
    int methodsLength = methods == null ? 0 : methods.length;
    int membersLength = members == null ? 0 : members.length;
    int fieldsIndex = 0;
    int methodsIndex = 0;
    int membersIndex = 0;

    while ((fieldsIndex < fieldsLength) || (membersIndex < membersLength) || (methodsIndex < methodsLength)) {
        org.eclipse.jdt.internal.compiler.ast.FieldDeclaration nextFieldDeclaration = null;
        org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration nextMethodDeclaration = null;
        org.eclipse.jdt.internal.compiler.ast.TypeDeclaration nextMemberDeclaration = null;

        int position = Integer.MAX_VALUE;
        int nextDeclarationType = -1;
        if (fieldsIndex < fieldsLength) {
            nextFieldDeclaration = fields[fieldsIndex];
            if (nextFieldDeclaration.declarationSourceStart < position) {
                position = nextFieldDeclaration.declarationSourceStart;
                nextDeclarationType = 0; // FIELD
            }/*from   w  w w . j  a v  a 2 s.c o m*/
        }
        if (methodsIndex < methodsLength) {
            nextMethodDeclaration = methods[methodsIndex];
            if (nextMethodDeclaration.declarationSourceStart < position) {
                position = nextMethodDeclaration.declarationSourceStart;
                nextDeclarationType = 1; // METHOD
            }
        }
        if (membersIndex < membersLength) {
            nextMemberDeclaration = members[membersIndex];
            if (nextMemberDeclaration.declarationSourceStart < position) {
                position = nextMemberDeclaration.declarationSourceStart;
                nextDeclarationType = 2; // MEMBER
            }
        }
        switch (nextDeclarationType) {
        case 0:
            if (nextFieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
                typeDecl.bodyDeclarations().add(convert(nextFieldDeclaration));
            } else {
                checkAndAddMultipleFieldDeclaration(fields, fieldsIndex, typeDecl.bodyDeclarations());
            }
            fieldsIndex++;
            break;
        case 1:
            methodsIndex++;
            if (!nextMethodDeclaration.isDefaultConstructor() && !nextMethodDeclaration.isClinit()) {
                // GROOVY start - a little ugly, but allows the conversion of the method declaration
                // to know if it is occurring within a pure java type or not
                boolean originalValue = this.scannerUsable;
                try {
                    this.scannerUsable = typeDeclaration.isScannerUsableOnThisDeclaration();
                    // GROOVY end
                    typeDecl.bodyDeclarations().add(convert(isInterface, nextMethodDeclaration));
                    // GROOVY start
                } finally {
                    this.scannerUsable = originalValue;
                }
                // GROOVY end
            }
            break;
        case 2:
            membersIndex++;
            ASTNode node = convert(nextMemberDeclaration);
            if (node == null) {
                typeDecl.setFlags(typeDecl.getFlags() | ASTNode.MALFORMED);
            } else {
                typeDecl.bodyDeclarations().add(node);
            }
        }
    }
    // Convert javadoc
    convert(typeDeclaration.javadoc, typeDecl);
}