@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")); }
private JCodeModel generate(final String className, final String packageName, final URL inputUrl, final File targetPath) throws Exception { final SchemaMapper mapper = createSchemaMapper(); final JCodeModel codeModel = new JCodeModel(); mapper.generate(codeModel, className, packageName, inputUrl); try (PrintStream status = new PrintStream(new ByteArrayOutputStream())) { codeModel.build(targetPath, status); } return codeModel; }
private SchemaMapper createSchemaMapper() { final RuleFactory ruleFactory = new RuleFactory(); ruleFactory.setAnnotator(new Jackson2Annotator() { @Override public boolean isAdditionalPropertiesSupported() { return false; } }); ruleFactory.setGenerationConfig(config); return new SchemaMapper(ruleFactory, new SchemaGenerator()); }
public static void main(String[] args) throws IOException { // BEGIN EXAMPLE JCodeModel codeModel = new JCodeModel(); URL source = new URL("file:///path/to/my/schema.json"); GenerationConfig config = new DefaultGenerationConfig() { @Override public boolean isGenerateBuilders() { // set config option by overriding method return true; } }; SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator()); mapper.generate(codeModel, "ClassName", "com.example", source); codeModel.build(new File("output")); // END EXAMPLE }
public void createModel(File jsonFile, String modelName) throws IOException { //CapFirst for java classes modelName = modelName.substring(0, 1).toUpperCase() + modelName.substring(1); System.out.println("Model name :"+modelName); JCodeModel codeModel = new JCodeModel(); GenerationConfig config = new DefaultGenerationConfig() { @Override public boolean isIncludeConstructors() { return true; } @Override public boolean isUseDoubleNumbers() { return true; } @Override public boolean isUsePrimitives() { return true; } @Override public boolean isIncludeToString() { return false; } @Override public boolean isIncludeHashcodeAndEquals() { return false; } @Override public boolean isIncludeAdditionalProperties() { return false; } @Override public Class<? extends RuleFactory> getCustomRuleFactory() { return APXCustomRuleFactory.class; } }; SchemaMapper mapper = new SchemaMapper(new APXCustomRuleFactory(config, new ORMLiteAnotator(), new SchemaStore()), new SchemaGenerator()); JType m = mapper.generate(codeModel, modelName, PACKAGE_NAME + ".models", jsonFile.toURI().toURL()); File f = new File(PROJECT_SRC); codeModel.build(f); System.out.print("Model created at :"); System.out.println(m.fullName().replace('.', File.separatorChar) + ".java"); }
/** * Generates classes based on json/jsonschema. * * @return class name * @throws IOException */ public String generate() throws IOException { String schemaPath = this.config.getJsonSchemaPath(); if (schemas.containsKey(schemaPath)){ LOG.info("Schema already exists " + schemaPath); return schemas.get(schemaPath); } JCodeModel codeModel = new JCodeModel(); URL source = new File(config.getInputPath()).toURI().toURL(); GenerationConfig generationConfig = new DefaultGenerationConfig() { @Override public boolean isGenerateBuilders() { // set config option by overriding metho return true; } @Override public SourceType getSourceType() { if (JsonPath.from(source).get("$schema") != null) { return SourceType.JSONSCHEMA; } return SourceType.JSON; } @Override public boolean isUseLongIntegers() { return true; } @Override public boolean isUseCommonsLang3() { return true; } }; SchemaMapper mapper = new SchemaMapper( new RuleFactory(generationConfig, new GsonAnnotator(), new SchemaStore()), new SchemaGenerator()); mapper.generate(codeModel, getClassName(), config.getPackageName(), source); codeModel.build(new File(config.getOutputPath())); schemas.put(schemaPath, getClassName()); return getClassName(); }