Example usage for org.eclipse.jdt.internal.compiler.lookup MethodScope MethodScope

List of usage examples for org.eclipse.jdt.internal.compiler.lookup MethodScope MethodScope

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup MethodScope MethodScope.

Prototype

public MethodScope(Scope parent, ReferenceContext context, boolean isStatic) 

Source Link

Usage

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

License:Apache License

/**
 * Creates a mock {@link CompilationUnitDeclaration} that has binary type
 * references in a superclass reference, in a method return type, in an
 * annotation and in a local variable declaration. It then checks that the we
 * find all of these locations except for the one used in an annotation.
 *///w  ww.  ja  va2s .co  m
public void testFindAllBinaryTypeReferenceSites() {
    CompilationResult compilationResult = new CompilationResult("TestCompilationUnit.java".toCharArray(), 0, 0,
            0);
    CompilationUnitDeclaration cud = new CompilationUnitDeclaration(null, compilationResult, 1);
    LookupEnvironment lookupEnvironment = createMockLookupEnvironment();
    cud.scope = new CompilationUnitScope(cud, lookupEnvironment);

    TypeDeclaration typeDeclaration = new TypeDeclaration(compilationResult);
    typeDeclaration.scope = new ClassScope(cud.scope, null);
    typeDeclaration.staticInitializerScope = new MethodScope(typeDeclaration.scope, null, false);
    cud.types = new TypeDeclaration[] { typeDeclaration };

    BinaryTypeBinding binaryTypeBinding = new BinaryTypeBinding(null, new MockBinaryType(BINARY_TYPE_NAME),
            lookupEnvironment);
    typeDeclaration.superclass = createMockBinaryTypeReference(binaryTypeBinding);

    MethodDeclaration methodDeclaration = new MethodDeclaration(compilationResult);
    methodDeclaration.scope = new MethodScope(typeDeclaration.scope, null, false);
    methodDeclaration.returnType = createMockBinaryTypeReference(binaryTypeBinding);

    LocalDeclaration localDeclaration = new LocalDeclaration(null, 0, 0);
    localDeclaration.type = createMockBinaryTypeReference(binaryTypeBinding);
    methodDeclaration.statements = new Statement[] { localDeclaration };

    SingleMemberAnnotation annotation = new SingleMemberAnnotation(
            createMockBinaryTypeReference(binaryTypeBinding), 0);
    annotation.memberValue = annotation.type;
    typeDeclaration.annotations = new Annotation[] { annotation };

    typeDeclaration.methods = new AbstractMethodDeclaration[] { methodDeclaration };

    /*
     * Check that we find binary type references in the following expected
     * locations.
     */
    Expression[] expectedExpressions = new Expression[] { typeDeclaration.superclass,
            methodDeclaration.returnType, localDeclaration.type };

    List<BinaryTypeReferenceSite> binaryTypeReferenceSites = BinaryTypeReferenceRestrictionsChecker
            .findAllBinaryTypeReferenceSites(cud);
    assertEquals(expectedExpressions.length, binaryTypeReferenceSites.size());
    for (int i = 0; i < binaryTypeReferenceSites.size(); ++i) {
        BinaryTypeReferenceSite binaryTypeReferenceSite = binaryTypeReferenceSites.get(i);
        assertSame(binaryTypeBinding, binaryTypeReferenceSite.getBinaryTypeBinding());
        assertSame(expectedExpressions[i], binaryTypeReferenceSite.getExpression());
    }
}

From source file:lombok.eclipse.handlers.ast.EclipseTypeEditor.java

License:Open Source License

private AbstractMethodDeclaration injectMethodImpl(final lombok.ast.AbstractMethodDecl<?> methodDecl) {
    final AbstractMethodDeclaration method = builder.build(methodDecl, MethodDeclaration.class);
    EclipseHandlerUtil.injectMethod(node(), method);

    TypeDeclaration type = get();/*from   w  w w .  j a  va 2  s  . c o m*/
    if (type.scope != null && method.scope == null) {
        boolean aboutToBeResolved = false;
        for (StackTraceElement elem : Thread.currentThread().getStackTrace()) {
            if ("org.eclipse.jdt.internal.compiler.lookup.ClassScope".equals(elem.getClassName())
                    && "buildFieldsAndMethods".equals(elem.getMethodName())) {
                aboutToBeResolved = true;
                break;
            }
        }
        if (!aboutToBeResolved) {
            MethodScope scope = new MethodScope(type.scope, method,
                    methodDecl.getModifiers().contains(lombok.ast.Modifier.STATIC));
            MethodBinding methodBinding = null;
            try {
                methodBinding = (MethodBinding) Reflection.methodScopeCreateMethodMethod.invoke(scope, method);
            } catch (final Exception e) {
                // See 'Reflection' class for why we ignore this exception.
            }
            if (methodBinding != null) {
                SourceTypeBinding sourceType = type.scope.referenceContext.binding;
                MethodBinding[] methods = sourceType.methods();
                methods = resize(methods, methods.length + 1);
                methods[methods.length - 1] = methodBinding;
                sourceType.setMethods(methods);
                sourceType.resolveTypesFor(methodBinding);
            }
        }
    }
    return method;
}

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

License:Open Source License

private MethodDeclaration createMethod(IProgramElement method, TypeDeclaration type, IType handle) {
    MethodDeclaration decl = new MethodDeclaration(type.compilationResult);
    decl.scope = new MethodScope(type.scope, decl, true);

    String[] split = method.getName().split("\\.");
    decl.selector = split[split.length - 1].toCharArray();
    decl.modifiers = method.getRawModifiers();

    decl.returnType = createTypeReference(method.getCorrespondingType(true));
    decl.modifiers = method.getRawModifiers();
    Argument[] args = method.getParameterTypes() != null ? new Argument[method.getParameterTypes().size()]
            : new Argument[0];
    try {/*from   w  ww  . j  a v  a 2s  .co m*/
        ErasedTypeSignature sig = null;
        if (handle != null) {
            AJWorldFacade world = new AJWorldFacade(handle.getJavaProject().getProject());
            sig = world.getMethodTypeSignatures(
                    Signature.createTypeSignature(handle.getFullyQualifiedName(), true), method);
        }
        if (sig == null) {
            String[] params = new String[method.getParameterTypes().size()];
            for (int i = 0; i < params.length; i++) {
                params[i] = new String(Signature.getTypeErasure((char[]) method.getParameterTypes().get(i)));
            }
            sig = new ErasedTypeSignature(method.getCorrespondingTypeSignature(), params);
        }

        List<String> pNames = method.getParameterNames();
        // bug 270123... no parameter names if coming in from a jar and
        // not build with debug info...mock it up.
        if (pNames == null || pNames.size() != args.length) {
            pNames = new ArrayList<String>(args.length);
            for (int i = 0; i < args.length; i++) {
                pNames.add("args" + i);
            }
        }
        for (int i = 0; i < args.length; i++) {
            args[i] = new Argument(((String) pNames.get(i)).toCharArray(), 0,
                    createTypeReference(Signature.getElementType(sig.paramTypes[i])), 0);
        }

        decl.returnType = createTypeReferenceFromSignature(sig.returnTypeSig);
        decl.typeParameters = createTypeParameters(sig.typeParameters);
    } catch (Exception e) {
        AJLog.log("Exception occurred in ITDInserter.createMethod().  (Ignoring)");
        AJLog.log("Relevant method: " + method.getParent().getName() + "." + method.getName());
        List<String> pNames = method.getParameterNames();
        // bug 270123... no parameter names if coming in from a jar and
        // not build with debug info...mock it up.
        if (pNames == null || pNames.size() != args.length) {
            pNames = new ArrayList<String>(args.length);
            for (int i = 0; i < args.length; i++) {
                pNames.add("args" + i);
            }
        }
        for (int i = 0; i < args.length; i++) {
            args[i] = new Argument(((String) pNames.get(i)).toCharArray(), 0,
                    createTypeReference(new String((char[]) method.getParameterTypes().get(i))), 0);
        }
    }
    decl.arguments = args;
    return decl;
}

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

License:Open Source License

private ConstructorDeclaration createConstructor(IProgramElement constructor, TypeDeclaration type) {
    ConstructorDeclaration decl = new ConstructorDeclaration(type.compilationResult);
    decl.scope = new MethodScope(type.scope, decl, true);
    decl.selector = constructor.getName().split("\\.")[1].toCharArray();
    decl.modifiers = constructor.getRawModifiers();
    Argument[] args = constructor.getParameterTypes() != null
            ? new Argument[constructor.getParameterTypes().size()]
            : new Argument[0];

    List<String> pNames = constructor.getParameterNames();
    // bug 270123, bug 334328... no parameter names if coming in from a jar and
    // not build with debug info...mock it up.
    if (pNames == null || pNames.size() != args.length) {
        pNames = new ArrayList<String>(args.length);
        for (int i = 0; i < args.length; i++) {
            pNames.add("args" + i);
        }// w  ww.  j  a va 2  s  . co  m
    }

    for (int i = 0; i < args.length; i++) {
        args[i] = new Argument(pNames.get(i).toCharArray(), 0,
                createTypeReference(new String((char[]) constructor.getParameterTypes().get(i))), 0);
    }
    decl.arguments = args;
    return decl;
}