Package com.sun.codemodel

Examples of com.sun.codemodel.JFieldVar


            // stop gap measure to avoid 'code too large' error in javac.
            return createQName(name);

        // [RESULT]
        // private static final QName _XYZ_NAME = new QName("uri", "local");
        JFieldVar qnameField = objectFactory.field(
            JMod.PRIVATE | JMod.STATIC | JMod.FINAL,
            QName.class,
            '_' + ei.getSqueezedName() + "_QNAME", createQName(name));

        qnameMap.put(name, qnameField);
View Full Code Here


            JExpression declaredType = JExpr.cast(cm.ref(Class.class),implType.boxify().dotclass()); // why do we have to cast?
            JClass scope=null;
            if(ei.getScope()!=null)
                scope = parent.getClazz(ei.getScope()).implRef;
            JExpression scopeClass = scope==null?JExpr._null():scope.dotclass();
            JFieldVar valField = implClass.field(JMod.PROTECTED|JMod.FINAL|JMod.STATIC,QName.class,"NAME",createQName(cm,ei.getElementName()));

            // take this opportunity to generate a constructor in the element class
            JMethod cons = implClass.constructor(JMod.PUBLIC);
            cons.body().invoke("super")
                .arg(valField)
View Full Code Here

        JClass mapType = codeModel.ref(Map.class).narrow(QName.class,String.class);
        JClass mapImpl = codeModel.ref(HashMap.class).narrow(QName.class,String.class);

        // [RESULT]
        // Map<QName,String> m = new HashMap<QName,String>();
        JFieldVar $ref = cc.implClass.field(JMod.PRIVATE,
                mapType, FIELD_NAME, JExpr._new(mapImpl) );
        $ref.annotate2(XmlAnyAttributeWriter.class);

        MethodWriter writer = cc.createMethodWriter();

        JMethod $get = writer.declareMethod( mapType, "get"+METHOD_SEED );
        $get.javadoc().append(
View Full Code Here


        if(needsValue) {
            // [RESULT]
            // final <valueType> value;
            JFieldVar $value = type.field( JMod.PRIVATE|JMod.FINAL, baseExposedType, "value" );

            // [RESULT]
            // public <valuetype> value() { return value; }
            type.method(JMod.PUBLIC, baseExposedType, "value" ).body()._return($value);
View Full Code Here

    {
      facadeClass = clientPackage._class(resourceName + BUILDERS);
    }
    annotate(facadeClass, sourceFile);

    final JFieldVar baseUriField;
    final JFieldVar requestOptionsField;
    final JExpression baseUriGetter = JExpr.invoke("getBaseUriTemplate");
    final JExpression requestOptionsGetter = JExpr.invoke("getRequestOptions");

    if (_config.isRestli2Format())
    {
      baseUriField = null;
      requestOptionsField = null;
      facadeClass._extends(BuilderBase.class);
    }
    else
    {
      // for old builder, instead of extending from RequestBuilderBase, add fields and getters in the class
      baseUriField = facadeClass.field(JMod.PRIVATE | JMod.FINAL, String.class, "_baseUriTemplate");
      requestOptionsField = facadeClass.field(JMod.PRIVATE, RestliRequestOptions.class, "_requestOptions");
      facadeClass.method(JMod.PRIVATE, String.class, "getBaseUriTemplate").body()._return(baseUriField);
      facadeClass.method(JMod.PUBLIC, RestliRequestOptions.class, "getRequestOptions").body()._return(requestOptionsField);
    }

    // make the original resource path available via a private final static variable.
    JFieldVar originalResourceField = facadeClass.field(JMod.PRIVATE | JMod.STATIC |JMod.FINAL, String.class,
                                                        "ORIGINAL_RESOURCE_PATH");
    String resourcePath = getResourcePath(resource.getPath());
    originalResourceField.init(JExpr.lit(resourcePath));

    // create reference to RestliRequestOptions.DEFAULT_OPTIONS
    final JClass restliRequestOptionsClass = getCodeModel().ref(RestliRequestOptions.class);
    JFieldRef defaultOptionsField = restliRequestOptionsClass.staticRef("DEFAULT_OPTIONS");

    if (!_config.isRestli2Format())
    {
      // same getPathComponents() logic as in RequestBuilderBase
      JMethod pathComponentsGetter = facadeClass.method(JMod.PUBLIC, String[].class, "getPathComponents");
      pathComponentsGetter.body()._return(getCodeModel().ref(URIParamUtils.class).staticInvoke("extractPathComponentsFromUriTemplate").arg(baseUriField));

      // method that expresses the following logic
      //   (requestOptions == null) ? return RestliRequestOptions.DEFAULT_OPTIONS : requestOptions;
      JMethod requestOptionsAssigner = facadeClass.method(JMod.PRIVATE | JMod.STATIC, RestliRequestOptions.class, "assignRequestOptions");
      JVar requestOptionsAssignerParam = requestOptionsAssigner.param(RestliRequestOptions.class, "requestOptions");
      JConditional requestNullCheck = requestOptionsAssigner.body()._if(requestOptionsAssignerParam.eq(JExpr._null()));
      requestNullCheck._then().block()._return(defaultOptionsField);
      requestNullCheck._else().block()._return(requestOptionsAssignerParam);
    }

    /*
    There will be 4 constructors:
      ()
      (RestliRequestOptions)
      (String)
      (String, RestliRequestOptions)
    */
    JMethod noArgConstructor = facadeClass.constructor(JMod.PUBLIC);
    JMethod requestOptionsOverrideConstructor = facadeClass.constructor(JMod.PUBLIC);
    JMethod resourceNameOverrideConstructor = facadeClass.constructor(JMod.PUBLIC);
    JMethod mainConstructor = facadeClass.constructor(JMod.PUBLIC);

    // no-argument constructor, delegates to the request options override constructor
    noArgConstructor.body().invoke(THIS).arg(defaultOptionsField);

    // request options override constructor
    JVar requestOptionsOverrideOptionsParam = requestOptionsOverrideConstructor.param(RestliRequestOptions.class, "requestOptions");

    if (_config.isRestli2Format())
    {
      requestOptionsOverrideConstructor.body().invoke(SUPER).arg(originalResourceField).arg(requestOptionsOverrideOptionsParam);
    }
    else
    {
      requestOptionsOverrideConstructor.body().assign(baseUriField, originalResourceField);
      final JInvocation requestOptionsOverrideAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(requestOptionsOverrideOptionsParam);
      requestOptionsOverrideConstructor.body().assign(requestOptionsField, requestOptionsOverrideAssignRequestOptions);
    }

    // primary resource name override constructor, delegates to the main constructor
    JVar resourceNameOverrideResourceNameParam = resourceNameOverrideConstructor.param(_stringClass, "primaryResourceName");
    resourceNameOverrideConstructor.body().invoke(THIS).arg(resourceNameOverrideResourceNameParam).arg(defaultOptionsField);

    // main constructor
    JVar mainConsResourceNameParam = mainConstructor.param(_stringClass, "primaryResourceName");
    JVar mainConsOptionsParam = mainConstructor.param(RestliRequestOptions.class, "requestOptions");

    JExpression baseUriExpr;
    if (resourcePath.contains("/"))
    {
      baseUriExpr = originalResourceField.invoke("replaceFirst").arg(JExpr.lit("[^/]*/")).arg(mainConsResourceNameParam.plus(JExpr.lit("/")));
    }
    else
    {
      baseUriExpr = mainConsResourceNameParam;
    }

    if (_config.isRestli2Format())
    {
      mainConstructor.body().invoke(SUPER).arg(baseUriExpr).arg(mainConsOptionsParam);
    }
    else
    {
      final JInvocation mainAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(mainConsOptionsParam);
      mainConstructor.body().assign(baseUriField, baseUriExpr);
      mainConstructor.body().assign(requestOptionsField, mainAssignRequestOptions);
    }

    JMethod primaryResourceGetter = facadeClass.method(JMod.PUBLIC | JMod.STATIC, String.class, "getPrimaryResource");
    primaryResourceGetter.body()._return(originalResourceField);

    List<String> pathKeys = getPathKeys(resourcePath, pathToAssocKeys);

    JClass keyTyperefClass = null;
    JClass keyClass;
    JClass keyKeyClass = null;
    JClass keyParamsClass = null;
    Class<?> resourceSchemaClass;
    Map<String, AssocKeyTypeInfo> assocKeyTypeInfos = Collections.emptyMap();
    StringArray supportsList=null;
    RestMethodSchemaArray restMethods = null;
    FinderSchemaArray finders = null;
    ResourceSchemaArray subresources = null;
    ActionSchemaArray resourceActions = null;
    ActionSchemaArray entityActions = null;

    if (resource.getCollection() != null)
    {
      resourceSchemaClass = CollectionSchema.class;
      CollectionSchema collection = resource.getCollection();
      String keyName = collection.getIdentifier().getName();
      // In case of collection with a simple key, return the one specified by "type" in
      // the "identifier". Otherwise, get both "type" and "params", and return
      // ComplexKeyResource parameterized by those two.
      if (collection.getIdentifier().getParams() == null)
      {
        keyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
        JClass declaredClass = getClassRefForSchema(RestSpecCodec.textToSchema(collection.getIdentifier().getType(), getSchemaResolver()), facadeClass);
        if(!declaredClass.equals(keyClass))
        {
          keyTyperefClass = declaredClass;
        }
      }
      else
      {
        keyKeyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
        keyParamsClass = getJavaBindingType(collection.getIdentifier().getParams(), facadeClass).valueClass;
        keyClass = getCodeModel().ref(ComplexResourceKey.class).narrow(keyKeyClass, keyParamsClass);
      }
      pathKeyTypes.put(keyName, keyClass);
      supportsList = collection.getSupports();
      restMethods = collection.getMethods();
      finders = collection.getFinders();
      subresources = collection.getEntity().getSubresources();
      resourceActions = collection.getActions();
      entityActions = collection.getEntity().getActions();
    }
    else if (resource.getAssociation() != null)
    {
      resourceSchemaClass = AssociationSchema.class;
      AssociationSchema association = resource.getAssociation();
      keyClass = getCodeModel().ref(CompoundKey.class);
      supportsList = association.getSupports();
      restMethods = association.getMethods();
      finders = association.getFinders();
      subresources = association.getEntity().getSubresources();
      resourceActions = association.getActions();
      entityActions = association.getEntity().getActions();

      assocKeyTypeInfos = generateAssociationKey(facadeClass, association);

      String keyName = getAssociationKey(resource, association);
      pathKeyTypes.put(keyName, keyClass);

      List<String> assocKeys = new ArrayList<String>(4);
      for(Map.Entry<String, AssocKeyTypeInfo> entry: assocKeyTypeInfos.entrySet())
      {
        assocKeys.add(entry.getKey());
        assocKeyTypes.put(entry.getKey(), entry.getValue().getBindingType());
      }
      pathToAssocKeys.put(keyName, assocKeys);
    }
    else if (resource.getSimple() != null)
    {
      resourceSchemaClass = SimpleSchema.class;
      SimpleSchema simpleSchema = resource.getSimple();
      keyClass = _voidClass;
      supportsList = simpleSchema.getSupports();
      restMethods = simpleSchema.getMethods();
      subresources = simpleSchema.getEntity().getSubresources();
      resourceActions = simpleSchema.getActions();
    }
    else if (resource.getActionsSet() != null)
    {
      resourceSchemaClass = ActionsSetSchema.class;
      ActionsSetSchema actionsSet = resource.getActionsSet();
      resourceActions = actionsSet.getActions();

      keyClass = _voidClass;
    }
    else
    {
      throw new IllegalArgumentException("unsupported resource type for resource: '" + resourceName + '\'');
    }

    generateOptions(facadeClass, baseUriGetter, requestOptionsGetter);

    JFieldVar resourceSpecField = facadeClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, _resourceSpecClass, "_resourceSpec");
    if (resourceSchemaClass == CollectionSchema.class ||
        resourceSchemaClass == AssociationSchema.class ||
        resourceSchemaClass == SimpleSchema.class)
    {
      JClass schemaClass = getJavaBindingType(resource.getSchema(), null).schemaClass;
View Full Code Here

  {
    for (String pathKey : pathKeys)
    {
      if (pathToAssocKeys.get(pathKey) != null)
      {
        JFieldVar compoundKey = builderClass.field(JMod.PRIVATE,
                                                   CompoundKey.class,
                                                   RestLiToolsUtils.nameCamelCase(pathKey),
                                                   JExpr._new(pathKeyTypes.get(pathKey)));

        for (String assocKeyName : pathToAssocKeys.get(pathKey))
        {
          JMethod assocKeyMethod = builderClass.method(JMod.PUBLIC, builderClass, RestLiToolsUtils.nameCamelCase(
              assocKeyName + "Key"));
          JVar assocKeyMethodParam = assocKeyMethod.param(assocKeyTypes.get(assocKeyName), "key");

          // put stuff into CompoundKey
          assocKeyMethod.body().add(compoundKey.invoke("append").arg(assocKeyName).arg(assocKeyMethodParam));
          // old and new super.pathKey()s
          assocKeyMethod.body().add(JExpr._super().invoke("pathKey").arg(assocKeyName).arg(assocKeyMethodParam));
          assocKeyMethod.body().add(JExpr._super().invoke("pathKey").arg(pathKey).arg(compoundKey)); // new...
          assocKeyMethod.body()._return(JExpr._this());
        }
View Full Code Here

    registerGeneratedClass(typerefDataSchema, unionClass);

    JDefinedClass typerefInfoClass = unionClass._class(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, "UnionTyperefInfo");
    generateTyperef(typerefDataSchema, typerefInfoClass);

    JFieldVar typerefInfoField = unionClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL,
                                                  TyperefInfo.class,
                                                  DataTemplateUtil.TYPEREFINFO_FIELD_NAME);
    typerefInfoField.init(JExpr._new(typerefInfoClass));

    unionClass._implements(HasTyperefInfo.class);
    JMethod typerefInfoMethod = unionClass.method(JMod.PUBLIC, TyperefInfo.class, "typerefInfo");
    typerefInfoMethod.body()._return(typerefInfoField);
View Full Code Here

    String wrappedOrDirect = isDirect? "Direct" : "Wrapped";
    String memberKey = memberType.getUnionMemberKey();
    String capitalizedName = memberName(memberType);

    String memberFieldName = "MEMBER_" + capitalizedName;
    JFieldVar memberField = unionClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, DataSchema.class, memberFieldName);
    memberField.init(schemaField.invoke("getType").arg(memberKey));
    String setterName = "set" + capitalizedName;

    // Generate builder.

    JMethod createMethod = unionClass.method(JMod.PUBLIC | JMod.STATIC, unionClass, "create");
View Full Code Here

      handleSchema(includedSchema);
    }

    generatePathSpecMethodsForRecord(templateClass, schema);

    JFieldVar schemaField = generateSchemaField(templateClass, schema);
    generateConstructorWithNoArg(templateClass, schemaField, _dataMapClass);
    generateConstructorWithArg(templateClass, schemaField, _dataMapClass);

    Map<CustomInfo, Object> customInfoMap = new IdentityHashMap<CustomInfo, Object>(schema.getFields().size() * 2);

 
View Full Code Here

    return fieldsNestedClass;
  }

  private JFieldVar generateSchemaField(JDefinedClass templateClass, DataSchema schema)
  {
    JFieldVar schemaField = templateClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL,
                                                schema.getClass(),
                                                DataTemplateUtil.SCHEMA_FIELD_NAME);
    String schemaJson = SchemaToJsonEncoder.schemaToJson(schema, JsonBuilder.Pretty.COMPACT);
    JInvocation parseSchemaInvocation;
    if (schemaJson.length() < MAX_SCHEMA_JSON_LENGTH)
    {
      parseSchemaInvocation = _dataTemplateUtilClass.staticInvoke("parseSchema").arg(schemaJson);
    }
    else
    {
      JInvocation stringBuilderInvocation = JExpr._new(_stringBuilderClass);
      for (int index = 0; index < schemaJson.length(); index += MAX_SCHEMA_JSON_LENGTH)
      {
        stringBuilderInvocation = stringBuilderInvocation.
          invoke("append").
          arg(schemaJson.substring(index, Math.min(schemaJson.length(), index + MAX_SCHEMA_JSON_LENGTH)));
      }
      stringBuilderInvocation = stringBuilderInvocation.invoke("toString");
      parseSchemaInvocation = _dataTemplateUtilClass.staticInvoke("parseSchema").arg(stringBuilderInvocation);
    }
    schemaField.init(JExpr.cast(getCodeModel()._ref(schema.getClass()),
                                parseSchemaInvocation));

    return schemaField;
  }
View Full Code Here

TOP

Related Classes of com.sun.codemodel.JFieldVar

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.