Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 JBoss, by Red Hat, Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.errai.enterprise.rebind;
 
 import static org.jboss.errai.codegen.meta.MetaClassFactory.parameterizedAs;
 import static org.jboss.errai.codegen.meta.MetaClassFactory.typeParametersOf;
 
 
 import java.util.List;
 import java.util.Set;

Generates the boiler plate for

Author(s):
Heiko Braun <hbraun@redhat.com>
Mike Brock <cbrock@redhat.com>
Christian Sadilek <csadilek@redhat.com>
Observes:
annotations use in GWT clients.
Basically creates a subscription for a CDI event type that invokes on the annotated method.
 
 public class ObservesExtension extends IOCDecoratorExtension<Observes> {
   public ObservesExtension(final Class<ObservesdecoratesWith) {
     super(decoratesWith);
   }
 
   @Override
   public List<? extends StatementgenerateDecorator(final InjectableInstance<Observesinstance) {
     final Context ctx = instance.getInjectionContext().getProcessingContext().getContext();
     final MetaMethod method = instance.getMethod();
     final MetaParameter parm = instance.getParm();
 
     if (!method.isPublic()) {
     }
 
     final String parmClassName = parm.getType().getFullyQualifiedName();
     final List<Annotationannotations = InjectUtil.extractQualifiers(instance);
     final Annotation[] qualifiers = annotations.toArray(new Annotation[annotations.size()]);
     final Set<StringqualifierNames = new HashSet<String>(CDI.getQualifiersPart(qualifiers));
 
     if (qualifierNames.contains(Any.class.getName())) {
       qualifierNames.remove(Any.class.getName());
     }
 
     final MetaClass callBackType = parameterizedAs(AbstractCDIEventCallback.classtypeParametersOf(parm.getType()));
 
     AnonymousClassStructureBuilder callBack = Stmt.newObject(callBackType).extend();
 
 
     if (!qualifierNames.isEmpty()) {
       callBackBlock = callBack.initialize();
       for (final String qualifierName : qualifierNames) {
         callBackBlock.append(Stmt.loadClassMember("qualifierSet").invoke("add"qualifierName));
       }
       callBack = callBackBlock.finish();
     }
 
    callBackBlock = callBack.publicOverridesMethod("fireEvent", Parameter.finalOf(parm"event"))
        ._(instance.callOrBind(Refs.get("event")))
        .finish()
        .publicOverridesMethod("toString")
        ._(Stmt.load("Observer: " + parmClassName + " " + Arrays.toString(qualifiers)).returnValue());
    final List<Statementstatements = new ArrayList<Statement>();
    // create the destruction callback to deregister the service when the bean is destroyed.
    final String subscrVar = InjectUtil.getUniqueVarName();
    final String subscribeMethod;
    if (EnvUtil.isPortableType(parm.getType().asClass()) && !EnvUtil.isLocalEventType(parm.getType().asClass())) {
      subscribeMethod = "subscribe";
    }
    else {
      subscribeMethod = "subscribeLocal";
    }
    final Statement subscribeStatement =
        Stmt.declareVariable(Subscription.class).asFinal().named(subscrVar)
            .initializeWith(Stmt.create(ctx).invokeStatic(CDI.classsubscribeMethodparmClassName,
                callBackBlock.finish().finish()));
    statements.add(subscribeStatement);
    // create the destruction callback to deregister the service when the bean is destroyed.
    final MetaClass destructionCallbackType =
    final BlockBuilder<AnonymousClassStructureBuilderdestroyMeth
        = ObjectBuilder.newInstanceOf(destructionCallbackType).extend()
        .publicOverridesMethod("destroy", Parameter.finalOf(instance.getEnclosingType(), "obj"))
        .append(Stmt.loadVariable(subscrVar).invoke("remove")).append(Stmt.codeComment("WEEEEE!"));
    for (final Class<?> cls : EnvUtil.getAllPortableConcreteSubtypes(parm.getType().asClass())) {
      if (!EnvUtil.isLocalEventType(cls)) {
        final String subscrHandle = InjectUtil.getUniqueVarName();
        statements.add(Stmt.declareVariable(Subscription.class).asFinal().named(subscrHandle)
            .initializeWith(Stmt.invokeStatic(ErraiBus.class"get").invoke("subscribe",
                CDI.getSubjectNameByType(cls.getName()),
                Stmt.loadStatic(CDI.class"ROUTING_CALLBACK"))));
        destroyMeth.append(Stmt.loadVariable(subscrHandle).invoke("remove"));
      }
    }
    final Statement destructionCallback = Stmt.create().loadVariable("context").invoke("addDestructionCallback",
        Refs.get(instance.getInjector().getInstanceVarName()), destroyMeth.finish().finish());
    statements.add(destructionCallback);
    return statements;
  }
New to GrepCode? Check out our FAQ X