Java 类com.sun.codemodel.internal.JExpression 实例源码

项目:OpenJSharp    文件:IsSetField.java   
private void generate( ClassOutlineImpl outline, CPropertyInfo prop ) {
    // add isSetXXX and unsetXXX.
    MethodWriter writer = outline.createMethodWriter();

    JCodeModel codeModel = outline.parent().getCodeModel();

    FieldAccessor acc = core.create(JExpr._this());

    if( generateIsSetMethod ) {
        // [RESULT] boolean isSetXXX()
        JExpression hasSetValue = acc.hasSetValue();
        if( hasSetValue==null ) {
            // this field renderer doesn't support the isSet/unset methods generation.
            // issue an error
            throw new UnsupportedOperationException();
        }
        writer.declareMethod(codeModel.BOOLEAN,"isSet"+this.prop.getName(true))
            .body()._return( hasSetValue );
    }

    if( generateUnSetMethod ) {
        // [RESULT] void unsetXXX()
        acc.unsetValues(
            writer.declareMethod(codeModel.VOID,"unset"+this.prop.getName(true)).body() );
    }
}
项目:OpenJSharp    文件:ObjectFactoryGeneratorImpl.java   
/**
 * return a JFieldVar that represents the QName field for the given information.
 *
 * if it doesn't exist, create a static field in the class and store a new JFieldVar.
 */
private JExpression getQNameInvocation(CElementInfo ei) {
    QName name = ei.getElementName();
    if(qnameMap.containsKey(name)) {
        return qnameMap.get(name);
    }

    if(qnameMap.size()>1024)
        // 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);

    return qnameField;
}
项目:OpenJSharp    文件:ElementCollectionAdapter.java   
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
    JCodeModel cm = outline().getCodeModel();
    JClass elementType = ei.toType(outline(),EXPOSED).boxify();

    // [RESULT]
    // $t = new ArrayList();
    // for( Type e : $var ) {
    //     $var.add(new JAXBElement(e));
    // }
    // [core.fromRawValue]

    JClass col = cm.ref(ArrayList.class).narrow(elementType);
    JVar $t = block.decl(col,uniqueName+"_col",JExpr._new(col));

    JForEach loop = block.forEach(itemType(), uniqueName+"_i", $t);
    loop.body().invoke($var,"add").arg(createJAXBElement(loop.var()));

    acc.fromRawValue(block, uniqueName, $t);
}
项目:OpenJSharp    文件:TypeUseImpl.java   
public JExpression createConstant(Outline outline, XmlString lexical) {
    if(isCollection())  return null;

    if(adapter==null)     return coreType.createConstant(outline, lexical);

    // [RESULT] new Adapter().unmarshal(CONSTANT);
    JExpression cons = coreType.createConstant(outline, lexical);
    Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();

    // try to run the adapter now rather than later.
    if(cons instanceof JStringLiteral && atype!=null) {
        JStringLiteral scons = (JStringLiteral) cons;
        XmlAdapter a = ClassFactory.create(atype);
        try {
            Object value = a.unmarshal(scons.str);
            if(value instanceof String) {
                return JExpr.lit((String)value);
            }
        } catch (Exception e) {
            // assume that we can't eagerly bind this
        }
    }

    return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
}
项目:openjdk-jdk10    文件:IsSetField.java   
private void generate( ClassOutlineImpl outline, CPropertyInfo prop ) {
    // add isSetXXX and unsetXXX.
    MethodWriter writer = outline.createMethodWriter();

    JCodeModel codeModel = outline.parent().getCodeModel();

    FieldAccessor acc = core.create(JExpr._this());

    if( generateIsSetMethod ) {
        // [RESULT] boolean isSetXXX()
        JExpression hasSetValue = acc.hasSetValue();
        if( hasSetValue==null ) {
            // this field renderer doesn't support the isSet/unset methods generation.
            // issue an error
            throw new UnsupportedOperationException();
        }
        writer.declareMethod(codeModel.BOOLEAN,"isSet"+this.prop.getName(true))
            .body()._return( hasSetValue );
    }

    if( generateUnSetMethod ) {
        // [RESULT] void unsetXXX()
        acc.unsetValues(
            writer.declareMethod(codeModel.VOID,"unset"+this.prop.getName(true)).body() );
    }
}
项目:OpenJSharp    文件:WhitespaceNormalizer.java   
public JExpression generate( JCodeModel codeModel, JExpression literal ) {
    // WhitespaceProcessor.replace(<literal>);
    if( literal instanceof JStringLiteral )
        // optimize
        return JExpr.lit( WhiteSpaceProcessor.replace(((JStringLiteral)literal).str) );
    else
        return codeModel.ref(WhiteSpaceProcessor.class)
            .staticInvoke("replace").arg(literal);
}
项目:OpenJSharp    文件:AbstractField.java   
/**
 * Case from {@link #exposedType} to {@link #implType} if necessary.
 */
protected final JExpression castToImplType( JExpression exp ) {
    if(implType==exposedType)
        return exp;
    else
        return JExpr.cast(implType,exp);
}
项目:OpenJSharp    文件:UnboxedField.java   
public FieldAccessor create(JExpression targetObject) {
    return new Accessor(targetObject) {

        public void unsetValues( JBlock body ) {
            // you can't unset a value
        }

        public JExpression hasSetValue() {
            return JExpr.TRUE;
        }
    };
}
项目:openjdk-jdk10    文件:CEnumLeafInfo.java   
public JExpression createConstant(Outline outline, XmlString literal) {
    // correctly identifying which constant it maps to is hard, so
    // here I'm cheating
    JClass type = toType(outline,Aspect.EXPOSED);
    for (CEnumConstant mem : members) {
        if(mem.getLexicalValue().equals(literal.value))
            return type.staticRef(mem.getName());
    }
    return null;
}
项目:openjdk-jdk10    文件:CDefaultValue.java   
/**
 * Creates a new {@link CDefaultValue} that computes the default value
 * by applying a lexical representation to a {@link TypeUse}.
 */
public static CDefaultValue create(final TypeUse typeUse, final XmlString defaultValue) {
    return new CDefaultValue() {
        public JExpression compute(Outline outline) {
            return typeUse.createConstant(outline,defaultValue);
        }
    };
}
项目:openjdk-jdk10    文件:ElementOutlineImpl.java   
ElementOutlineImpl(BeanGenerator parent, CElementInfo ei) {
    super(ei,
          parent.getClassFactory().createClass(
                  parent.getContainer( ei.parent, Aspect.EXPOSED ), ei.shortName(), ei.getLocator() ));
    this.parent = parent;
    parent.elements.put(ei,this);

    JCodeModel cm = parent.getCodeModel();

    implClass._extends(
        cm.ref(JAXBElement.class).narrow(
            target.getContentInMemoryType().toType(parent,Aspect.EXPOSED).boxify()));

    if(ei.hasClass()) {
        JType implType = ei.getContentInMemoryType().toType(parent,Aspect.IMPLEMENTATION);
        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)
            .arg(declaredType)
            .arg(scopeClass)
            .arg(cons.param(implType,"value"));

        // generate no-arg constructor in the element class (bug #391; section 5.6.2 in JAXB spec 2.1)
        JMethod noArgCons = implClass.constructor(JMod.PUBLIC);
        noArgCons.body().invoke("super")
            .arg(valField)
            .arg(declaredType)
            .arg(scopeClass)
            .arg(JExpr._null());

    }
}
项目:OpenJSharp    文件:CDefaultValue.java   
/**
 * Creates a new {@link CDefaultValue} that computes the default value
 * by applying a lexical representation to a {@link TypeUse}.
 */
public static CDefaultValue create(final TypeUse typeUse, final XmlString defaultValue) {
    return new CDefaultValue() {
        public JExpression compute(Outline outline) {
            return typeUse.createConstant(outline,defaultValue);
        }
    };
}
项目:openjdk-jdk10    文件:WhitespaceNormalizer.java   
public JExpression generate( JCodeModel codeModel, JExpression literal ) {
    // WhitespaceProcessor.replace(<literal>);
    if( literal instanceof JStringLiteral )
        // optimize
        return JExpr.lit( WhiteSpaceProcessor.replace(((JStringLiteral)literal).str) );
    else
        return codeModel.ref(WhiteSpaceProcessor.class)
            .staticInvoke("replace").arg(literal);
}
项目:openjdk-jdk10    文件:WhitespaceNormalizer.java   
public JExpression generate( JCodeModel codeModel, JExpression literal ) {
    // WhitespaceProcessor.replace(<literal>);
    if( literal instanceof JStringLiteral )
        // optimize
        return JExpr.lit( WhiteSpaceProcessor.collapse(((JStringLiteral)literal).str) );
    else
        return codeModel.ref(WhiteSpaceProcessor.class)
            .staticInvoke("collapse").arg(literal);
}
项目:openjdk-jdk10    文件:AbstractField.java   
/**
 * Case from {@link #exposedType} to {@link #implType} if necessary.
 */
protected final JExpression castToImplType( JExpression exp ) {
    if(implType==exposedType)
        return exp;
    else
        return JExpr.cast(implType,exp);
}
项目:openjdk-jdk10    文件:ElementAdapter.java   
/**
 * Wraps a type value into a {@link JAXBElement}.
 */
protected final JInvocation createJAXBElement(JExpression $var) {
    JCodeModel cm = codeModel();

    return JExpr._new(cm.ref(JAXBElement.class))
        .arg(JExpr._new(cm.ref(QName.class))
            .arg(ei.getElementName().getNamespaceURI())
            .arg(ei.getElementName().getLocalPart()))
        .arg(getRawType().boxify().erasure().dotclass())
        .arg($var);
}
项目:openjdk-jdk10    文件:NoExtendedContentField.java   
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
    // [RESULT]
    // bean.getLIST().addAll($<var>);
    JVar $list = block.decl(listT,uniqueName+'l',$target.invoke($get));
    block.invoke($list,"addAll").arg($var);
}
项目:OpenJSharp    文件:WhitespaceNormalizer.java   
public JExpression generate( JCodeModel codeModel, JExpression literal ) {
    return literal;
}
项目:openjdk-jdk10    文件:UntypedListField.java   
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
    // [RESULT]
    // bean.getLIST().addAll($<var>);
    JVar $list = block.decl(listT,uniqueName+'l',$target.invoke($get));
    block.invoke($list,"addAll").arg($var);
}
项目:OpenJSharp    文件:IsSetField.java   
public FieldAccessor create(JExpression targetObject) {
    return new Accessor(targetObject);
}
项目:OpenJSharp    文件:IsSetField.java   
Accessor( JExpression $target ) {
    super($target);
    this.core = IsSetField.this.core.create($target);
}
项目:openjdk-jdk10    文件:SingleField.java   
public JExpression hasSetValue() {
    return $ref.ne( JExpr._null() );
}
项目:openjdk-jdk10    文件:UntypedListField.java   
public Accessor create(JExpression targetObject) {
    return new Accessor(targetObject);
}
项目:openjdk-jdk10    文件:UntypedListField.java   
protected Accessor( JExpression $target ) {
    super($target);
}
项目:OpenJSharp    文件:ArrayField.java   
protected Accessor( JExpression $target ) {
    super($target);
}
项目:OpenJSharp    文件:ArrayField.java   
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
    block.invoke($target,$setAll).arg($var);
}
项目:OpenJSharp    文件:ArrayField.java   
@Override
public JExpression hasSetValue() {
    return field.ne(JExpr._null()).cand(field.ref("length").gt(JExpr.lit(0)));
}
项目:OpenJSharp    文件:ArrayField.java   
public Accessor create(JExpression targetObject) {
    return new Accessor(targetObject);
}
项目:OpenJSharp    文件:ArrayField.java   
/**
 * Case from {@link #exposedType} to array of {@link #implType} .
 */
protected final JExpression castToImplTypeArray( JExpression exp ) {
    return JExpr.cast(implType.array(), exp);
}
项目:OpenJSharp    文件:NoExtendedContentField.java   
protected Accessor( JExpression $target ) {
    super($target);
}
项目:OpenJSharp    文件:NoExtendedContentField.java   
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
    // [RESULT]
    // bean.getLIST().addAll($<var>);
    JVar $list = block.decl(listT,uniqueName+'l',$target.invoke($get));
    block.invoke($list,"addAll").arg($var);
}
项目:openjdk-jdk10    文件:ConstField.java   
Accessor( JExpression $target ) {
    super($target);
}
项目:OpenJSharp    文件:DummyListField.java   
protected Accessor( JExpression $target ) {
    super($target);
}
项目:openjdk-jdk10    文件:DummyListField.java   
protected Accessor( JExpression $target ) {
    super($target);
}
项目:openjdk-jdk10    文件:CBuiltinLeafInfo.java   
public JExpression createConstant(Outline outline, XmlString lexical) {
    return JExpr.lit(DatatypeConverter.parseBoolean(lexical.value));
}
项目:OpenJSharp    文件:AbstractListField.java   
protected Accessor( JExpression $target ) {
    super($target);
    field = $target.ref(AbstractListField.this.field);
}
项目:OpenJSharp    文件:AbstractListField.java   
protected final JExpression box( JExpression exp ) {
    if(primitiveType==null) return exp;
    else                    return primitiveType.wrap(exp);
}
项目:OpenJSharp    文件:AbstractListField.java   
public JExpression count() {
    return JOp.cond( field.eq(JExpr._null()), JExpr.lit(0), field.invoke("size") );
}
项目:openjdk-jdk10    文件:ConstField.java   
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
    throw new UnsupportedOperationException();
}
项目:OpenJSharp    文件:UntypedListField.java   
public Accessor create(JExpression targetObject) {
    return new Accessor(targetObject);
}