Search in sources :

Example 1 with ComponentGenerationException

use of com.vaadin.generator.exception.ComponentGenerationException in project flow by vaadin.

the class ComponentGenerator method generateClass.

/**
 * Generates the Java class by using the {@link ComponentMetadata} object.
 *
 * @param metadata
 *            The webcomponent metadata.
 * @param targetPath
 *            The output base directory for the generated Java file.
 * @param basePackage
 *            The base package to be used for the generated Java class. The
 *            final package of the class is basePackage plus the
 *            {@link ComponentMetadata#getBaseUrl()}.
 * @param licenseNote
 *            A note to be added on top of the class as a comment. Usually
 *            used for license headers.
 * @throws ComponentGenerationException
 *             If an error occurs when generating the class.
 */
public void generateClass(ComponentMetadata metadata, File targetPath, String basePackage, String licenseNote) {
    JavaClassSource javaClass = generateClassSource(metadata, basePackage);
    String source = addLicenseHeaderIfAvailable(javaClass.toString(), licenseNote);
    String fileName = ComponentGeneratorUtils.generateValidJavaClassName(javaClass.getName()) + ".java";
    if (!targetPath.isDirectory() && !targetPath.mkdirs()) {
        throw new ComponentGenerationException("Could not create target directory \"" + targetPath + "\"");
    }
    try {
        Files.write(new File(ComponentGeneratorUtils.convertPackageToDirectory(targetPath, javaClass.getPackage(), true), fileName).toPath(), source.getBytes(UTF_8));
    } catch (IOException ex) {
        throw new ComponentGenerationException("Error writing the generated Java source file \"" + fileName + "\" at \"" + targetPath + "\" for component \"" + metadata.getName() + "\"", ex);
    }
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentGenerationException(com.vaadin.generator.exception.ComponentGenerationException) IOException(java.io.IOException) File(java.io.File)

Example 2 with ComponentGenerationException

use of com.vaadin.generator.exception.ComponentGenerationException in project flow by vaadin.

the class ComponentGenerator method generateNestedPojo.

private JavaClassSource generateNestedPojo(JavaClassSource javaClass, ComponentObjectType type, String nameHint, String description) {
    JavaClassSource nestedClass = new NestedClassGenerator().withType(type).withFluentSetters(fluentMethod).withNameHint(nameHint).build();
    if (javaClass.getNestedType(nestedClass.getName()) != null) {
        throw new ComponentGenerationException("Duplicated nested class: \"" + nestedClass.getName() + "\". Please make sure your webcomponent definition contains unique properties, events and method names.");
    }
    nestedClass.getJavaDoc().setText(description);
    javaClass.addNestedType(nestedClass);
    javaClass.addImport(JsonObject.class);
    javaClass.addImport(JsonSerializable.class);
    return nestedClass;
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentGenerationException(com.vaadin.generator.exception.ComponentGenerationException)

Aggregations

ComponentGenerationException (com.vaadin.generator.exception.ComponentGenerationException)2 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)2 File (java.io.File)1 IOException (java.io.IOException)1