本文整理匯總了Java中com.sun.codemodel.JFieldVar.init方法的典型用法代碼示例。如果您正苦於以下問題:Java JFieldVar.init方法的具體用法?Java JFieldVar.init怎麽用?Java JFieldVar.init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.codemodel.JFieldVar
的用法示例。
在下文中一共展示了JFieldVar.init方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: apply
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* Default values are implemented by assigning an expression to the given
* field (so when instances of the generated POJO are created, its fields
* will then contain their default values).
* <p>
* Collections (Lists and Sets) are initialized to an empty collection, even
* when no default value is present in the schema (node is null).
*
* @param nodeName
* the name of the property which has (or may have) a default
* @param node
* the default node (may be null if no default node was present
* for this property)
* @param field
* the Java field that has added to a generated type to represent
* this property
* @return field, which will have an init expression is appropriate
*/
@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
boolean defaultPresent = node != null && isNotEmpty(node.asText());
String fieldType = field.type().fullName();
if (defaultPresent && !field.type().isPrimitive() && node.isNull()) {
field.init(JExpr._null());
} else if (fieldType.startsWith(List.class.getName())) {
field.init(getDefaultList(field.type(), node));
} else if (fieldType.startsWith(Set.class.getName())) {
field.init(getDefaultSet(field.type(), node));
} else if (fieldType.startsWith(String.class.getName()) && node != null ) {
field.init(getDefaultValue(field.type(), node));
} else if (defaultPresent) {
field.init(getDefaultValue(field.type(), node));
}
return field;
}
示例2: listMember
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
private JFieldVar listMember(
final JDefinedClass instance,
final JType clazz,
final String name,
final String[] generics,
final boolean isNullable,
final boolean isCollectionNullable) {
final JFieldVar field = instance.field(JMod.FINAL | JMod.PRIVATE, clazz, name);
if (isNullable && isCollectionNullable) {
return field;
}
final JType type = withoutGenerics(clazz.fullName()).startsWith(JAVA_UTIL_LIST)
? _type(JAVA_UTIL_ARRAYLIST, generics)
: clazz;
field.init(JExpr._new(type));
return field;
}
示例3: addDiscriminator
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
private void addDiscriminator(JDefinedClass klass, ObjectTypeDeclaration type) {
if (type.discriminator() == null) {
return;
}
String discriminatorValue = type.discriminatorValue();
if (discriminatorValue == null) {
discriminatorValue = type.name();
}
JFieldVar field = klass.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL,
codeModel._ref(String.class), DISCRIMINATOR);
field.init(JExpr.lit(discriminatorValue));
if (context.getConfig().isDiscriminatorMutable()) {
klass.constructor(JMod.PUBLIC).body().invoke(getSetterName(type.discriminator()))
.arg(field);
}
else {
generateDiscriminatorGetter(type, klass, type.discriminator());
}
}
示例4: testClassWithPersistenceContextWithKonfiguredUnitNameSpecified
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
@Test
public void testClassWithPersistenceContextWithKonfiguredUnitNameSpecified() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class);
jAnnotation.param("unitName", "test-unit-1");
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
final BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(cut);
final RunListener listener = mock(RunListener.class);
final RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
// WHEN
runner.run(notifier);
// THEN
final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class);
verify(listener).testStarted(descriptionCaptor.capture());
assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
verify(listener).testFinished(descriptionCaptor.capture());
assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
}
示例5: renderConstantsClass
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
private void renderConstantsClass(JDefinedClass classModel) throws Exception {
// define constants class
JDefinedClass constantsClass = classModel._class(JMod.STATIC, Util.CONSTANTS_CLASS_NAME);
// generate the javadoc on the top of the Constants class
JDocComment javadoc = constantsClass.javadoc();
javadoc.append(Util.CONSTANTS_CLASS_JAVADOC);
// render root element name
JFieldVar rootElementField = constantsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.ROOT_ELEMENT_NAME_FIELD);
rootElementField.init(JExpr.lit(Util.toLowerCaseFirstLetter(classModel.name())));
// render type name
JFieldVar typeNameField = constantsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.TYPE_NAME_FIELD);
typeNameField.init(JExpr.lit(classModel.name() + Util.TYPE_NAME_SUFFIX));
}
示例6: renderElementsClass
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
private void renderElementsClass(JDefinedClass classModel, List<FieldModel> fields) throws Exception {
// define constants class
JDefinedClass elementsClass = classModel._class(JMod.STATIC, Util.ELEMENTS_CLASS_NAME);
// generate the javadoc on the top of the Elements class
JDocComment javadoc = elementsClass.javadoc();
javadoc.append(Util.ELEMENTS_CLASS_JAVADOC);
// go through each field and create a corresponding constant
for (FieldModel fieldModel : fields) {
if (Util.isCommonElement(fieldModel.fieldName)) {
continue;
}
JFieldVar elementFieldVar = elementsClass.field(JMod.FINAL | JMod.STATIC, String.class, Util.toConstantsVariable(fieldModel.fieldName));
elementFieldVar.init(JExpr.lit(fieldModel.fieldName));
}
}
示例7: addAttributeToClass
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
public static JFieldVar addAttributeToClass(JCodeModel codeModel, JDefinedClass definedClass, FieldMeta field) {
Object type = createTypeScaffold(codeModel, definedClass, field);
int fieldMod = attributeMod(field);
JFieldVar jfieldVar = null;
if (type instanceof Class<?>) {
jfieldVar = definedClass.field(fieldMod, (Class<?>) type, field.getName());
} else {
jfieldVar = definedClass.field(fieldMod, (JClass) type, field.getName());
}
if (!StringUtils.isEmpty(field.getInitValue())) {
jfieldVar.init(JExpr.direct(field.getInitValue()));
}
return jfieldVar;
}
示例8: addMixins
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
private void addMixins(Feed feed, JDefinedClass cls, JDefinedClass bldrCls, JVar paramBuilder) throws JClassAlreadyExistsException {
if (feed.getMixins() != null && feed.getMixins().getMixin() != null) {
List<String> mixinValues = new ArrayList<>();
for (Mixin mixin : feed.getMixins().getMixin()) {
mixinValues.add(mixin.getName());
}
String mixinEnumName = camel(feed.getName(), true) + "Mixin";
JDefinedClass mixinEnum = buildEnumClass(mixinEnumName, mixinValues);
JFieldVar field = cls.field(privateStaticFinal, String.class, "MIXIN");
field.init(JExpr.lit("mixin"));
JMethod iterWither = bldrCls.method(JMod.PUBLIC, bldrCls, "withMixins");
JVar param = iterWither.param(iterable(mixinEnum), "mixins");
JBlock mthdBody = iterWither.body();
mthdBody.add(paramBuilder.invoke("put")
.arg(field)
.arg(immutableList.staticInvoke("copyOf").arg(param)));
mthdBody._return(JExpr._this());
JMethod varArgWither = bldrCls.method(JMod.PUBLIC, bldrCls, "withMixins");
param = varArgWither.varParam(mixinEnum, "mixins");
varArgWither.body()._return(JExpr.invoke(iterWither)
.arg(immutableList.staticInvoke("copyOf").arg(param)));
}
}
示例9: addQuickLookupMap
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
private JFieldVar addQuickLookupMap(JDefinedClass _enum, JType backingType) {
JClass lookupType = _enum.owner().ref(Map.class).narrow(backingType.boxify(), _enum);
JFieldVar lookupMap = _enum.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, lookupType, "CONSTANTS");
JClass lookupImplType = _enum.owner().ref(HashMap.class).narrow(backingType.boxify(), _enum);
lookupMap.init(JExpr._new(lookupImplType));
JForEach forEach = _enum.init().forEach(_enum, "c", JExpr.invoke("values"));
JInvocation put = forEach.body().invoke(lookupMap, "put");
put.arg(forEach.var().ref("value"));
put.arg(forEach.var());
return lookupMap;
}
示例10: mapMember
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
public JFieldVar mapMember(final JFieldVar field, final JType clazz, final String[] generics) {
final JType type = withoutGenerics(clazz.fullName()).startsWith(JAVA_UTIL_MAP)
? _type(JAVA_UTIL_LINKHASHMAP, generics)
: clazz;
field.init(JExpr._new(type));
return field;
}
示例11: generateDelegator
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
/**
* Generates a delegator for the given class.
*
* @param delegateClass
* delegate class
*/
public void generateDelegator(JDefinedClass delegateClass) {
JPackage pkg = context.getDelegatorPackage();
try {
JDefinedClass delegator = pkg
._class(delegateClass.name() + context.getConfig().getDelegatorSuffix());
context.annotateAsGenerated(delegator);
JClass delegateType = delegateClass;
if (delegateClass.typeParams().length > 0) {
Stream.of(delegateClass.typeParams()).map(JTypeVar::name)
.forEach(delegator::generify);
delegateType = delegateClass.narrow(delegateClass.typeParams());
}
JFieldVar delegate = delegator.field(JMod.PROTECTED, delegateType,
context.getConfig().getDelegateFieldName());
delegate.init(JExpr._new(delegateType));
JDefinedClass klass = delegateClass;
while (klass != null) {
buildDelegatingMethods(klass, delegator, delegate);
if (klass._extends() instanceof JDefinedClass) {
klass = (JDefinedClass) klass._extends();
}
else {
klass = null;
}
}
}
catch (JClassAlreadyExistsException exc) {
throw Exceptions.unchecked(exc);
}
}
示例12: testClassWithoutPersistenceContextField
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
@Test
public void testClassWithoutPersistenceContextField() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
try {
// WHEN
new JpaUnitRule(cut);
fail("IllegalArgumentException expected");
} catch (final IllegalArgumentException e) {
// THEN
assertThat(e.getMessage(), containsString("EntityManagerFactory or EntityManager field annotated"));
}
}
示例13: testClassWithMultiplePersistenceContextFields
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
@Test
public void testClassWithMultiplePersistenceContextFields() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar em1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em1");
em1Field.annotate(PersistenceContext.class);
final JFieldVar em2Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em2");
em2Field.annotate(PersistenceContext.class);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
try {
// WHEN
new JpaUnitRule(cut);
fail("IllegalArgumentException expected");
} catch (final IllegalArgumentException e) {
// THEN
assertThat(e.getMessage(), containsString("Only single field is allowed"));
}
}
示例14: testClassWithMultiplePersistenceUnitFields
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
@Test
public void testClassWithMultiplePersistenceUnitFields() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf1");
emf1Field.annotate(PersistenceUnit.class);
final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf2");
emf2Field.annotate(PersistenceUnit.class);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
try {
// WHEN
new JpaUnitRule(cut);
fail("IllegalArgumentException expected");
} catch (final IllegalArgumentException e) {
// THEN
assertThat(e.getMessage(), containsString("Only single field is allowed"));
}
}
示例15: testClassWithPersistenceContextAndPersistenceUnitFields
import com.sun.codemodel.JFieldVar; //導入方法依賴的package包/類
@Test
public void testClassWithPersistenceContextAndPersistenceUnitFields() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
emf1Field.annotate(PersistenceContext.class);
final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf");
emf2Field.annotate(PersistenceUnit.class);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
try {
// WHEN
new JpaUnitRule(cut);
fail("IllegalArgumentException expected");
} catch (final IllegalArgumentException e) {
// THEN
assertThat(e.getMessage(), containsString("either @PersistenceUnit or @PersistenceContext"));
}
}