Java 类com.sun.codemodel.JArray 实例源码

项目:android-modules    文件:EModuleHandler.java   
@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    if (skipGeneratedClass(element)) // I can't prevent copying @Module to generated class, so just don't process it
        return;
    holder.getGeneratedClass()._implements(ModuleObjectsShare.IModule.class);
    final JFieldVar instances = holder.getGeneratedClass().field(JMod.PRIVATE, ModuleObjectsShare.InstancesHolder.class, "instances_");

    String moduleName = element.getAnnotation(EModule.class).moduleName();
    if (Utility.isEmpty(moduleName))
        moduleName = element.asType().toString();
    holder.getInitBody().assign(instances, ModuleCodeGenerator.createModule(moduleName, holder));

    holder.getInitBody().add(ModuleCodeGenerator.moduleSetInstance(holder, refClass(ModuleObjectsShare.IModule.class), _this()));
    holder.getInitBody().directStatement("// submodules communicate via {@link " + refClass(Bus.class).fullName() + "}, so we only need to store them");

    final JArray submodules = ModuleCodeGenerator.generateSubmodulesArray(element, holder, annotationHelper, getTarget());
    if (submodules != null) {
        final JFieldVar submodulesField = holder.getGeneratedClass().field(JMod.PRIVATE, Object[].class, "submodules_");
        holder.getInitBody().assign(submodulesField, submodules);
    }
}
项目:kola    文件:ExpressionAdapter.java   
@Override
public void caseAClassArrayCreationExpression(AClassArrayCreationExpression node) {
    TypeAdapter ta = new TypeAdapter(context);
    node.getClassOrInterfaceType().apply(ta);
    int dim = node.getDim().size();
    JType type = ta.type;
    for (int i = 0; i < dim; i++) {
        type = type.array();
    }
    JArray arr = JExpr.newArray(type);
    for (PDimExpr dexp : node.getDimExpr()) {
        ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context);
        dexp.apply(ea);
        arr.addSize(ea.expr);
    }
    expr = arr;
}
项目:kola    文件:ExpressionAdapter.java   
@Override
public void caseAPrimitiveArrayCreationExpression(APrimitiveArrayCreationExpression node) {
    TypeAdapter ta = new TypeAdapter(context);
    node.getPrimitiveType().apply(ta);
    int dim = node.getDim().size();
    JType type = ta.type;
    for (int i = 0; i < dim; i++) {
        type = type.array();
    }
    JArray arr = JExpr.newArray(type);
    for (PDimExpr dexp : node.getDimExpr()) {
        ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context);
        dexp.apply(ea);
        arr.addSize(ea.expr);
    }
    expr = arr;
}
项目:android-modules    文件:ModuleCodeGenerator.java   
public static JArray generateSubmodulesArray(Element element, EComponentHolder holder, TargetAnnotationHelper annotationHelper, String annotationName) {
    final List<DeclaredType> submodules = annotationHelper.extractAnnotationClassArrayParameter(element, annotationName, "submodules");
    if (submodules != null && submodules.size() > 0) {
        final JArray submodulesArray = newArray(holder.refClass(Object.class));
        for (DeclaredType type : submodules) {
            submodulesArray.add(instantiateGeneratedClass(holder, annotationHelper, type.toString()));
        }
        return submodulesArray;
    }
    return null;
}
项目:visualakka    文件:DefaultGenerator.java   
private JExpression getActorRef(JCodeModel jm,
        ConcreticisedMethod mth, String module, String method)
        throws JClassAlreadyExistsException {

    ConcurrentHashMap<String, String> newdef = mth.getProperties();

    JClass actorT = jm.ref(module);

    JInvocation newI = JExpr._new(jm.ref(module + ".Creator"));

    JClass enm = jm.ref(module + ".MethodName");
    newI.arg(JExpr._new(jm.ref(VisualProps.class)).arg(JExpr.invoke("getPath")).
            arg(JExpr.lit(mth.supvisstrat.toString())).
            arg(JExpr.lit(mth.maxnumofretr)).arg(JExpr.lit(mth.withTimeRange)));

    newI.arg(enm.staticRef(method));

    JArray args = JExpr.newArray(jm.ref(String.class));
    for (String defValue
            : newdef.values()) {
        args.add(JExpr.lit(defValue));
    }

    newI.arg(args);

    return jm.ref(Props.class
    ).staticInvoke("create").
            arg(actorT.staticRef("class")).arg(newI);

}
项目:LRPaGe    文件:ParsingEngineGenerator.java   
private JMethod createStateActionMethod(State s){
    JMethod method = clazz.method(JMod.PRIVATE, actionsEnum,
            "getState"+stateIndex.get(s)+"Action");
    JVar lookahead = method.param(f.getNodeInterface().narrow(nodes.getVisitorInterface()), "lookahead");
    JSwitch sw = method.body()._switch(symbolGen.castTo(JExpr.invoke(lookahead, "symbol")));
    List<JExpression> expected = new ArrayList<JExpression>();
    //add a case statement for each valid lookahead symbol
    for(String symbol : nodes.getAllSymbols()){
        Action a = table.getAction(s, symbol);
        if(a != null){
            JEnumConstant actionConst = this.defineAction(a);
            sw._case(symbolGen.getRef(symbol)).body()._return(actionConst);
            expected.add(symbolGen.getSymbolObj(symbol));
        }
    }
    //the default case should return error action
    sw._default().body()._return(actionsEnum.enumConstant("ERROR"));

    JArray array = JExpr.newArray(f.getSymbolInterface());
    for(JExpression exp : expected){
        array.add(exp);
    }

    expectedStateSwitch._case(JExpr.lit(stateIndex.get(s)))
        .body()._return(array);

    return method;
}
项目:libraries    文件:SourceFactoryUtilities.java   
public static void adjust(
    final JCodeModel codeModel,
    final JFieldVar field,
    final Object value,
    final boolean isArrayNullable) {
  final ValueConverter valueConverter = new ValueConverter(codeModel);
  if (value instanceof JExpression) {
    field.init(valueConverter.convert(value));
  }
  final String typeName = field.type().isArray() ? field.type().elementType().name() : field.type().name();
  if (stringTypes.contains(typeName)) {
    if (!field.type().isArray()) {
      field.init(valueConverter.convert(value));
      return;
    }
    if (value == null && isArrayNullable) {
      field.init(JExpr._null());
      return;
    }
    final JArray newArray = JExpr.newArray(field.type().elementType());
    if (value != null) {
      final String[] values = (String[]) value;
      for (final String i : values) {
        newArray.add(valueConverter.convert(i));
      }
    }
    field.init(newArray);
    return;
  }
  if (floatTypes.contains(typeName)) {
    adjust(codeModel, field, value, isArrayNullable, valueConverter, JAVA_LANG_FLOAT, Float[].class);
    return;
  }
  if (doubleTypes.contains(typeName)) {
    adjust(codeModel, field, value, isArrayNullable, valueConverter, JAVA_LANG_DOUBLE, Double[].class);
    return;
  }
  if (shortTypes.contains(typeName)) {
    adjust(codeModel, field, value, isArrayNullable, valueConverter, JAVA_LANG_SHORT, Short[].class);
    return;
  }
  if (integerTypes.contains(typeName)) {
    adjust(codeModel, field, value, isArrayNullable, valueConverter, JAVA_LANG_INTEGER, Integer[].class);
    return;
  }
  if (longTypes.contains(typeName)) {
    adjust(codeModel, field, value, isArrayNullable, valueConverter, JAVA_LANG_LONG, Long[].class);
    return;
  }
  if (booleanTypes.contains(typeName)) {
    adjust(codeModel, field, value, isArrayNullable, valueConverter, JAVA_LANG_BOOLEAN, Boolean[].class);
    return;
  }
  if (byteTypes.contains(typeName)) {
    adjust(codeModel, field, value, isArrayNullable, valueConverter, JAVA_LANG_BYTE, Byte[].class);
    return;
  }
  if (characterTypes.contains(typeName)) {
    adjust(codeModel, field, value, isArrayNullable, valueConverter, JAVA_LANG_CHARACTER, Character[].class);
    return;
  }
  if (value == null) {
    field.init(JExpr._null());
  }
}
项目:libraries    文件:SourceFactoryUtilities.java   
public static <C> void adjust(
    final JCodeModel codeModel,
    final JFieldVar field,
    final Object value,
    final boolean isArrayNullable,
    final ValueConverter valueConverter,
    final String className,
    final Class<C[]> clazz) {
  if (!field.type().isArray()) {
    if (field.type().isPrimitive()) {
      if (value == null) {
        if (field.type().name().equals("short")) {
          return;
        }
        if (field.type().name().equals("int")) {
          field.init(JExpr.lit(0));
          return;
        }
        if (field.type().name().equals("long")) {
          field.init(JExpr.lit(0l));
          return;
        }
        if (field.type().name().equals("float")) {
          field.init(JExpr.lit(Float.NaN));
          return;
        }
        if (field.type().name().equals("double")) {
          field.init(JExpr.lit(Double.NaN));
          return;
        }
        if (field.type().name().equals("boolean")) {
          field.init(JExpr.lit(false));
          return;
        }
        if (field.type().name().equals("byte")) {
          field.init(JExpr.lit(0x0b));
          return;
        }
        if (field.type().name().equals("char")) {
          field.init(JExpr.lit(0x0c));
          return;
        }
      }
      field.init(valueConverter.convert(value));
      return;
    }
    if (value == null) {
      field.init(JExpr._null());
      return;
    }
    field.init(codeModel.ref(className).staticInvoke("valueOf").arg(valueConverter.convert(value)));
    return;
  }
  if (value == null && isArrayNullable) {
    field.init(JExpr._null());
    return;
  }
  final JArray newArray = JExpr.newArray(field.type().elementType());
  if (value != null) {
    final C[] values = clazz.cast(value);
    for (final C i : values) {
      newArray.add(
          (field.type().isArray() ? field.type().elementType().isPrimitive() : field.type().isPrimitive())
              ? valueConverter.convert(i)
              : codeModel.ref(className).staticInvoke("valueOf").arg(valueConverter.convert(i)));
    }
  }
  field.init(newArray);
}
项目:visualakka    文件:DefaultGenerator.java   
private void introduceUserCodeBlock(JDefinedClass jc, JCodeModel jm,
        ConcreticisedMethod block,
        JSwitch methodSwitch,
        JVar _ca2, JVar _ca1,
        Map<String, JMethod> virtualMethods) {

    JMethod vmethhandler = jc.method(JMod.PUBLIC,
            jm.VOID,
            block.getRefMeth().getArguments().get(0).getIdentificator());

    vmethhandler._throws(AVEInternaException.class);
    vmethhandler.javadoc().add("Virtual method handler for " + block.getMethodName() + " method.");
    vmethhandler.javadoc().addThrows(AVEInternaException.class).
            add("Throws when message has an invalid instance.");

    allMethods.put(UCB_DECLPREFIX + block.getMethodName(), methodsEnum.enumConstant(UCB_DECLPREFIX + block.getMethodName()));

    JBlock userBlockSwitch = methodSwitch._case(JExpr.ref(UCB_DECLPREFIX + block.getMethodName())).body();
    JArray mthNames = JExpr.newArray(jm.ref(String.class));

    int argId = 0;
    for (Argument arg : block.getRefMeth().getArguments()) {
        String defaultV = arg.getDefaultValue();
        userBlockSwitch.add(_ca2.invoke("add").arg(
                JExpr._new(jm.ref(Node.class))
                .arg(arg.getIdentificator())
                .arg(arg.getType())
                .arg(defaultV == null ? "" : defaultV)
                .arg(JExpr.lit(arg.isFixed()))));

        mthNames.add(JExpr.lit(arg.getIdentificator()));
        vmethargs.put(block.getMethodName() + argId, vmethhandler.param(Message.class, arg.getIdentificator()));

        argId++;
    }
    userBlockSwitch.add(_ca1.invoke("add").arg(jm.ref(Arrays.class)
            .staticInvoke("asList").arg(mthNames)));

    userBlockSwitch._break();

    virtualMethods.put(block.getMethodName(), vmethhandler);
}