Package com.sun.codemodel

Examples of com.sun.codemodel.JFieldVar


            Field field = property.getField();

            if (!isPrivate(field)) {
                propertyVar.init(beanVar.ref(field.getName()));
            } else {
                JFieldVar fieldAccessorField = builder.getPrivateFieldAccessor(field);

                String methodName;
                if (Boolean.TYPE.equals(propertyType)) {
                    methodName = "getBoolean";
                } else if (Byte.TYPE.equals(propertyType)) {
                    methodName = "getByte";
                } else if (Character.TYPE.equals(propertyType)) {
                    methodName = "getChar";
                } else if (Short.TYPE.equals(propertyType)) {
                    methodName = "getShort";
                } else if (Integer.TYPE.equals(propertyType)) {
                    methodName = "getInt";
                } else if (Long.TYPE.equals(propertyType)) {
                    methodName = "getLong";
                } else if (Float.TYPE.equals(propertyType)) {
                    methodName = "getFloat";
                } else if (Double.TYPE.equals(propertyType)) {
                    methodName = "getDouble";
                } else {
                    methodName = "getObject";
                }

                propertyVar.init(fieldAccessorField.invoke(methodName).arg(beanVar).arg(builder.getWriteContextVar()).arg(beanVar));
            }
        } else if (property.getGetter() != null) {
            Method getter = property.getGetter();
            if (!isPrivate(getter)) {
                propertyVar.init(JExpr._null());

                JTryBlock tryGetter = block._try();
                tryGetter.body().assign(propertyVar, beanVar.invoke(getter.getName()));

                JCatchBlock catchException = tryGetter._catch(context.toJClass(Exception.class));
                catchException.body().invoke(builder.getReadContextVar(), "getterError")
                        .arg(beanVar)
                        .arg(property.getName())
                        .arg(context.dotclass(property.getBean().getType()))
                        .arg(getter.getName())
                        .arg(catchException.param("e"));
            } else {
                JFieldVar propertyAccessorField = builder.getPrivatePropertyAccessor(property.getGetter(), property.getSetter(), property.getName());
                propertyVar.init(propertyAccessorField.invoke("getObject").arg(beanVar).arg(builder.getWriteContextVar()).arg(beanVar));
            }
        } else {
            throw new BuildException("Property does not have a getter " + property.getBean().getClass().getName() + "." + property.getName());
        }
View Full Code Here


        return writerBuilder;
    }

    public JVar getAdapter(Class adapterType) {
        String adapterId = adapterType.getName();
        JFieldVar var = adapters.get(adapterId);
        if (var == null) {
            String fieldName = decapitalize(adapterType.getSimpleName()) + "Adapter";
            fieldName = fieldManager.createId(fieldName);
            JClass jClass = builderContext.toJClass(adapterType);
            var = jaxbObjectClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, jClass, fieldName, JExpr._new(jClass));
View Full Code Here

        return var;
    }

    public JFieldVar getPrivateFieldAccessor(Field field) {
        String fieldId = field.getDeclaringClass().getSimpleName() + "." + field.getName();
        JFieldVar fieldAccessorField = privateFieldAccessors.get(fieldId);
        if (fieldAccessorField == null) {
            JClass fieldAccessorType = builderContext.toJClass(FieldAccessor.class).narrow(builderContext.toJClass(field.getDeclaringClass()), builderContext.getGenericType(field.getGenericType()));
            JInvocation newFieldAccessor = JExpr._new(fieldAccessorType)
                    .arg(builderContext.getCodeModel().ref(field.getDeclaringClass()).staticRef("class"))
                    .arg(JExpr.lit(field.getName()));
View Full Code Here

    public JFieldVar getPrivatePropertyAccessor(Method getter, Method setter, String propertyName) {
        Class beanClass = getter != null ? getter.getDeclaringClass() : setter.getDeclaringClass();
        Type propertyType = getter != null ? getter.getGenericReturnType() : setter.getGenericParameterTypes()[0];

        String fieldId = beanClass.getSimpleName() + "." + propertyName;
        JFieldVar fieldAccessorField = privatePropertyAccessors.get(fieldId);
        if (fieldAccessorField == null) {
            JClass fieldAccessorType = builderContext.toJClass(PropertyAccessor.class).narrow(builderContext.toJClass(beanClass), builderContext.getGenericType(propertyType));
            JInvocation newFieldAccessor = JExpr._new(fieldAccessorType)
                    .arg(builderContext.dotclass(beanClass))
                    .arg(builderContext.dotclass(propertyType))
View Full Code Here

        } catch (JClassAlreadyExistsException e) {
            throw new BuildException(e);
        }

        // INSTANCE variable
        JFieldVar instanceVar = jaxbEnumClass.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, jaxbEnumClass, "INSTANCE", JExpr._new(jaxbEnumClass));

        // constructor
        JMethod constructor = jaxbEnumClass.constructor(JMod.PUBLIC);
        constructor.body().invoke("super")
                .arg(JExpr.dotclass(builderContext.toJClass(type)))
                .arg(newQName(xmlRootElement))
                .arg(newQName(xmlType));

        // static parse
        JMethod staticParse = jaxbEnumClass.method(JMod.PUBLIC | JMod.STATIC, type, "parse" + type.getSimpleName())._throws(Exception.class);
        JVar xsrVar = staticParse.param(XoXMLStreamReader.class, "reader");
        JVar contextVar = staticParse.param(builderContext.toJClass(RuntimeContext.class), "context");
        JVar value = staticParse.param(String.class, "value");
        staticParse.body()._return(instanceVar.invoke("parse").arg(xsrVar).arg(contextVar).arg(value));

        // static toString
        JMethod staticToString = jaxbEnumClass.method(JMod.PUBLIC | JMod.STATIC, String.class, "toString" + type.getSimpleName())._throws(Exception.class);
        JVar beanVar = staticToString.param(Object.class, "bean");
        JVar parameterNameVar = staticToString.param(String.class, "parameterName");
        contextVar = staticToString.param(builderContext.toJClass(RuntimeContext.class), "context");
        value = staticToString.param(type, decapitalize(type.getSimpleName()));
        staticToString.body()._return(instanceVar.invoke("toString").arg(beanVar).arg(parameterNameVar).arg(contextVar).arg(value));

        // instance parse
        parseMethod = jaxbEnumClass.method(JMod.PUBLIC, type, "parse")._throws(Exception.class);
        parseXSR = parseMethod.param(XoXMLStreamReader.class, "reader");
        parseContext = parseMethod.param(builderContext.toJClass(RuntimeContext.class), "context");
View Full Code Here

    return testClass;
  }

  protected void createVersionsDataPoint( @Nonnull JDefinedClass testClass, @Nonnull JClass serializerClass, @Nonnull JClass domainType, @Nonnull DomainObjectDescriptor domainObjectDescriptor ) {
    JFieldVar field = testClass.field( JMod.STATIC | JMod.PUBLIC | JMod.FINAL, codeGenerator.ref( VersionEntry.class ), DATA_POINT_FIELD_NAME );
    field.annotate( codeGenerator.ref( "org.junit.experimental.theories.DataPoint" ) );

    JInvocation versionInvocation = codeGenerator.ref( Version.class ).staticInvoke( METHOD_NAME_VALUE_OF ).arg( JExpr.lit( 1 ) ).arg( JExpr.lit( 0 ) ).arg( JExpr.lit( 0 ) );
    JExpression expected = createExpectedExpression( testClass, domainType );

    field.init( testClass.staticInvoke( METHOD_NAME_CREATE ).arg( versionInvocation ).arg( expected ) );
  }
View Full Code Here

    return testClass;
  }

  protected void createDataPoint( @Nonnull JDefinedClass testClass, @Nonnull JClass serializerClass, @Nonnull JClass domainType, @Nonnull DomainObjectDescriptor domainObjectDescriptor ) {
    JFieldVar field = testClass.field( JMod.STATIC | JMod.PUBLIC | JMod.FINAL, codeGenerator.ref( Entry.class ).narrow( domainType.wildcard() ), DATA_POINT_FIELD_NAME );
    field.annotate( codeModel.ref( "org.junit.experimental.theories.DataPoint" ) );

    JInvocation domainObjectCreation = createDomainObjectCreationExpression( domainObjectDescriptor );
    JExpression expected = createExpectedExpression( testClass, domainType );

    field.init( testClass.staticInvoke( METHOD_NAME_CREATE ).arg( domainObjectCreation ).arg( expected ) );
  }
View Full Code Here

  @Override
  @Nonnull
  public JInvocation createAddToSerializeToExpression( @Nonnull AbstractGenerator<?> generator, @Nonnull JDefinedClass serializerClass, @Nonnull JExpression serializeTo, @Nonnull FieldDeclarationInfo fieldInfo, @Nonnull JVar object, JVar formatVersion ) {
    generator.addDelegatingSerializerToConstructor( serializerClass, codeGenerator.ref( TypeUtils.getErasure( fieldInfo.getCollectionParam() ).toString() ) );

    JFieldVar constant = getConstant( serializerClass, fieldInfo );

    JInvocation getterInvocation = codeGenerator.createGetterInvocation( object, fieldInfo );

    return JExpr.invoke( METHOD_NAME_SERIALIZE_COLLECTION )
      .arg( getterInvocation )
View Full Code Here

  @Override
  public JStatement createAddToSerializeToExpression( @Nonnull AbstractGenerator<?> generator, @Nonnull JDefinedClass serializerClass, @Nonnull JExpression serializeTo, @Nonnull FieldDeclarationInfo fieldInfo, @Nonnull JVar object, JVar formatVersion ) {
    //Add serializer to constructor
    generator.addDelegatingSerializerToConstructor( serializerClass, codeGenerator.ref( TypeUtils.getErasure( fieldInfo.getType() ).toString() ) );

    JFieldVar constant = getConstant( serializerClass, fieldInfo );

    JExpression getterInvocation = codeGenerator.createGetterInvocation( object, fieldInfo );

    return JExpr.invoke( METHOD_NAME_SERIALIZE )
      .arg( getterInvocation )
View Full Code Here

  }

  @Override
  @Nonnull
  public JInvocation createAddToSerializeToExpression( @Nonnull AbstractGenerator<?> generator, @Nonnull JDefinedClass serializerClass, @Nonnull JExpression serializeTo, @Nonnull FieldDeclarationInfo fieldInfo, @Nonnull JVar object, JVar formatVersion ) {
    JFieldVar constant = getConstant( serializerClass, fieldInfo );

    JExpression objectAsString = codeGenerator.getParseExpressionFactory().createToStringExpression( codeGenerator.createGetterInvocation( object, fieldInfo ), fieldInfo );

    return serializeTo.invoke( METHOD_NAME_ADD_ELEMENT_WITH_CHARACTERS )
      .arg( serializeTo.invoke( METHOD_NAME_GET_NAMESPACE ) )
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.