Java 类org.springframework.boot.context.properties.EnableConfigurationProperties 实例源码

项目:Camel    文件:SpringBootAutoConfigurationMojo.java   
private void createComponentAutoConfigurationSource(String packageName, ComponentModel model) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);

    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Component", "ComponentAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);

    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);

    javaClass.addAnnotation(Configuration.class);

    String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration");
    AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
    ann.setLiteralValue("value", configurationName + ".class");

    // add method for auto configure

    javaClass.addImport("java.util.HashMap");
    javaClass.addImport("java.util.Map");
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    javaClass.addImport("org.apache.camel.util.IntrospectionSupport");

    String body = createComponentBody(model.getShortJavaType());
    String methodName = "configure" + model.getShortJavaType();

    MethodSource<JavaClassSource> method = javaClass.addMethod()
            .setName(methodName)
            .setPublic()
            .setBody(body)
            .setReturnType(model.getShortJavaType())
            .addThrows(Exception.class);

    method.addParameter("CamelContext", "camelContext");
    method.addParameter(configurationName, "configuration");

    method.addAnnotation(Bean.class);
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");

    sortImports(javaClass);

    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    File target = new File(srcDir, fileName);

    try {
        InputStream is = getClass().getClassLoader().getResourceAsStream("license-header-java.txt");
        String header = loadText(is);
        String code = sourceToString(javaClass);
        code = header + code;
        getLog().debug("Source code generated:\n" + code);

        if (target.exists()) {
            String existing = FileUtils.readFileToString(target);
            if (!code.equals(existing)) {
                FileUtils.write(target, code, false);
                getLog().info("Updated existing file: " + target);
            } else {
                getLog().debug("No changes to existing file: " + target);
            }
        } else {
            FileUtils.write(target, code);
            getLog().info("Created file: " + target);
        }
    } catch (Exception e) {
        throw new MojoFailureException("IOError with file " + target, e);
    }
}
项目:Camel    文件:SpringBootAutoConfigurationMojo.java   
private void createDataFormatAutoConfigurationSource(String packageName, DataFormatModel model) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);

    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("DataFormat", "DataFormatAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);

    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);

    javaClass.addAnnotation(Configuration.class);

    String configurationName = name.replace("DataFormatAutoConfiguration", "DataFormatConfiguration");
    AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
    ann.setLiteralValue("value", configurationName + ".class");

    // add method for auto configure

    javaClass.addImport("java.util.HashMap");
    javaClass.addImport("java.util.Map");
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    javaClass.addImport("org.apache.camel.CamelContextAware");
    javaClass.addImport("org.apache.camel.util.IntrospectionSupport");

    String body = createDataFormatBody(model.getShortJavaType());
    String methodName = "configure" + model.getShortJavaType();

    MethodSource<JavaClassSource> method = javaClass.addMethod()
            .setName(methodName)
            .setPublic()
            .setBody(body)
            .setReturnType(model.getShortJavaType())
            .addThrows(Exception.class);

    method.addParameter("CamelContext", "camelContext");
    method.addParameter(configurationName, "configuration");

    method.addAnnotation(Bean.class);
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");

    sortImports(javaClass);

    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    File target = new File(srcDir, fileName);

    try {
        InputStream is = getClass().getClassLoader().getResourceAsStream("license-header-java.txt");
        String header = loadText(is);
        String code = sourceToString(javaClass);
        code = header + code;
        getLog().debug("Source code generated:\n" + code);

        if (target.exists()) {
            String existing = FileUtils.readFileToString(target);
            if (!code.equals(existing)) {
                FileUtils.write(target, code, false);
                getLog().info("Updated existing file: " + target);
            } else {
                getLog().debug("No changes to existing file: " + target);
            }
        } else {
            FileUtils.write(target, code);
            getLog().info("Created file: " + target);
        }
    } catch (Exception e) {
        throw new MojoFailureException("IOError with file " + target, e);
    }
}