Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 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.ui.rebind;
 
 import java.util.List;
 import java.util.Map;
 
 import  javax.inject.Inject;
 
 import  org.jboss.errai.codegen.Statement;
 import  org.jboss.errai.codegen.exception.GenerationException;
 import  org.jboss.errai.codegen.meta.MetaClass;
 import  org.jboss.errai.codegen.util.Stmt;
 import  org.jboss.errai.common.client.ui.ElementWrapperWidget;
 import  org.jboss.errai.ioc.client.api.CodeDecorator;
 import  org.jboss.errai.ioc.rebind.ioc.extension.IOCDecoratorExtension;
 import  org.jboss.errai.ioc.rebind.ioc.injector.api.InjectableInstance;
 
 import  com.google.common.base.Strings;
 import  com.google.gwt.dom.client.Element;


Store all injected DataField Statement instances into the aggregate Map for this composite Template.

Author(s):
Lincoln Baxter, III
 
 @CodeDecorator
 public class DataFieldCodeDecorator extends IOCDecoratorExtension<DataField> {
 
   public DataFieldCodeDecorator(Class<DataFielddecoratesWith) {
     super(decoratesWith);
   }
 
   @Override
   public List<? extends Statement> generateDecorator(InjectableInstance<DataFieldctx) {
     ctx.ensureMemberExposed();
     Statement instance = ctx.getValueStatement();
     String name = getTemplateDataFieldName(ctx.getAnnotation(), ctx.getMemberName());
     if (ctx.getElementTypeOrMethodReturnType().isAssignableTo(Element.class)) {
       if (ctx.isAnnotationPresent(Inject.class)) {
         throw new GenerationException("@DataField [" + name + "] in class ["
             + ctx.getEnclosingType().getFullyQualifiedName() + "] is of type ["
             + ctx.getElementTypeOrMethodReturnType().getFullyQualifiedName()
             + "] which does not support @Inject; this instance must be created manually.");
       }
       instance = Stmt.invokeStatic(ElementWrapperWidget.class"getWidget"instance);
     }
     saveDataField(ctxctx.getElementTypeOrMethodReturnType(), namectx.getMemberName(), instance);
 
     return Collections.emptyList();
   }
 
   private void saveDataField(InjectableInstance<DataFieldctx, MetaClass typeString nameString fieldName, Statement instance) {
     dataFieldMap(ctxctx.getEnclosingType()).put(nameinstance);
     dataFieldTypeMap(ctxctx.getEnclosingType()).put(nametype);
   }
 
   private String getTemplateDataFieldName(DataField annotationString deflt) {
     String value = Strings.nullToEmpty(annotation.value()).trim();
     return value.isEmpty() ? deflt : value;
   }

  
Get the map of DataField names and Statement instances.
 
   @SuppressWarnings("unchecked")
   private static Map<String, Statement> dataFieldMap(InjectableInstance<?> ctx, MetaClass templateType) {
     String dataFieldMapName = dataFieldMapName(templateType);
 
     Map<String, Statement> dataFields = (Map<String, Statement>) ctx.getInjectionContext().getAttribute(
         dataFieldMapName);
     if (dataFields == null) {
       dataFields = new LinkedHashMap<String, Statement>();
       ctx.getInjectionContext().setAttribute(dataFieldMapNamedataFields);
     }
 
     return dataFields;
   }

  
Get the map of DataField names and MetaClass types.
  @SuppressWarnings("unchecked")
  private static Map<String, MetaClass> dataFieldTypeMap(InjectableInstance<?> ctx, MetaClass templateType) {
    String dataFieldTypeMapName = dataFieldTypeMapName(templateType);
    Map<String, MetaClass> dataFieldTypes = (Map<String, MetaClass>) ctx.getInjectionContext().getAttribute(
        dataFieldTypeMapName);
    if (dataFieldTypes == null) {
      dataFieldTypes = new LinkedHashMap<String, MetaClass>();
      ctx.getInjectionContext().setAttribute(dataFieldTypeMapNamedataFieldTypes);
    }
    return dataFieldTypes;
  }

  
Get the aggregate map of DataField names and Statement instances for the given MetaClass type and all ancestors returned by MetaClass.getSuperClass().
  @SuppressWarnings("unchecked")
  public static Map<String, Statement> aggregateDataFieldMap(InjectableInstance<?> ctx, MetaClass componentType) {
    Map<String, Statement> result = new LinkedHashMap<String, Statement>();
    if (componentType.getSuperClass() != null) {
      result.putAll(aggregateDataFieldMap(ctxcomponentType.getSuperClass()));
    }
    Map<String, Statement> dataFields = (Map<String, Statement>) ctx.getInjectionContext().getAttribute(
        dataFieldMapName(componentType));
    if (dataFields != null) {
      result.putAll(dataFields);
    }
    return result;
  }

  
Get the aggregate map of DataField names and MetaClass types for the given MetaClass component type and all ancestors returned by MetaClass.getSuperClass().
  @SuppressWarnings("unchecked")
  public static Map<String, MetaClass> aggregateDataFieldTypeMap(InjectableInstance<?> ctx, MetaClass componentType) {
    Map<String, MetaClass> result = new LinkedHashMap<String, MetaClass>();
    if (componentType.getSuperClass() != null) {
      result.putAll(aggregateDataFieldTypeMap(ctxcomponentType.getSuperClass()));
    }
    Map<String, MetaClass> dataFields = (Map<String, MetaClass>) ctx.getInjectionContext().getAttribute(
        dataFieldTypeMapName(componentType));
    if (dataFields != null) {
      result.putAll(dataFields);
    }
    return result;
  }

  
Using the given composite Template type, return the name of the map of DataField names and variable Statement instances.
  private static final String dataFieldMapName(MetaClass composite) {
    return DataFieldCodeDecorator.class.getName() + "_DATA_FIELD_MAP_" + composite.getName();
  }

  
Using the given composite Template type, return the name of the map of DataField names and variable MetaClass types.
  private static final String dataFieldTypeMapName(MetaClass composite) {
    return DataFieldCodeDecorator.class.getName() + "_DATA_FIELD_TYPE_MAP_" + composite.getName();
  }
New to GrepCode? Check out our FAQ X