Example usage for com.google.gwt.dev.util.collect Lists create

List of usage examples for com.google.gwt.dev.util.collect Lists create

Introduction

In this page you can find the example usage for com.google.gwt.dev.util.collect Lists create.

Prototype

public static <T> List<T> create(T... items) 

Source Link

Usage

From source file:xapi.dev.inject.MagicMethods.java

License:Open Source License

private static JExpression injectInstance(TreeLogger logger, JClassLiteral classLiteral, JMethodCall x,
        JMethod method, UnifyAstView ast) throws UnableToCompleteException {
    JDeclaredType type = (JDeclaredType) classLiteral.getRefType();
    // JExpression expr = injectLazySingleton(logger, classLiteral, x, type, ast);
    // String[] names = type.getShortName().split("[$]");

    String instanceType = classLiteral.getRefType().getName();
    StandardGeneratorContext ctx = ast.getRebindPermutationOracle().getGeneratorContext();
    // make sure the requested interface is compiled for the generator
    ast.searchForTypeBySource(type.getName());
    JDeclaredType injectedInstance;/*from   ww w  .j av  a  2 s. c o  m*/
    try {
        // our hardcoded class is definitely a generator ;-}
        Class<? extends Generator> generator = InstanceInjectionGenerator.class;
        // creates the singleton and provider
        RebindResult result = ctx.runGeneratorIncrementally(logger, generator, type.getName());
        // commit the generator result, w/out updating rebind cache (to allow GWT.create() rebinds)
        ctx.finish(logger);
        // pull back the LazySingeton provider
        logger.log(logLevel(), "Loading injected result: " + result.getResultTypeName());
        injectedInstance = ast.searchForTypeBySource(result.getResultTypeName());
        // sanity check
        if (injectedInstance == null) {
            ast.error(x, "Rebind result '" + instanceType + "' could not be found");
            throw new InternalCompilerException("Unable to generate instance provider");
        }
    } catch (UnableToCompleteException e) {
        logger.log(Type.ERROR, "Error trying to generate provider for " + type.getName() + ". "
                + "\nPlease make sure this class is non-abstract, or that a concrete class on the classpath "
                + "is annotated with @SingletonOverride or @SingletonDefault", e);
        ast.error(x, "Rebind result '" + instanceType + "' could not be found");
        throw new InternalCompilerException("Unable to generate instance provider");
    }
    if (!(injectedInstance instanceof JClassType)) {
        ast.error(x, "Rebind result '" + instanceType + "' must be a class");
        throw new InternalCompilerException("Unable to generate instance provider");
    }
    if (injectedInstance.isAbstract()) {
        ast.error(x, "Rebind result '" + instanceType + "' cannot be abstract");
        throw new InternalCompilerException("Unable to generate instance provider");
    }
    // now that we have our injected answer,
    // let's run it through normal gwt deferred binding as well

    // copied from UnifyAst#handleGwtCreate
    String reqType = BinaryName.toSourceName(injectedInstance.getName());
    List<String> answers;
    try {
        answers = Lists.create(ast.getRebindPermutationOracle().getAllPossibleRebindAnswers(logger, reqType));
        ast.getRebindPermutationOracle().getGeneratorContext().finish(logger);
    } catch (UnableToCompleteException e) {
        ast.error(x, "Failed to resolve '" + reqType + "' via deferred binding");
        return null;
    }
    ArrayList<JExpression> instantiationExpressions = new ArrayList<JExpression>(answers.size());
    for (String answer : answers) {
        JDeclaredType answerType = ast.searchForTypeBySource(answer);
        if (answerType == null) {
            ast.error(x, "Rebind result '" + answer + "' could not be found");
            return null;
        }
        if (!(answerType instanceof JClassType)) {
            ast.error(x, "Rebind result '" + answer + "' must be a class");
            return null;
        }
        if (answerType.isAbstract()) {
            ast.error(x, "Rebind result '" + answer + "' cannot be abstract");
            return null;
        }
        JDeclaredType enclosing = injectedInstance.getEnclosingType();
        if (enclosing == null) {
            enclosing = method.getEnclosingType();
        }
        JExpression result = JGwtCreate.createInstantiationExpression(x.getSourceInfo(),
                (JClassType) answerType);
        if (result == null) {
            ast.error(x, "Rebind result '" + answer + "' has no default (zero argument) constructors");
            return null;
        }
        instantiationExpressions.add(result);
    }
    assert answers.size() == instantiationExpressions.size();
    if (answers.size() == 1)
        return instantiationExpressions.get(0);
    else
        return new JGwtCreate(x.getSourceInfo(), reqType, answers, ast.getProgram().getTypeJavaLangObject(),
                instantiationExpressions);

    // TODO: cache each injection to detect the first time a class is injected,
    // then see if the given injection target is Preloadable,
    // so we can call it's clinit before it is ever accessed,
    // to reduce the bloat of clinits by visiting preloadable methods before
    // any client code can possibly access them (less clinit in runtime code)

}