Search in sources :

Example 66 with JCodeModel

use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.

the class FragmentRefIT method refToInnerFragmentThatHasRefToOuterFragmentWithoutParentFile.

@Test
public void refToInnerFragmentThatHasRefToOuterFragmentWithoutParentFile() throws IOException, ClassNotFoundException {
    JCodeModel codeModel = new JCodeModel();
    JsonNode schema = new ObjectMapper().readTree("{\n" + "    \"type\": \"object\",\n" + "    \"definitions\": {\n" + "        \"location\": {\n" + "            \"type\": \"object\",\n" + "            \"properties\": {\n" + "                \"cat\": {\n" + "                    \"$ref\": \"#/definitions/cat\"\n" + "                }\n" + "            }\n" + "        },\n" + "        \"cat\": {\n" + "            \"type\": \"number\"\n" + "        }\n" + "    },\n" + "    \"properties\": {\n" + "        \"location\": {\n" + "            \"$ref\": \"#/definitions/location\"\n" + "        }\n" + "    }\n" + "}");
    JPackage p = codeModel._package("com.example");
    new RuleFactory().getSchemaRule().apply("Example", schema, p, new Schema(null, schema, schema));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) Schema(org.jsonschema2pojo.Schema) JPackage(com.sun.codemodel.JPackage) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 67 with JCodeModel

use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.

the class FragmentRefIT method selfRefWithoutParentFile.

@Test
public void selfRefWithoutParentFile() throws IOException {
    JCodeModel codeModel = new JCodeModel();
    JsonNode schema = new ObjectMapper().readTree("{\"type\":\"object\", \"properties\":{\"a\":{\"$ref\":\"#/b\"}}, \"b\":\"string\"}");
    JPackage p = codeModel._package("com.example");
    new RuleFactory().getSchemaRule().apply("Example", schema, p, new Schema(null, schema, schema));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) Schema(org.jsonschema2pojo.Schema) JPackage(com.sun.codemodel.JPackage) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 68 with JCodeModel

use of com.sun.codemodel.JCodeModel in project drools by kiegroup.

the class DroolsJaxbHelperProviderImpl method addXsdModel.

public static String[] addXsdModel(Resource resource, KnowledgeBuilderImpl kBuilder, Options xjcOpts, String systemId) throws IOException {
    InputSource source = new InputSource(new CachingRewindableReader(resource.getReader()));
    source.setSystemId(systemId.trim().startsWith(".") ? systemId : "." + systemId);
    xjcOpts.addGrammar(source);
    try {
        xjcOpts.parseArguments(new String[] { "-npa" });
    } catch (BadCommandLineException e) {
        throw new IllegalArgumentException("Unable to parse arguments", e);
    }
    ErrorReceiver errorReceiver = new JaxbErrorReceiver4Drools();
    Model model = ModelLoader.load(xjcOpts, new JCodeModel(), errorReceiver);
    model.generateCode(xjcOpts, errorReceiver);
    MapVfsCodeWriter codeWriter = new MapVfsCodeWriter();
    model.codeModel.build(xjcOpts.createCodeWriter(codeWriter));
    MemoryResourceReader src = new MemoryResourceReader();
    boolean useProjectClassLoader = kBuilder.getRootClassLoader() instanceof ProjectClassLoader;
    List<String> classNames = new ArrayList<String>();
    List<String> srcNames = new ArrayList<String>();
    for (Entry<String, byte[]> entry : codeWriter.getMap().entrySet()) {
        String name = entry.getKey();
        int dotPos = name.lastIndexOf('.');
        String pkgName = name.substring(0, dotPos);
        if (!name.endsWith("package-info.java")) {
            classNames.add(pkgName);
        }
        dotPos = pkgName.lastIndexOf('.');
        if (dotPos != -1) {
            pkgName = pkgName.substring(0, dotPos);
        }
        PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
        if (pkgReg == null) {
            kBuilder.addPackage(new PackageDescr(pkgName));
            pkgReg = kBuilder.getPackageRegistry(pkgName);
        }
        if (useProjectClassLoader) {
            String srcName = convertToResource(entry.getKey());
            src.add(srcName, entry.getValue());
            srcNames.add(srcName);
        } else {
            JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
            dialect.addSrc(convertToResource(entry.getKey()), entry.getValue());
        }
    }
    if (useProjectClassLoader) {
        ProjectJavaCompiler compiler = new ProjectJavaCompiler(kBuilder.getBuilderConfiguration());
        List<KnowledgeBuilderResult> results = compiler.compileAll((ProjectClassLoader) kBuilder.getRootClassLoader(), srcNames, src);
        for (String className : classNames) {
            Class<?> clazz = null;
            try {
                clazz = Class.forName(className, true, kBuilder.getRootClassLoader());
            } catch (ClassNotFoundException e) {
                continue;
            }
            String pkgName = className.substring(0, className.lastIndexOf('.'));
            PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
            pkgReg.getPackage().addTypeDeclaration(TypeDeclaration.createTypeDeclarationForBean(clazz));
        }
        kBuilder.updateResults(results);
    } else {
        kBuilder.compileAll();
        kBuilder.updateResults();
    }
    return classNames.toArray(new String[classNames.size()]);
}
Also used : InputSource(org.xml.sax.InputSource) ArrayList(java.util.ArrayList) ErrorReceiver(com.sun.tools.xjc.ErrorReceiver) MemoryResourceReader(org.drools.compiler.commons.jci.readers.MemoryResourceReader) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) ProjectJavaCompiler(org.drools.compiler.compiler.ProjectJavaCompiler) JCodeModel(com.sun.codemodel.JCodeModel) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) Model(com.sun.tools.xjc.model.Model) JCodeModel(com.sun.codemodel.JCodeModel) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 69 with JCodeModel

use of com.sun.codemodel.JCodeModel in project cxf by apache.

the class JAXBDataBinding method generate.

public void generate(ToolContext c) throws ToolException {
    if (!initialized) {
        initialize(c);
    }
    if (rawJaxbModelGenCode == null) {
        return;
    }
    if (c.getErrorListener().getErrorCount() > 0) {
        return;
    }
    try {
        String dir = (String) context.get(ToolConstants.CFG_OUTPUTDIR);
        TypesCodeWriter fileCodeWriter = new TypesCodeWriter(new File(dir), context.getExcludePkgList(), (String) context.get(ToolConstants.CFG_ENCODING), context.get(OutputStreamCreator.class));
        S2JJAXBModel schem2JavaJaxbModel = rawJaxbModelGenCode;
        ClassCollector classCollector = context.get(ClassCollector.class);
        for (JClass cls : schem2JavaJaxbModel.getAllObjectFactories()) {
            classCollector.getTypesPackages().add(cls._package().name());
        }
        JCodeModel jcodeModel = schem2JavaJaxbModel.generateCode(null, null);
        if (!isSuppressCodeGen()) {
            jcodeModel.build(fileCodeWriter);
        }
        context.put(JCodeModel.class, jcodeModel);
        for (String str : fileCodeWriter.getExcludeFileList()) {
            context.getExcludeFileList().add(str);
        }
        return;
    } catch (IOException e) {
        Message msg = new Message("FAIL_TO_GENERATE_TYPES", LOG);
        throw new ToolException(msg, e);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) ClassCollector(org.apache.cxf.tools.util.ClassCollector) JClass(com.sun.codemodel.JClass) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) S2JJAXBModel(com.sun.tools.xjc.api.S2JJAXBModel) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File) JCodeModel(com.sun.codemodel.JCodeModel)

Example 70 with JCodeModel

use of com.sun.codemodel.JCodeModel in project cxf by apache.

the class DummyXjcPlugin method run.

@Override
public boolean run(Outline arg0, Options arg1, ErrorHandler arg2) {
    for (ClassOutline classOutline : arg0.getClasses()) {
        JDefinedClass implClass = classOutline.implClass;
        JCodeModel codeModel = implClass.owner();
        JMethod dummyMethod = implClass.method(JMod.PUBLIC, codeModel.ref(String.class), "dummy");
        dummyMethod.body()._return(JExpr.lit("dummy"));
    }
    return true;
}
Also used : ClassOutline(com.sun.tools.xjc.outline.ClassOutline) JDefinedClass(com.sun.codemodel.JDefinedClass) JMethod(com.sun.codemodel.JMethod) JCodeModel(com.sun.codemodel.JCodeModel)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)77 Test (org.junit.Test)59 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)52 JPackage (com.sun.codemodel.JPackage)47 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)45 JType (com.sun.codemodel.JType)41 JDefinedClass (com.sun.codemodel.JDefinedClass)15 JClass (com.sun.codemodel.JClass)12 Schema (org.jsonschema2pojo.Schema)11 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 TextNode (com.fasterxml.jackson.databind.node.TextNode)6 File (java.io.File)6 IOException (java.io.IOException)5 JBlock (com.sun.codemodel.JBlock)4 JCatchBlock (com.sun.codemodel.JCatchBlock)4 JMethod (com.sun.codemodel.JMethod)4 JTryBlock (com.sun.codemodel.JTryBlock)4 JVar (com.sun.codemodel.JVar)4 URL (java.net.URL)4