Search in sources :

Example 26 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project flow by vaadin.

the class NestedClassGeneratorTest method emptyClassDefinition_onlyJsonSerializableMethods.

@Test
public void emptyClassDefinition_onlyJsonSerializableMethods() {
    JavaClassSource javaClass = generator.withNameHint("test").withType(type).build();
    assertJsonSerializableWasImplemented(javaClass);
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Test(org.junit.Test)

Example 27 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project flow by vaadin.

the class NestedClassGeneratorTest method multipleObjectTypesClassDefinition_classHasGenericGetterAndSetter.

@Test
public void multipleObjectTypesClassDefinition_classHasGenericGetterAndSetter() {
    type.getInnerTypes().add(createComponentObjectType("multipleSimpleProperty", ComponentBasicType.BOOLEAN, ComponentBasicType.STRING, ComponentBasicType.NUMBER));
    type.getInnerTypes().add(createComponentObjectType("multipleObjectProperty", ComponentBasicType.STRING, ComponentBasicType.OBJECT, ComponentBasicType.BOOLEAN));
    type.getInnerTypes().add(createComponentObjectType("multipleRepeatedProperty", ComponentBasicType.NUMBER, ComponentBasicType.NUMBER));
    JavaClassSource javaClass = generator.withNameHint("test").withType(type).build();
    assertJsonSerializableWasImplemented(javaClass);
    assertGetterAndSetterArePresent(javaClass, "getMultipleSimpleProperty", "setMultipleSimpleProperty", JsonValue.class);
    assertGetterAndSetterArePresent(javaClass, "getMultipleObjectProperty", "setMultipleObjectProperty", JsonObject.class);
    assertGetterAndSetterArePresent(javaClass, "getMultipleRepeatedProperty", "setMultipleRepeatedProperty", double.class);
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Test(org.junit.Test)

Example 28 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project flow by vaadin.

the class NestedClassGeneratorTest method simpleObjectTypesClassDefinition_classHasGettersAndSetters.

@Test
public void simpleObjectTypesClassDefinition_classHasGettersAndSetters() {
    type.getInnerTypes().add(createComponentObjectType("booleanProperty", ComponentBasicType.BOOLEAN));
    type.getInnerTypes().add(createComponentObjectType("stringProperty", ComponentBasicType.STRING));
    type.getInnerTypes().add(createComponentObjectType("numberProperty", ComponentBasicType.NUMBER));
    type.getInnerTypes().add(createComponentObjectType("objectProperty", ComponentBasicType.OBJECT));
    type.getInnerTypes().add(createComponentObjectType("arrayProperty", ComponentBasicType.ARRAY));
    type.getInnerTypes().add(createComponentObjectType("undefinedProperty", ComponentBasicType.UNDEFINED));
    JavaClassSource javaClass = generator.withNameHint("test").withType(type).build();
    assertJsonSerializableWasImplemented(javaClass);
    assertGetterAndSetterArePresent(javaClass, "isBooleanProperty", "setBooleanProperty", boolean.class);
    assertGetterAndSetterArePresent(javaClass, "getStringProperty", "setStringProperty", String.class);
    assertGetterAndSetterArePresent(javaClass, "getNumberProperty", "setNumberProperty", double.class);
    assertGetterAndSetterArePresent(javaClass, "getObjectProperty", "setObjectProperty", JsonObject.class);
    assertGetterAndSetterArePresent(javaClass, "getArrayProperty", "setArrayProperty", JsonArray.class);
    assertGetterAndSetterArePresent(javaClass, "getUndefinedProperty", "setUndefinedProperty", JsonValue.class);
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Test(org.junit.Test)

Example 29 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project herd by FINRAOS.

the class RestControllerProcessor method processRestControllerClass.

/**
 * Processes a Spring MVC REST controller class that is annotated with RestController. Also collects any required model objects based on parameters and
 * return types of each endpoint into the specified model classes set.
 *
 * @param clazz the class to process
 *
 * @throws MojoExecutionException if any errors were encountered.
 */
private void processRestControllerClass(Class<?> clazz) throws MojoExecutionException {
    // Get the Java class source information.
    JavaClassSource javaClassSource = sourceMap.get(clazz.getSimpleName());
    if (javaClassSource == null) {
        throw new MojoExecutionException("No source resource found for class \"" + clazz.getName() + "\".");
    }
    Api api = clazz.getAnnotation(Api.class);
    boolean hidden = api != null && api.hidden();
    if ((clazz.getAnnotation(RestController.class) != null) && (!hidden)) {
        log.debug("Processing RestController class \"" + clazz.getName() + "\".");
        // Default the tag name to the simple class name.
        String tagName = clazz.getSimpleName();
        // See if the "Api" annotation exists.
        if (api != null && api.tags().length > 0) {
            // The "Api" annotation was found so use it's configured tag.
            tagName = api.tags()[0];
        } else {
            // No "Api" annotation so try to get the tag name from the class name. If not, we will stick with the default simple class name.
            Matcher matcher = tagPattern.matcher(clazz.getSimpleName());
            if (matcher.find()) {
                // If our class has the form
                tagName = matcher.group("tag");
            }
        }
        log.debug("Using tag name \"" + tagName + "\".");
        // Add the tag and process each method.
        swagger.addTag(new Tag().name(tagName));
        List<Method> methods = Lists.newArrayList(clazz.getDeclaredMethods());
        // Based on the Java 8 doc, getDeclaredMethods() is not guaranteed sorted
        // In order to be sure we generate stable API when the URL's don't change
        // we can give it a sort order based on the first URI hit in the request mapping
        List<Method> outMethods = methods.stream().filter((method) -> {
            RequestMapping rm = method.getAnnotation(RequestMapping.class);
            ApiOperation apiOp = method.getAnnotation(ApiOperation.class);
            return // Has RequestMapping
            rm != null && // has at least 1 URI
            rm.value().length > 0 && // has at least 1 HttpMethod
            rm.method().length > 0 && // marked as a hidden ApiOperation
            (apiOp == null || !apiOp.hidden());
        }).sorted(Comparator.comparing(a -> ((Method) a).getAnnotation(RequestMapping.class).value()[0]).thenComparing(a -> ((Method) a).getAnnotation(RequestMapping.class).method()[0])).collect(Collectors.toList());
        for (Method method : outMethods) {
            // Get the method source information.
            List<Class<?>> methodParamClasses = new ArrayList<>();
            for (Parameter parameter : method.getParameters()) {
                methodParamClasses.add(parameter.getType());
            }
            MethodSource<JavaClassSource> methodSource = javaClassSource.getMethod(method.getName(), methodParamClasses.toArray(new Class<?>[methodParamClasses.size()]));
            if (methodSource == null) {
                throw new MojoExecutionException("No method source found for class \"" + clazz.getName() + "\" and method name \"" + method.getName() + "\".");
            }
            // Process the REST controller method along with its source information.
            processRestControllerMethod(method, clazz.getAnnotation(RequestMapping.class), tagName, methodSource);
        }
    } else {
        log.debug("Skipping class \"" + clazz.getName() + "\" because it is either not a RestController or it is hidden.");
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) RequestParam(org.springframework.web.bind.annotation.RequestParam) Tag(io.swagger.models.Tag) SerializableParameter(io.swagger.models.parameters.SerializableParameter) ParameterSource(org.jboss.forge.roaster.model.source.ParameterSource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ApiOperation(io.swagger.annotations.ApiOperation) Matcher(java.util.regex.Matcher) TagElement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.TagElement) Roaster(org.jboss.forge.roaster.Roaster) Path(io.swagger.models.Path) Map(java.util.Map) MetadataReader(org.springframework.core.type.classreading.MetadataReader) Operation(io.swagger.models.Operation) Method(java.lang.reflect.Method) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) Resource(org.springframework.core.io.Resource) RefModel(io.swagger.models.RefModel) ClassUtils(org.springframework.util.ClassUtils) PathParameter(io.swagger.models.parameters.PathParameter) JavaDocSource(org.jboss.forge.roaster.model.source.JavaDocSource) Set(java.util.Set) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) QueryParameter(io.swagger.models.parameters.QueryParameter) Response(io.swagger.models.Response) List(java.util.List) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) CollectionUtils(org.springframework.util.CollectionUtils) JavaDocTag(org.jboss.forge.roaster.model.JavaDocTag) Pattern(java.util.regex.Pattern) Javadoc(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Javadoc) Swagger(io.swagger.models.Swagger) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestBody(org.springframework.web.bind.annotation.RequestBody) XmlType(javax.xml.bind.annotation.XmlType) Lists(com.google.common.collect.Lists) Parameter(java.lang.reflect.Parameter) RefProperty(io.swagger.models.properties.RefProperty) SystemPropertyUtils(org.springframework.util.SystemPropertyUtils) Api(io.swagger.annotations.Api) BodyParameter(io.swagger.models.parameters.BodyParameter) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Log(org.apache.maven.plugin.logging.Log) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Comparator(java.util.Comparator) Collections(java.util.Collections) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) RestController(org.springframework.web.bind.annotation.RestController) Method(java.lang.reflect.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiOperation(io.swagger.annotations.ApiOperation) SerializableParameter(io.swagger.models.parameters.SerializableParameter) PathParameter(io.swagger.models.parameters.PathParameter) QueryParameter(io.swagger.models.parameters.QueryParameter) Parameter(java.lang.reflect.Parameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Api(io.swagger.annotations.Api) Tag(io.swagger.models.Tag) JavaDocTag(org.jboss.forge.roaster.model.JavaDocTag)

Example 30 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project herd by FINRAOS.

the class RestControllerProcessor method findAndProcessRestControllers.

/**
 * Finds all the REST controllers within the configured REST Java package and process the REST methods within each one.
 *
 * @throws MojoExecutionException if any errors were encountered.
 */
private void findAndProcessRestControllers() throws MojoExecutionException {
    try {
        log.debug("Finding and processing REST controllers.");
        // Loop through each resources and process each one.
        for (Resource resource : ResourceUtils.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + restJavaPackage.replace('.', '/') + "/**/*.java")) {
            if (resource.isReadable()) {
                JavaClassSource javaClassSource = Roaster.parse(JavaClassSource.class, resource.getInputStream());
                sourceMap.put(javaClassSource.getName(), javaClassSource);
                log.debug("Found Java source class \"" + javaClassSource.getName() + "\".");
            }
        }
        // Loop through each controller resources and process each one.
        for (Resource resource : ResourceUtils.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(restJavaPackage)) + "/**/*.class")) {
            if (resource.isReadable()) {
                // Create a resource resolver to fetch resources.
                MetadataReader metadataReader = new CachingMetadataReaderFactory(new PathMatchingResourcePatternResolver()).getMetadataReader(resource);
                Class<?> clazz = Class.forName(metadataReader.getClassMetadata().getClassName());
                processRestControllerClass(clazz);
            }
        }
    } catch (ClassNotFoundException | IOException e) {
        throw new MojoExecutionException("Error processing REST classes. Reason: " + e.getMessage(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Resource(org.springframework.core.io.Resource) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Aggregations

JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)95 File (java.io.File)37 ArrayList (java.util.ArrayList)28 Test (org.junit.Test)26 ParserResult (org.apache.camel.parser.ParserResult)19 List (java.util.List)15 IOException (java.io.IOException)14 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)12 InputStream (java.io.InputStream)10 Collections (java.util.Collections)10 Roaster (org.jboss.forge.roaster.Roaster)10 BufferedReader (java.io.BufferedReader)8 HashMap (java.util.HashMap)8 Matcher (java.util.regex.Matcher)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)8 Arrays (java.util.Arrays)7 Map (java.util.Map)7 Import (org.jboss.forge.roaster.model.source.Import)7 ClassTypeResolver (org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)7