本文整理匯總了Java中com.sun.codemodel.JCodeModel.build方法的典型用法代碼示例。如果您正苦於以下問題:Java JCodeModel.build方法的具體用法?Java JCodeModel.build怎麽用?Java JCodeModel.build使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.codemodel.JCodeModel
的用法示例。
在下文中一共展示了JCodeModel.build方法的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: 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");
}
示例4: createRetrieveClass
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private static void createRetrieveClass(Retrieve retrieve, String packageName, File destinationDir) throws JClassAlreadyExistsException, IOException {
JCodeModel codeModel = new JCodeModel();
JClass mapClass = codeModel.ref(String.format("%s.%sMap", packageName, retrieve.getId()));
JDefinedClass retrieveClass = codeModel._class(String.format("%s.%sRetrieve", packageName, retrieve.getId()));
retrieveClass.javadoc().append("Auto generated class. Do not modify!");
retrieveClass._extends(codeModel.ref(AbstractRetrieve.class).narrow(mapClass));
// Constructor
JMethod constructor = retrieveClass.constructor(JMod.PUBLIC);
constructor.param(String.class, "id");
constructor.body().invoke("super").arg(JExpr.ref("id"));
// Implemented method
JMethod getMapMethod = retrieveClass.method(JMod.PUBLIC, mapClass, "getMap");
getMapMethod.annotate(Override.class);
getMapMethod.param(codeModel.ref(Map.class).narrow(String.class, Object.class), "data");
getMapMethod.body()._return(JExpr._new(mapClass).arg(JExpr.ref("data")));
codeModel.build(destinationDir);
}
示例5: testAnnotationImplements
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
public void testAnnotationImplements() throws Exception{
JCodeModel jmod = new JCodeModel();
// adding a test package
JPackage pack = jmod._package("testAnnotationImplements");
// building an interface
JDefinedClass interface1 = pack._interface("Interface1");
// adding annotations
JDefinedClass annotation1 = pack._annotationTypeDeclaration("Annot1");
try{
//this is perfectly legal in CodeModel:
annotation1._implements(interface1);
fail("No Exception was thrown for Illegal behavior");
} catch ( IllegalArgumentException ie){
}
jmod.build(new SingleStreamCodeWriter(System.out));
}
示例6: buildJavaClass
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private void buildJavaClass() {
try {
JCodeModel codeModel = new JCodeModel();
JDefinedClass definedClass = codeModel._class(this.clazz.getCanonicalClassName());
definedClass.javadoc().append(this.clazz.getJavaDoc());
JMethod constructor = definedClass.constructor(Visibility.PRIVATE.getMod());
constructor.body().directStatement(String.format("this.baseUri = \"%s\";", this.baseUri));
this.appendClassAttributes(codeModel, definedClass);
this.appendClassMethods(codeModel, definedClass);
codeModel.build(new File("src/main/java"));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
示例7: buildJavaClass
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private void buildJavaClass() {
try {
JCodeModel codeModel = new JCodeModel();
JDefinedClass definedClass = codeModel._class(this.clazz.getCanonicalClassName());
definedClass.javadoc().append(this.clazz.getJavaDoc());
String narrowClass = this.getModelFetchType();
for (String interfaceName : this.clazz.getInterfaces()) {
definedClass._implements(codeModel.ref(interfaceName).narrow(codeModel.ref(narrowClass)));
}
JMethod constructor = definedClass.constructor(Visibility.PUBLIC.getMod());
constructor.param(String.class, "url");
constructor.body().directStatement("this.url = ((url == null || url.equals(\"\")) ? \"\" : url) + \"" + this.serviceMeta.getUri() + "\";");
this.appendClassAttributes(codeModel, definedClass);
this.appendClassMethods(codeModel, definedClass);
codeModel.build(new File("src/main/java"));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
示例8: buildJavaClass
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private void buildJavaClass() {
try {
JCodeModel codeModel = new JCodeModel();
JDefinedClass definedClass = codeModel._class(this.clazz.getCanonicalClassName());
definedClass.javadoc().append(this.clazz.getJavaDoc());
for (String interfaceName : this.clazz.getInterfaces()) {
definedClass._implements(codeModel.ref(interfaceName));
}
this.appendClassAttributes(codeModel, definedClass);
this.appendClassMethods(codeModel, definedClass);
codeModel.build(new File("src/main/java"));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
示例9: buildJavaClass
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private void buildJavaClass() {
try {
JCodeModel codeModel = new JCodeModel();
JDefinedClass definedClass = codeModel._class(this.clazz.getCanonicalClassName(), ClassType.INTERFACE);
definedClass.javadoc().append(this.clazz.getJavaDoc());
String genericType = this.clazz.getPackageName().replace(".service", ".model.ICollectionModel");
definedClass.generify("T extends " + genericType + "<?>");
this.appendClassMethods(codeModel, definedClass);
codeModel.build(new File("src/main/java"));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
示例10: buildJavaClass
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private void buildJavaClass() {
try {
JCodeModel codeModel = new JCodeModel();
JDefinedClass definedClass = codeModel._class(this.clazz.getCanonicalClassName());
definedClass.javadoc().append(this.clazz.getJavaDoc());
JClass collectionType = codeModel.ref(ArrayList.class).narrow(codeModel.ref("E"));
definedClass._extends(collectionType).generify("E");
for (String interfaceName : this.clazz.getInterfaces()) {
definedClass._implements(codeModel.ref(interfaceName));
}
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
JClass param = codeModel.ref(this.clazz.getPackageName() + ".service." + ServiceFetchInterfaceGenerator.CLASS_NAME).narrow(codeModel.ref("?"));
constructor.param(param, "service");
constructor.body().directStatement(this.getConstructoryBody());
this.appendClassAttributes(codeModel, definedClass);
this.appendClassMethods(codeModel, definedClass);
codeModel.build(new File("src/main/java"));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
示例11: execute
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
public void execute() throws MojoExecutionException {
final JCodeModel codeModel = generateCodeModel();
Preconditions.checkState(outputDirectory.exists() || outputDirectory.mkdirs(), "Unable to create output directory: %s", outputDirectory.getPath());
try {
codeModel.build(outputDirectory);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例12: buildCodeModelToDisk
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private void buildCodeModelToDisk(JCodeModel codeModel, String name, File dir) {
try {
codeModel.build(dir);
} catch (IOException e) {
e.printStackTrace();
this.getLog().error("Could not build code model for " + name, e);
}
}
示例13: serializeModel
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
protected String serializeModel(JCodeModel jCodeModel) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
jCodeModel.build(new SingleStreamCodeWriter(bos));
} catch (IOException e) {
assertThat(e.getMessage(), is(nullValue()));
}
return bos.toString();
}
示例14: createMapClass
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private static void createMapClass(Retrieve retrieve, Map<String, Integer> columns, String packageName, File destinationDir) throws JClassAlreadyExistsException,
MapperEngineCodeGeneratorException,
IOException {
JCodeModel codeModel = new JCodeModel();
JDefinedClass mapClass = codeModel._class(String.format("%s.%sMap", packageName, retrieve.getId()));
mapClass.javadoc().append("Auto generated class. Do not modify!");
// Constructor
JMethod constructor = mapClass.constructor(JMod.PROTECTED);
constructor.param(codeModel.ref(Map.class).narrow(String.class, Object.class), "data");
for (String column : columns.keySet()) {
String varName = WordUtils.uncapitalize(column);
Class<?> type = null;
switch (columns.get(column)) {
case Types.INTEGER:
type = Integer.class;
break;
case Types.VARCHAR:
type = String.class;
break;
case Types.DECIMAL:
type = BigDecimal.class;
break;
default:
throw new MapperEngineCodeGeneratorException(String.format("Missing Type map for column %s: %d", column, columns.get(column)));
}
JFieldVar field = mapClass.field(JMod.PRIVATE | JMod.FINAL, type, varName);
// Getter
JMethod getter = mapClass.method(JMod.PUBLIC, type, String.format("get%s", column));
getter.body()._return(JExpr._this().ref(field));
constructor.body().assign(JExpr._this().ref(varName), JExpr.cast(codeModel.ref(type), JExpr.ref("data").invoke("get").arg(column)));
}
codeModel.build(destinationDir);
}
示例15: createRetrieveIdEnum
import com.sun.codemodel.JCodeModel; //導入方法依賴的package包/類
private static void createRetrieveIdEnum(List<Retrieve> retrieves, String packageName, File destinationDir) throws JClassAlreadyExistsException, IOException {
JCodeModel codeModel = new JCodeModel();
JDefinedClass enumClass = codeModel._class(JMod.PUBLIC, String.format("%s.MapEngineRetrieveId", packageName), ClassType.ENUM);
for (Retrieve retrieve : retrieves) {
enumClass.enumConstant(retrieve.getId());
}
codeModel.build(destinationDir);
}