@Override public void generateDecorator(final Decorable decorable, final FactoryController controller) { final List<Statement> statements = new ArrayList<Statement>(); if (decorable.getDecorableDeclaringType().isAnnotationPresent(RestrictedAccess.class)) { final RestrictedAccess annotation = decorable.getDecorableDeclaringType().getAnnotation(RestrictedAccess.class); final String roleListenerVar = "roleListener"; statements.add(controller.setReferenceStmt(roleListenerVar, Stmt.newObject(PageRoleLifecycleListener.class, annotation, Stmt.newObject(ClientRequiredRolesExtractorImpl.class)))); Statement roleListenerRef = controller.getReferenceStmt(roleListenerVar, PageRoleLifecycleListener.class); statements.add(Stmt.invokeStatic(IOC.class, "registerLifecycleListener", Refs.get("instance"), roleListenerRef)); controller.addInitializationStatements(statements); controller.addDestructionStatements(Collections.<Statement> singletonList(Stmt.invokeStatic(IOC.class, "unregisterLifecycleListener", Refs.get("instance"), roleListenerRef))); } }
@Override public void generateDecorator(final Decorable decorable, final FactoryController controller) { final ShadowService shadowService = (ShadowService) decorable.getAnnotation(); String serviceName = null; Statement subscribeShadowStatement = null; final Class<?> javaClass = decorable.getType().asClass(); for (final Class<?> intf : javaClass.getInterfaces()) { if (intf.isAnnotationPresent(Remote.class)) { serviceName = intf.getName() + ":RPC"; final AnonymousClassStructureBuilder builder = generateMethodDelegates(intf, decorable, controller); subscribeShadowStatement = Stmt.castTo(ClientMessageBus.class, Stmt.invokeStatic(ErraiBus.class, "get")) .invoke("subscribeShadow", serviceName, builder.finish()); } if (serviceName == null) { if (shadowService.value().equals("")) { serviceName = decorable.getName(); } else { serviceName = shadowService.value(); } subscribeShadowStatement = Stmt.castTo(ClientMessageBus.class, Stmt.invokeStatic(ErraiBus.class, "get")) .invoke("subscribeShadow", serviceName, controller.contextGetInstanceStmt()); } controller.addFactoryInitializationStatements(Collections.singletonList(subscribeShadowStatement)); } }
@Override protected List<Statement> generateDestroyInstanceStatements(final ClassStructureBuilder<?> bodyBlockBuilder, final Injectable injectable, final DependencyGraph graph, final InjectionContext injectionContext) { final List<Statement> destroyInstanceStmts = new ArrayList<>(); final Multimap<DependencyType, Dependency> depsByType = separateByType(injectable.getDependencies()); final Collection<Dependency> producerMemberDeps = depsByType.get(DependencyType.ProducerMember); if (producerMemberDeps.size() != 1) { throw new RuntimeException("A produced type must have exactly 1 producing instance but " + producerMemberDeps.size() + " were found."); } final Collection<Dependency> disposerMethodDeps = depsByType.get(DependencyType.DisposerMethod); if (disposerMethodDeps.size() > 1) { // TODO error message with matching disposer names. throw new RuntimeException(); } else if (disposerMethodDeps.size() == 1) { final DisposerMethodDependency disposerDep = (DisposerMethodDependency) disposerMethodDeps.iterator().next(); final MetaMethod disposer = disposerDep.getDisposerMethod(); controller.ensureMemberExposed(disposer); final Statement invocation = controller.exposedMethodStmt( controller.getReferenceStmt(PRODUCER_INSTANCE, disposer.getDeclaringClass()), disposer, getDisposerParams(disposer, depsByType.get(DependencyType.DisposerParameter), bodyBlockBuilder.getClassDefinition())); destroyInstanceStmts.add(invocation); } return destroyInstanceStmts; }
private static void addCleanup(final Decorable decorable, final FactoryController controller, final List<Statement> destructionStmts) { final DataBindingUtil.DataBinderRef dataBinder = DataBindingUtil.lookupDataBinderRef(decorable, controller); if (!controller.hasAttribute(STYLE_BINDING_HOUSEKEEPING_ATTR)) { destructionStmts.add( Stmt.invokeStatic(StyleBindingsRegistry.class, "get").invoke("cleanAllForBean", Refs.get("instance"))); if (dataBinder != null) { destructionStmts.add(controller.getReferenceStmt("styleBindingChangeHandlerUnsub", PropertyChangeUnsubscribeHandle.class).invoke("unsubscribe")); } controller.setAttribute(STYLE_BINDING_HOUSEKEEPING_ATTR, Boolean.TRUE); } }
+ targetClass + " has not been initialized. Either initialize or add @Inject!")).finish()); statements.add(controller.setReferenceStmt(DataBindingUtil.BINDER_VAR_NAME, loadVariable("binder"))); controller.ensureMemberExposed(decorable.get()); controller.setAttribute(DataBindingUtil.BINDER_MODEL_TYPE_VALUE, binderLookup.getDataModelType()); controller.addInitializationStatements(statements); if (!hasRunForType) { controller.addDestructionStatements(Collections.<Statement> singletonList( nestedCall(controller.getReferenceStmt(DataBindingUtil.BINDER_VAR_NAME, DataBinder.class)).invoke("unbind")));
if (!controller.hasAttribute(DATA_BINDING_CONFIG_ATTR)) { final String handlerVarName = "bindingChangeHandler"; controller.setAttribute(DATA_BINDING_CONFIG_ATTR, Boolean.TRUE); initStmts.add(controller.setReferenceStmt(handlerVarName, newObject(StyleBindingChangeHandler.class))); initStmts.add(controller.setReferenceStmt("styleBindingChangeHandlerUnsub", nestedCall(dataBinder.getValueAccessor()).invoke("addPropertyChangeHandler", controller.getReferenceStmt(handlerVarName, StyleBindingChangeHandler.class)))); controller.addInitializationStatements(initStmts); controller.addDestructionStatements(destructionStmts);
controller.addInitializationStatements( Collections.<Statement>singletonList( controller.setReferenceStmt(MODEL_VAR_NAME, DecorableType.PARAM.getAccessStatement(mp, decorable.getFactoryMetaClass())))); dataBinderRef = controller.getInstancePropertyStmt( controller.getReferenceStmt(MODEL_VAR_NAME, dataModelType), BINDER_VAR_NAME, DataBinder.class); controller.exposedFieldStmt(field); dataBinderRef = controller.getInstancePropertyStmt( DecorableType.FIELD.getAccessStatement(field, decorable.getFactoryMetaClass()), BINDER_VAR_NAME, DataBinder.class);
@Override public void generateDecorator(final Decorable decorable, final FactoryController controller) { MetaMethod method = decorable.getAsMethod(); MetaParameter[] params = method.getParameters(); if (params.length != 1 || !params[0].getType().getErased().equals(MetaClassFactory.get(SyncResponses.class))) { throw new GenerationException("Methods annotated with @" + Sync.class.getName() + " need to have exactly one parameter of type: " + SyncResponses.class.getName() + ". Check method: " + GenUtil.getMethodString(method) + " in class " + method.getDeclaringClass().getFullyQualifiedName()); } Sync syncAnnotation = (Sync) decorable.getAnnotation(); controller.addInitializationStatements(createInitStatements(decorable.getDecorableDeclaringType(), "obj", syncAnnotation, decorable, controller)); final Statement syncWorker = controller.getReferenceStmt("syncWorker", ClientSyncWorker.class); final Statement destruction = Stmt.nestedCall(syncWorker).invoke("stop"); controller.addDestructionStatements(Collections.singletonList(destruction)); }
@Override public void generateDecorator(Decorable decorable, FactoryController controller) { controller.addInvokeBefore(decorable.getAsMethod(), Stmt.invokeStatic(TestDataCollector.class, "beforeInvoke", Refs.get("text"), Refs.get("blah"))); controller.addInvokeAfter(decorable.getAsMethod(), Stmt.invokeStatic(TestDataCollector.class, "afterInvoke", Refs.get("text"), Refs.get("blah"))); final Statement foobar = controller.addProxyProperty("foobar", String.class, Stmt.load("foobie!")); controller.addInvokeAfter(decorable.getAsMethod(), Stmt.invokeStatic(TestDataCollector.class, "property", "foobar", foobar) ); controller.addInitializationStatements(Arrays.<Statement>asList(Stmt.loadVariable("instance").invoke("setFlag", true))); controller.addDestructionStatements(Arrays.<Statement>asList(Stmt.loadVariable("instance").invoke("setFlag", false))); } }
.finish()); controller.addInitializationStatements(Collections.singletonList(init)); controller.addInitializationStatements(initStmts); controller.addDestructionStatements(generateTemplateDestruction(decorable)); controller.addInitializationStatementsToEnd(Collections.<Statement>singletonList(invokeStatic(StyleBindingsRegistry.class, "get") .invoke("updateStyles", Refs.get("instance"))));
@Override public void generateDecorator(final Decorable decorable, final FactoryController controller) { if (decorable.getAsMethod().getParameters() == null || decorable.getAsMethod().getParameters().length != 1) throw new GenerationException("@ModelSetter method needs to have exactly one parameter: " + decorable.getAsMethod()); final MetaClass modelType = (MetaClass) controller.getAttribute(DataBindingUtil.BINDER_MODEL_TYPE_VALUE); if (!decorable.getAsMethod().getParameters()[0].getType().equals(modelType)) { throw new GenerationException("@ModelSetter method parameter must be of type: " + modelType); } final Statement dataBinder = controller.getReferenceStmt(DataBindingUtil.BINDER_VAR_NAME, DataBinder.class); final Statement proxyProperty = controller.addProxyProperty("dataBinder", DataBinder.class, dataBinder); final String modelParamName = decorable.getAsMethod().getParameters()[0].getName(); controller.addInvokeBefore(decorable.getAsMethod(), nestedCall(proxyProperty) .invoke("setModel", Refs.get(modelParamName), loadStatic(StateSync.class, "FROM_MODEL"))); controller.addInvokeBefore( decorable.getAsMethod(), loadVariable(modelParamName).assignValue( Cast.to(decorable.getAsMethod().getParameters()[0].getType(), nestedCall( proxyProperty).invoke("getModel")))); } }
QueryParamInitCallback.class, queryParamCallback.finish().finish())); statements.add(controller.setReferenceStmt("syncWorker", Stmt.invokeStatic(ClientSyncWorker.class, "create", syncAnnotation.query(), Stmt.loadVariable("objectClass"), null))); final Statement syncWorkerRef = controller.getReferenceStmt("syncWorker", ClientSyncWorker.class); statements.add( Stmt.nestedCall(syncWorkerRef).invoke("addSyncCallback", createSyncCallback(decorable)));
private Statement setProducerInstanceReference() { return controller.setReferenceStmt(PRODUCER_INSTANCE, loadVariable(PRODUCER_INSTANCE)); }
@Override public void generateDecorator(final Decorable decorable, final FactoryController controller) { final Statement callbackStmt = Stmt.newObject(Runnable.class).extend() .publicOverridesMethod("run") .append(decorable.getAccessStatement()) .finish() .finish(); controller.addInitializationStatements(Collections.<Statement>singletonList(Stmt.invokeStatic(InitVotes.class, "registerOneTimeInitCallback", callbackStmt))); } }
private BlockBuilder<AnonymousClassStructureBuilder> getSubscriptionCallback(final Decorable decorable, final FactoryController controller) { final MetaParameter parm = decorable.getAsParameter(); final MetaClass eventType = parm.getType().asBoxed(); final String parmClassName = eventType.getFullyQualifiedName(); final List<Annotation> annotations = InjectUtil.extractQualifiers(parm); final Annotation[] qualifiers = annotations.toArray(new Annotation[annotations.size()]); final Set<String> qualifierNames = new HashSet<>(CDI.getQualifiersPart(qualifiers)); final MetaClass callBackType = parameterizedAs(AbstractCDIEventCallback.class, typeParametersOf(eventType)); AnonymousClassStructureBuilder callBack = Stmt.newObject(callBackType).extend(); BlockBuilder<AnonymousClassStructureBuilder> callBackBlock; if (!qualifierNames.isEmpty()) { callBackBlock = callBack.initialize(); for (final String qualifierName : qualifierNames) { callBackBlock.append(Stmt.loadClassMember("qualifierSet").invoke("add", qualifierName)); } callBack = callBackBlock.finish(); } final List<Statement> fireEventStmts = new ArrayList<>(); if (!decorable.isEnclosingTypeDependent()) { fireEventStmts.add(Stmt.declareFinalVariable("instance", decorable.getEnclosingInjectable().getInjectedType(), Stmt.invokeStatic(Factory.class, "maybeUnwrapProxy", controller.contextGetInstanceStmt()))); } fireEventStmts.add(decorable.call(Refs.get("event"))); callBackBlock = callBack.publicOverridesMethod("fireEvent", Parameter.finalOf(eventType, "event")) .appendAll(fireEventStmts) .finish() .publicOverridesMethod("toString") ._(Stmt.load("Observer: " + parmClassName + " " + Arrays.toString(qualifiers)).returnValue()); return callBackBlock; }
@Override protected List<Statement> generateInvokePostConstructsStatements(final ClassStructureBuilder<?> bodyBlockBuilder, final Injectable injectable, final DependencyGraph graph, final InjectionContext injectionContext) { final List<Statement> stmts = new ArrayList<>(); final Queue<MetaMethod> postConstructMethods = gatherPostConstructs(injectable); for (final MetaMethod postConstruct : postConstructMethods) { if (postConstruct.isPublic()) { stmts.add(loadVariable("instance").invoke(postConstruct)); } else { controller.ensureMemberExposed(postConstruct); final String accessorName = getPrivateMethodName(postConstruct); stmts.add(invokePrivateAccessorWithNoParams(accessorName)); } } return stmts; }
private List<Statement> fieldCreateInstanceStatements(final MetaField producingMember, final Injectable producerInjectable, final Injectable producedInjectable, final ClassStructureBuilder<?> bodyBlockBuilder) { final List<Statement> stmts = new ArrayList<>(); controller.ensureMemberExposed(producingMember); if (!producingMember.isStatic()) { final Statement producerInstanceValue = loadVariable("contextManager").invoke("getInstance", producerInjectable.getFactoryName()); stmts.add(declareVariable(PRODUCER_INSTANCE, producerInjectable.getInjectedType(), producerInstanceValue)); stmts.add(loadVariable(PRODUCER_INSTANCE).assignValue(Stmt.castTo(producerInjectable.getInjectedType(), invokeStatic(Factory.class, "maybeUnwrapProxy", loadVariable(PRODUCER_INSTANCE))))); } final Statement invocation = controller.exposedFieldStmt(loadVariable(PRODUCER_INSTANCE), producingMember); stmts.add(declareFinalVariable("instance", producedInjectable.getInjectedType(), invocation)); if (!producingMember.isStatic()) { stmts.add(setProducerInstanceReference()); if (producerInjectable.getWiringElementTypes().contains(WiringElementType.DependentBean)) { stmts.add(loadVariable("this").invoke("registerDependentScopedReference", loadVariable("instance"), loadVariable(PRODUCER_INSTANCE))); } } stmts.add(loadVariable("instance").returnValue()); return stmts; }
@Override public void generateDecorator(final Decorable decorable, final FactoryController controller) { MetaMethod metaMethod = decorable.getAsMethod(); PermissionCheck securedResource = metaMethod.getAnnotation(PermissionCheck.class); String permission = securedResource.value(); String onGranted = securedResource.onGranted(); String onDenied = securedResource.onDenied(); String declaringClass = metaMethod.getDeclaringClassName(); // The method must return void if (!metaMethod.getReturnType().getName().equals("void")) { throw new RuntimeException("The @PermissionCheck annotated method \"" + declaringClass + "#" + metaMethod.getName() + "\" must return void"); } // Permission check if (permission == null || permission.trim().length() == 0) { Statement stmt = createPermissionCheck(permission, onGranted, onDenied); controller.addInvokeBefore(metaMethod, stmt); } }
private List<Statement> methodCreateInstanceStatements(final MetaMethod producingMember, final Injectable producerInjectable, final Injectable producedInjectable, final Collection<Dependency> paramDeps, final ClassStructureBuilder<?> bodyBlockBuilder) { final List<Statement> stmts = new ArrayList<>(); controller.ensureMemberExposed(producingMember); if (!producingMember.isStatic()) { final Statement producerInstanceValue = loadVariable("contextManager").invoke("getInstance", producerInjectable.getFactoryName()); stmts.add(declareVariable(PRODUCER_INSTANCE, producerInjectable.getInjectedType(), producerInstanceValue)); stmts.add(loadVariable(PRODUCER_INSTANCE).assignValue(castTo(producerInjectable.getInjectedType(), invokeStatic(Factory.class, "maybeUnwrapProxy", loadVariable(PRODUCER_INSTANCE))))); } final List<Statement> depScopeRegistrationStmts = new ArrayList<>(); final Statement[] producerParams = generateProducerParams(producingMember, paramDeps, stmts, depScopeRegistrationStmts); final Statement invocation = controller.exposedMethodStmt(loadVariable(PRODUCER_INSTANCE), producingMember, producerParams); stmts.add(declareFinalVariable("instance", producedInjectable.getInjectedType(), invocation)); if (!producingMember.isStatic()) { stmts.add(setProducerInstanceReference()); if (producerInjectable.getWiringElementTypes().contains(WiringElementType.DependentBean)) { stmts.add(loadVariable("this").invoke("registerDependentScopedReference", loadVariable("instance"), loadVariable(PRODUCER_INSTANCE))); } } stmts.addAll(depScopeRegistrationStmts); stmts.add(loadVariable("instance").returnValue()); return stmts; }
controller.addFactoryInitializationStatements(singletonList(castTo(constructed.get(declaringClass), invokeStatic(GWT.class, "create", constructed.get(declaringClass))).invoke("getStyle") .invoke("ensureInjected"))); final String compiledCss = compiler.compile(source); controller.addFactoryInitializationStatements(singletonList(invokeStatic(StyleInjector.class, "inject", loadLiteral(compiledCss)))); } catch (IOException | LessException e) { throw new RuntimeException("Error while attempting to compile the LESS stylesheet [" + resolvedStylesheetPath.get() + "].", e);