Example usage for org.eclipse.jdt.internal.compiler.ast TypeDeclaration declarationOf

List of usage examples for org.eclipse.jdt.internal.compiler.ast TypeDeclaration declarationOf

Introduction

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

Prototype

public AbstractMethodDeclaration declarationOf(MethodBinding methodBinding) 

Source Link

Document

Find the matching parse node, answers null if nothing found

Usage

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

License:Open Source License

protected void reportDeclaration(MethodBinding methodBinding, MatchLocator locator, SimpleSet knownMethods)
        throws CoreException {
    ReferenceBinding declaringClass = methodBinding.declaringClass;
    IType type = locator.lookupType(declaringClass);
    if (type == null)
        return; // case of a secondary type

    // Report match for binary
    if (type.isBinary()) {
        IMethod method = null;//from   w  ww . ja  v  a  2  s .  c om
        TypeBinding[] parameters = methodBinding.original().parameters;
        int parameterLength = parameters.length;
        char[][] parameterTypes = new char[parameterLength][];
        for (int i = 0; i < parameterLength; i++) {
            char[] typeName = parameters[i].qualifiedSourceName();
            for (int j = 0, dim = parameters[i].dimensions(); j < dim; j++) {
                typeName = CharOperation.concat(typeName, new char[] { '[', ']' });
            }
            parameterTypes[i] = typeName;
        }
        method = locator.createBinaryMethodHandle(type, methodBinding.selector, parameterTypes);
        if (method == null || knownMethods.addIfNotIncluded(method) == null)
            return;

        IResource resource = type.getResource();
        if (resource == null)
            resource = type.getJavaProject().getProject();
        IBinaryType info = locator.getBinaryInfo((org.eclipse.jdt.internal.core.ClassFile) type.getClassFile(),
                resource);
        locator.reportBinaryMemberDeclaration(resource, method, methodBinding, info, SearchMatch.A_ACCURATE);
        return;
    }

    // When source is available, report match if method is found in the declaring type
    IResource resource = type.getResource();
    if (declaringClass instanceof ParameterizedTypeBinding)
        declaringClass = ((ParameterizedTypeBinding) declaringClass).genericType();
    ClassScope scope = ((SourceTypeBinding) declaringClass).scope;
    if (scope != null) {
        TypeDeclaration typeDecl = scope.referenceContext;
        AbstractMethodDeclaration methodDecl = typeDecl.declarationOf(methodBinding.original());
        if (methodDecl != null) {
            // Create method handle from method declaration
            String methodName = new String(methodBinding.selector);
            Argument[] arguments = methodDecl.arguments;
            int length = arguments == null ? 0 : arguments.length;
            String[] parameterTypes = new String[length];
            for (int i = 0; i < length; i++) {
                char[][] typeName = arguments[i].type.getParameterizedTypeName();
                parameterTypes[i] = Signature.createTypeSignature(CharOperation.concatWith(typeName, '.'),
                        false);
            }
            IMethod method = type.getMethod(methodName, parameterTypes);
            if (method == null || knownMethods.addIfNotIncluded(method) == null)
                return;

            // Create and report corresponding match
            int offset = methodDecl.sourceStart;
            this.match = new MethodDeclarationMatch(method, SearchMatch.A_ACCURATE, offset,
                    methodDecl.sourceEnd - offset + 1, locator.getParticipant(), resource);
            locator.report(this.match);
        }
    }
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode.java

License:Open Source License

private FieldNode fieldBindingToFieldNode(FieldBinding fieldBinding, TypeDeclaration groovyTypeDecl) {
    String name = new String(fieldBinding.name);
    int modifiers = fieldBinding.modifiers;
    ClassNode fieldType = resolver.convertToClassNode(fieldBinding.type);
    Constant c = fieldBinding.constant();

    Expression initializerExpression = null;
    // FIXASC for performance reasons could fetch the initializer lazily if a JDTFieldNode were created
    if (c == Constant.NotAConstant) {
        /**/*  w  w w . ja v  a 2 s .  c  o m*/
         * If the field binding is for a real source field, we should be able to see any initializer in it.
         */
        if (groovyTypeDecl != null) {
            FieldDeclaration fieldDecl = groovyTypeDecl.declarationOf(fieldBinding);
            if (fieldDecl instanceof FieldDeclarationWithInitializer) {
                initializerExpression = ((FieldDeclarationWithInitializer) fieldDecl).getGroovyInitializer();
            }
        }
    } else {
        if (c instanceof StringConstant) {
            initializerExpression = new ConstantExpression(((StringConstant) c).stringValue());
        } else if (c instanceof BooleanConstant) {
            initializerExpression = new ConstantExpression(((BooleanConstant) c).booleanValue());
        } else if (c instanceof IntConstant) {
            initializerExpression = new ConstantExpression(((IntConstant) c).intValue());
        } else if (c instanceof LongConstant) {
            initializerExpression = new ConstantExpression(((LongConstant) c).longValue());
        } else if (c instanceof DoubleConstant) {
            initializerExpression = new ConstantExpression(((DoubleConstant) c).doubleValue());
        } else if (c instanceof FloatConstant) {
            initializerExpression = new ConstantExpression(((FloatConstant) c).floatValue());
        } else if (c instanceof ByteConstant) {
            initializerExpression = new ConstantExpression(((ByteConstant) c).byteValue());
        } else if (c instanceof CharConstant) {
            initializerExpression = new ConstantExpression(((CharConstant) c).charValue());
        } else if (c instanceof ShortConstant) {
            initializerExpression = new ConstantExpression(((ShortConstant) c).shortValue());
        }
    }
    FieldNode fNode = new JDTFieldNode(fieldBinding, resolver, name, modifiers, fieldType, this,
            initializerExpression);
    return fNode;
}