本文整理匯總了Java中com.sun.codemodel.JCodeModel類的典型用法代碼示例。如果您正苦於以下問題:Java JCodeModel類的具體用法?Java JCodeModel怎麽用?Java JCodeModel使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JCodeModel類屬於com.sun.codemodel包,在下文中一共展示了JCodeModel類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: nestedSelfRefsInStringContentWithoutParentFile
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@Test
public void nestedSelfRefsInStringContentWithoutParentFile() throws NoSuchMethodException, ClassNotFoundException, IOException {
String schemaContents = IOUtils.toString(CodeGenerationHelper.class.getResource("/schema/ref/nestedSelfRefsReadAsString.json"));
JCodeModel codeModel = new JCodeModel();
new SchemaMapper().generate(codeModel, "NestedSelfRefsInString", "com.example", schemaContents);
codeModel.build(schemaRule.getGenerateDir());
ClassLoader classLoader = schemaRule.compile();
Class<?> nestedSelfRefs = classLoader.loadClass("com.example.NestedSelfRefsInString");
assertThat(nestedSelfRefs.getMethod("getThings").getReturnType().getSimpleName(), equalTo("List"));
Class<?> listEntryType = (Class<?>) ((ParameterizedType)nestedSelfRefs.getMethod("getThings").getGenericReturnType()).getActualTypeArguments()[0];
assertThat(listEntryType.getName(), equalTo("com.example.Thing"));
Class<?> thingClass = classLoader.loadClass("com.example.Thing");
assertThat(thingClass.getMethod("getNamespace").getReturnType().getSimpleName(), equalTo("String"));
assertThat(thingClass.getMethod("getName").getReturnType().getSimpleName(), equalTo("String"));
assertThat(thingClass.getMethod("getVersion").getReturnType().getSimpleName(), equalTo("String"));
}
示例2: generateFromSchema
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
/**
* Generates Java classes in targetPath directory given an XML schema.
*
* @param schemaFile file reference to the XML schema
* @param packageName package name for generated model classes
* @param targetPath directory where class source will be generated
* @return the generated code model
* @throws Exception failure during model generation
*/
public JCodeModel generateFromSchema(final File schemaFile, final String packageName,
final File targetPath) throws Exception {
final SchemaCompiler sc = XJC.createSchemaCompiler();
final FileInputStream schemaStream = new FileInputStream(schemaFile);
final InputSource is = new InputSource(schemaStream);
is.setSystemId(schemaFile.getAbsolutePath());
sc.parseSchema(is);
sc.forcePackageName(packageName);
final S2JJAXBModel s2 = sc.bind();
final JCodeModel jcm = s2.generateCode(null, null);
try (PrintStream status = new PrintStream(new ByteArrayOutputStream())) {
jcm.build(targetPath, status);
}
return jcm;
}
示例3: applyGeneratesDate
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@Test
public void applyGeneratesDate() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "string");
TextNode formatNode = TextNode.valueOf("date-time");
objectNode.set("format", formatNode);
JType mockDateType = mock(JType.class);
FormatRule mockFormatRule = mock(FormatRule.class);
when(mockFormatRule.apply(eq("fooBar"), eq(formatNode), Mockito.isA(JType.class), isNull(Schema.class))).thenReturn(mockDateType);
when(ruleFactory.getFormatRule()).thenReturn(mockFormatRule);
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result, equalTo(mockDateType));
}
示例4: applyGeneratesArray
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@Test
public void applyGeneratesArray() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "array");
JClass mockArrayType = mock(JClass.class);
ArrayRule mockArrayRule = mock(ArrayRule.class);
when(mockArrayRule.apply("fooBar", objectNode, jpackage, null)).thenReturn(mockArrayType);
when(ruleFactory.getArrayRule()).thenReturn(mockArrayRule);
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result, is((JType) mockArrayType));
}
示例5: applyGeneratesCustomObject
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@Test
public void applyGeneratesCustomObject() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "object");
JDefinedClass mockObjectType = mock(JDefinedClass.class);
ObjectRule mockObjectRule = mock(ObjectRule.class);
when(mockObjectRule.apply("fooBar", objectNode, jpackage, null)).thenReturn(mockObjectType);
when(ruleFactory.getObjectRule()).thenReturn(mockObjectRule);
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result, is((JType) mockObjectType));
}
示例6: arrayWithUniqueItemsProducesSet
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@Test
public void arrayWithUniqueItemsProducesSet() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "integer");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.TRUE);
propertyNode.set("items", itemsNode);
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, mock(Schema.class));
assertThat(propertyType, notNullValue());
assertThat(propertyType.erasure(), is(codeModel.ref(Set.class)));
assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Integer.class.getName()));
}
示例7: arrayWithNonUniqueItemsProducesList
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@Test
public void arrayWithNonUniqueItemsProducesList() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "number");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.FALSE);
propertyNode.set("items", itemsNode);
Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
when(config.isUseDoubleNumbers()).thenReturn(true);
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
assertThat(propertyType, notNullValue());
assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Double.class.getName()));
}
示例8: arrayOfPrimitivesProducesCollectionOfWrapperTypes
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@Test
public void arrayOfPrimitivesProducesCollectionOfWrapperTypes() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "number");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.FALSE);
propertyNode.set("items", itemsNode);
Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
when(config.isUsePrimitives()).thenReturn(true);
when(config.isUseDoubleNumbers()).thenReturn(true);
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
assertThat(propertyType, notNullValue());
assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Double.class.getName()));
}
示例9: arrayDefaultsToNonUnique
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@Test
public void arrayDefaultsToNonUnique() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "boolean");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.FALSE);
propertyNode.set("items", itemsNode);
Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/defaultArray"));
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
}
示例10: MetaJavaBean
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
/**
* Creates a new MetaJavaBean with random fields.
*
* @param fqn
* The fully qualified name of the bean's class.
* @param numFields
* Number of simple type fields that will be added.
*/
public MetaJavaBean(JCodeModel cm, String fqn, int numFields) {
super(cm, fqn, ClassType.CLASS);
// Empty constructor
getGeneratedClass().constructor(JMod.PUBLIC);
// Init the simple fields
this.fields = Maps.newHashMapWithExpectedSize(numFields);
for (int i = 0; i < numFields; ++i) {
String fieldName = "field" + Config.CFG.nextUniqueNum();
JavaBeanBasicField field = new JavaBeanBasicField(this, fieldName);
fields.put(fieldName, field);
}
}
示例11: ClassGenerator
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@SuppressWarnings("unchecked")
ClassGenerator(CodeGenerator<T> codeGenerator, MappingSet mappingSet, SignatureHolder signature, EvaluationVisitor eval, JDefinedClass clazz, JCodeModel model) throws JClassAlreadyExistsException {
this.codeGenerator = codeGenerator;
this.clazz = clazz;
this.mappings = mappingSet;
this.sig = signature;
this.evaluationVisitor = eval;
this.model = model;
blocks = (LinkedList<JBlock>[]) new LinkedList[sig.size()];
for (int i =0; i < sig.size(); i++) {
blocks[i] = Lists.newLinkedList();
}
rotateBlock();
for (SignatureHolder child : signature.getChildHolders()) {
String innerClassName = child.getSignatureClass().getSimpleName();
JDefinedClass innerClazz = clazz._class(Modifier.FINAL + Modifier.PRIVATE, innerClassName);
innerClasses.put(innerClassName, new ClassGenerator<>(codeGenerator, mappingSet, child, eval, innerClazz, model));
}
}
示例12: CodeGenerator
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
CodeGenerator(MappingSet mappingSet, TemplateClassDefinition<T> definition,
FunctionImplementationRegistry funcRegistry) {
Preconditions.checkNotNull(definition.getSignature(),
"The signature for defintion %s was incorrectly initialized.", definition);
this.definition = definition;
this.className = definition.getExternalInterface().getSimpleName() + "Gen" + definition.getNextClassNumber();
this.fqcn = PACKAGE_NAME + "." + className;
try {
this.model = new JCodeModel();
JDefinedClass clazz = model._package(PACKAGE_NAME)._class(className);
rootGenerator = new ClassGenerator<>(this, mappingSet, definition.getSignature(), new EvaluationVisitor(
funcRegistry), clazz, model);
} catch (JClassAlreadyExistsException e) {
throw new IllegalStateException(e);
}
}
示例13: preview
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
@RequestMapping(value = "/generator/preview", method = RequestMethod.POST)
public String preview(@RequestParam(value = "schema") String schema,
@RequestParam(value = "targetpackage") String targetpackage,
@RequestParam(value = "sourcetype", required = false) final String sourcetype,
@RequestParam(value = "annotationstyle", required = false) final String annotationstyle,
@RequestParam(value = "usedoublenumbers", required = false) final boolean usedoublenumbers,
@RequestParam(value = "includeaccessors", required = false) final boolean includeaccessors,
@RequestParam(value = "includeadditionalproperties", required = false) final boolean includeadditionalproperties,
@RequestParam(value = "propertyworddelimiters", required = false) final String propertyworddelimiters,
@RequestParam(value = "classname") String classname) throws IOException {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JCodeModel codegenModel = getCodegenModel(schema, targetpackage, sourcetype, annotationstyle, usedoublenumbers, includeaccessors, includeadditionalproperties, propertyworddelimiters, classname);
codegenModel.build(new CodeWriter() {
@Override
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
return byteArrayOutputStream;
}
@Override
public void close() throws IOException {
byteArrayOutputStream.close();
}
});
return byteArrayOutputStream.toString("utf-8");
}
示例14: CodeGenerator
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
CodeGenerator(CodeCompiler compiler, MappingSet mappingSet, TemplateClassDefinition<T> definition) {
Preconditions.checkNotNull(definition.getSignature(),
"The signature for defintion %s was incorrectly initialized.", definition);
this.definition = definition;
this.compiler = compiler;
this.className = definition.getExternalInterface().getSimpleName() + "Gen" + definition.getNextClassNumber();
this.fqcn = PACKAGE_NAME + "." + className;
try {
this.model = new JCodeModel();
JDefinedClass clazz = model._package(PACKAGE_NAME)._class(className);
clazz = clazz._extends(model.directClass(definition.getTemplateClassName()));
clazz.constructor(JMod.PUBLIC).body().invoke(SignatureHolder.INIT_METHOD);
rootGenerator = new ClassGenerator<>(this, mappingSet, definition.getSignature(), new EvaluationVisitor(), clazz, model);
} catch (JClassAlreadyExistsException e) {
throw new IllegalStateException(e);
}
}
示例15: getUDFInstance
import com.sun.codemodel.JCodeModel; //導入依賴的package包/類
private JInvocation getUDFInstance(JCodeModel m) {
if (isGenericUDF) {
return JExpr._new(m.directClass(genericUdfClazz.getCanonicalName()));
} else {
return JExpr._new(m.directClass(GenericUDFBridge.class.getCanonicalName()))
.arg(JExpr.lit(udfName))
.arg(JExpr.lit(false))
.arg(JExpr.lit(udfClazz.getCanonicalName().toString()));
}
}