List of usage examples for com.google.gwt.dev.jjs.ast JMethodCall getArgs
public List<JExpression> getArgs()
From source file:rocket.logging.compiler.LoggerOptimiser.java
License:Apache License
@Override public void endVisit(final JMethodCall methodCall, final Context context) { while (true) { final JMethod method = methodCall.getTarget(); // its definitely not the LoggerFactory.getLogger(String) method... if (method != this.getGetLogger()) { break; }//w ww .ja v a 2s. c o m // complain if the method doesnt have exactly 1 argument... assert (methodCall.getArgs().size() == 1); // check if the argument to the method is a string literal... final JExpression arg = (JExpression) methodCall.getArgs().get(0); // cant continue if argument is not a StringLiteral... this request // will have to be resolved at runtime. if (false == arg instanceof JStringLiteral) { break; } this.adjustMethod(methodCall, context); break; } }
From source file:rocket.logging.compiler.LoggingLevelByNameAssigner.java
License:Apache License
@Override protected void adjustMethod(final JMethodCall methodCall, final Context context) { final JStringLiteral stringLiteral = (JStringLiteral) methodCall.getArgs().get(0); final String name = stringLiteral.getValue(); final JClassType logger = this.getLevelLogger(name); if (null == logger) { throw new IllegalStateException("Unable to fetch logger for \"" + name + "\"."); }//from w ww . j a va 2s . c om final JMethod loggerConstructor = this.findLevelLoggerConstructorMethod(logger); if (null == loggerConstructor) { throw new IllegalStateException("Unable to find constructor for type: " + logger); } // replace LoggerFactory.getLogger(String) with new XXXLevelLogger( new // WrappedLogger ); final JClassType targetLogger = this.getTargetLogger(name); if (null == logger) { throw new IllegalStateException("Unable to fetch logger for \"" + name + "\"."); } final JMethod targetLoggerConstructor = this.findTargetLoggerConstructorMethod(targetLogger); if (null == targetLoggerConstructor) { throw new IllegalStateException("Unable to find constructor for type: " + targetLogger); } // havent built new wrapped expression! final JProgram program = this.getProgram(); final JNewInstance newTargetInstance = new JNewInstance(program, methodCall.getSourceInfo(), targetLogger); final JMethodCall callNewTargetInstance = new JMethodCall(program, methodCall.getSourceInfo(), newTargetInstance, targetLoggerConstructor); callNewTargetInstance.getArgs().add(program.getLiteralString(name)); // inserts a new xxxLevelLogger( Logger ) final JNewInstance newLevelLoggerInstance = new JNewInstance(program, methodCall.getSourceInfo(), logger); JMethodCall call = new JMethodCall(program, methodCall.getSourceInfo(), newLevelLoggerInstance, loggerConstructor); call.getArgs().add(callNewTargetInstance); context.replaceMe(call); }
From source file:xapi.dev.inject.MagicMethods.java
License:Open Source License
/** * Replaces a call from {@link X_Inject#singletonAsync(Class, xapi.util.api.ReceivesValue)} by first a) * generating an async provider, and then b) sending the value receiver into the async provider as a * callback. See the {@link AsyncProxy} class and {@link AsyncInjectionGenerator} for implementation. * * @param logger - The logger to log to. * @param methodCall - The method call we are overwriting * @param currentMethod - The encapsulated method itself * @param context - The method call context, so you can insert clinits / whatnot * @param ast - A view over UnifyAst, exposing our basic needs * @return - A JExpression to replace this method call with * @throws UnableToCompleteException// ww w. ja v a 2 s . c o m */ public static JExpression rebindSingletonAsync(TreeLogger logger, JMethodCall methodCall, JMethod currentMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { assert (methodCall.getArgs().size() == 2); JExpression classParam = methodCall.getArgs().get(0); JExpression receiverParam = methodCall.getArgs().get(1); if (!(classParam instanceof JClassLiteral)) { ast.error(methodCall, "Only class literals may be used as arguments to X_Inject.singletonAsync; you sent " + classParam.getClass() + " - " + classParam); return null; } logger.log(logLevel(), receiverParam.toString() + " : " + receiverParam.getClass() + " : " + receiverParam.toSource()); JClassLiteral classLiteral = (JClassLiteral) classParam; JDeclaredType answerType = injectSingletonAsync(logger, classLiteral, methodCall, ast); JDeclaredType receiverType = ast.searchForTypeBySource("xapi.util.api.ReceivesValue"); for (JMethod method : receiverType.getMethods()) { if (method.getName().equals("set")) { SourceInfo info = methodCall.getSourceInfo().makeChild(SourceOrigin.UNKNOWN); JExpression result; result = JGwtCreate.createInstantiationExpression(methodCall.getSourceInfo(), (JClassType) answerType); if (result == null) { ast.error(methodCall, "Rebind result '" + answerType + "' has no default (zero argument) constructors"); return null; } JMethodCall call = new JMethodCall(info, result, method); call.addArg(receiverParam); if (logger.isLoggable(logLevel())) { TreeLogger branch = logger.branch(logLevel(), "Generated asynchronous magic singleton: "); for (String str : call.toSource().split("\n")) { branch.log(logLevel(), str); } } return call; } } throw new InternalCompilerException("Unable to generate asynchronous class injector"); }
From source file:xapi.dev.inject.MagicMethods.java
License:Open Source License
/** * Replaces a call from {@link X_Inject#singletonAsync(Class, xapi.util.api.ReceivesValue)} by first a) * generating an async provider, and then b) sending the value receiver into the async provider as a * callback. See the {@link AsyncProxy} class and {@link AsyncInjectionGenerator} for implementation. * * @param logger - The logger to log to. * @param methodCall - The method call we are overwriting * @param currentMethod - The encapsulated method itself * @param context - The method call context, so you can insert clinits / whatnot * @param ast - A view over UnifyAst, exposing our basic needs * @return - A JExpression to replace this method call with * @throws UnableToCompleteException/*w ww. j a v a 2 s .c o m*/ */ public static JExpression rebindSingletonAndCallback(TreeLogger logger, JMethodCall methodCall, JMethod currentMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { assert (methodCall.getArgs().size() == 2); JExpression classParam = methodCall.getArgs().get(0); JExpression receiveParam = methodCall.getArgs().get(1); if (!(classParam instanceof JClassLiteral)) { ast.error(methodCall, "Only class literals may be used as arguments to X_Inject.singletonAsync; you sent " + classParam.getClass() + " - " + classParam); return null; } logger.log(logLevel(), receiveParam.toString() + " : " + receiveParam.getClass() + " : " + receiveParam.toSource()); JClassLiteral classLiteral = (JClassLiteral) classParam; JClassLiteral receiverLiteral = (JClassLiteral) receiveParam; JDeclaredType type = (JDeclaredType) classLiteral.getRefType(); String[] names = type.getShortName().split("[$]"); // TODO: stop stripping the enclosing class name (need to update generators) // String reqType = JGwtCreate.nameOf(type); String answer = receiverLiteral.getRefType().getName(); answer = answer.substring(0, answer.lastIndexOf('.') + 1) + "impl.AsyncProxy_" + names[names.length - 1]; JDeclaredType answerType = null; JDeclaredType knownType = ast.getProgram().getFromTypeMap(answer); // ensure we have a service provider JDeclaredType provider = injectSingletonAsync(logger, classLiteral, methodCall, ast); StandardGeneratorContext ctx = ast.getRebindPermutationOracle().getGeneratorContext(); // ctx.finish(logger); if (knownType != null) {// if the singleton already exists, just use it answerType = ast.searchForTypeBySource(answer); // result = // JGwtCreate.createInstantiationExpression(methodCall.getSourceInfo(), (JClassType) answerType, // currentMethod.getEnclosingType()); } else {// we need to generate the singleton on the fly, without updating rebind cache // make sure the requested interface is compiled for the generator logger.log(logLevel(), "Rebinding singleton w/ callback: " + type + " -> " + provider.getName()); ast.searchForTypeBySource(type.getName()); ast.searchForTypeBySource(BinaryName.toSourceName(provider.getName())); try { InjectionCallbackArtifact rebindResult; try { logger.log(logLevel(), "Loading injected result: " + provider.getName()); rebindResult = AsyncProxyGenerator.setupAsyncCallback(logger, ctx, ctx.getTypeOracle().findType(BinaryName.toSourceName(type.getName())), ((JDeclaredType) receiverLiteral.getRefType())); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new UnableToCompleteException(); } // creates the singleton and provider // RebindResult rebindResult = // 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 answerType = ast.searchForTypeBySource(rebindResult.getAsyncInjectionName()); // sanity check if (answerType == null) { ast.error(methodCall, "Rebind result '" + answer + "' could not be found. Please be sure that " + type.getName() + " has a subclass on the classpath which contains @SingletonOverride or @SingletonDefault annotations."); return null; } } catch (UnableToCompleteException e) { logger.log(Type.ERROR, "Error trying to generator 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(methodCall, "Rebind result '" + answer + "' could not be found"); return null; } } for (JMethod method : answerType.getMethods()) { if (method.getName().equals("go")) // JExpression inst = JGwtCreate.createInstantiationExpression(answerType.getSourceInfo(), // (JClassType)answerType, answerType.getEnclosingType()); return new JMethodCall(method.getSourceInfo(), null, method); } throw new InternalCompilerException("Did not generate async proxy for " + answerType); }
From source file:xapi.dev.inject.MagicMethods.java
License:Open Source License
/** * Replaces a call from {@link X_Inject#singletonLazy(Class)} by first a-0) generating a provider which will * be synchronous if an async call hasn't already been made, or a-1) generating a provider which will route * through the async provider, and return null before inited. then b) returning a simple lazy provider which * will poll the actual provider until it is set. If you use the * {@link X_Inject#singletonAsync(Class, xapi.util.api.ReceivesValue)} once, you should not use the * other two synchronous provider methods, as they may return null if you happen to request them before the * code split containing the service is downloaded. * * @param logger - The logger to log to. * @param methodCall - The method call we are overwriting * @param currentMethod - The encapsulated method itself * @param context - The method call context, so you can insert clinits / whatnot * @param ast - A view over UnifyAst, exposing our basic needs * @return - A JExpression to replace this method call with * @throws UnableToCompleteException/*from w w w. ja v a 2 s.c o m*/ */ public static JExpression rebindSingletonLazy(TreeLogger logger, JMethodCall x, JMethod currentMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { assert (x.getArgs().size() == 1); JExpression arg = x.getArgs().get(0); if (!(arg instanceof JClassLiteral)) { ast.error(x, "Only class literals may be used as arguments to X_Inject.lazySingleton; you sent " + arg.getClass() + " - " + arg); return null; } JClassLiteral classLiteral = (JClassLiteral) arg; if (!(classLiteral.getRefType() instanceof JDeclaredType)) { ast.error(x, "Only classes and interfaces may be used as arguments to X_Inject.singletonLazy()"); return null; } return injectLazySingleton(logger, classLiteral, x, currentMethod.getEnclosingType(), ast); }
From source file:xapi.dev.inject.MagicMethods.java
License:Open Source License
/** * Replaces a call from {@link X_Inject#singleton(Class)} by first a-0) generating a provider which will be * synchronous if an async call hasn't already been made, or a-1) generating a provider which will route * through the async provider, and return null before inited. then b) creates a lazy provider to call into * the synchronous provider finally c) calls .get() on the provider and return the value. If you use the * {@link X_Inject#singletonAsync(Class, xapi.util.api.ReceivesValue)} once, you should not use the * other two synchronous provider methods, as they may return null if you happen to request them before the * code split containing the service is downloaded. * * @param logger - The logger to log to. * @param methodCall - The method call we are overwriting * @param currentMethod - The encapsulated method itself * @param context - The method call context, so you can insert clinits / whatnot * @param ast - A view over UnifyAst, exposing our basic needs * @return - A JExpression to replace this method call with * @throws UnableToCompleteException/* w w w . j a v a 2 s. c o m*/ */ public static JExpression rebindSingleton(TreeLogger logger, JMethodCall x, JMethod currentMethod, Context context, UnifyAstView ast) throws UnableToCompleteException { assert (x.getArgs().size() == 1); JExpression arg = x.getArgs().get(0); if (!(arg instanceof JClassLiteral)) { ast.error(x, "Only class literals may be used as arguments to X_Inject.lazySingleton; you sent " + arg.getClass() + " - " + arg); return null; } JClassLiteral classLiteral = (JClassLiteral) arg; return injectSingleton(logger, classLiteral, x, ast); }
From source file:xapi.dev.inject.MagicMethods.java
License:Open Source License
/** * Replaces a call from {@link X_Inject#singleton(Class)} by first a-0) generating a provider which will be * synchronous if an async call hasn't already been made, or a-1) generating a provider which will route * through the async provider, and return null before inited. then b) creates a lazy provider to call into * the synchronous provider finally c) calls .get() on the provider and return the value. If you use the * {@link X_Inject#singletonAsync(Class, xapi.util.api.ReceivesValue)} once, you should not use the * other two synchronous provider methods, as they may return null if you happen to request them before the * code split containing the service is downloaded. * * @param logger - The logger to log to. * @param methodCall - The method call we are overwriting * @param currentMethod - The encapsulated method itself * @param context - The method call context, so you can insert clinits / whatnot * @param ast - A view over UnifyAst, exposing our basic needs * @return - A JExpression to replace this method call with * @throws UnableToCompleteException//from www .j av a 2 s.co m */ public static JExpression rebindInstance(TreeLogger logger, JMethodCall x, JMethod currentMethod, Context context, final UnifyAstView ast) throws UnableToCompleteException { assert (x.getArgs().size() == 1); JExpression arg = x.getArgs().get(0); if (!(arg instanceof JClassLiteral)) { //uh-oh; our class argument isn't actually a literal. //it may be a reference to a magic class, //in which case it will have java.lang.Class as a supertype. //our search semantics (for methods) are as follows: //first check the first arguments of methods for magic class or class lit. //if one is found, emit log and use it //if not, throw UnableToComplete (TODO a xinject.strict flag to disallow type search) if (arg instanceof JVariableRef) { JVariableRef local = (JVariableRef) arg; JExpression init = local.getTarget().getDeclarationStatement().initializer; if (init instanceof JVariableRef) { JVariableRef ref = (JVariableRef) init; String fromSourceInfo = ref.getSourceInfo().getFileName().replace("gen/", "") .replace("_MC.java", "").replaceAll("/", "."); JDeclaredType type; //TODO error handling type = ast.searchForTypeBySource(fromSourceInfo); arg = new JClassLiteral(type.getSourceInfo(), type); } } else if (arg instanceof JMethodCall) { JMethodCall call = (JMethodCall) arg; System.out.println(call.getType()); arg = call.getArgs().get(0); } if (!(arg instanceof JClassLiteral)) { logger.log(Type.ERROR, "Could not generate X_Inject.instance for " + arg.getType().getName()); } } JClassLiteral classLiteral = (JClassLiteral) arg; return injectInstance(logger, classLiteral, x, currentMethod, ast); }