Search in sources :

Example 1 with GenericType

use of org.apache.camel.tooling.util.srcgen.GenericType in project camel-spring-boot by apache.

the class SpringBootAutoConfigurationMojo method readJavaType.

// read java type from project, returns null if not found
private JavaClass readJavaType(String type) {
    if (!type.startsWith("java.lang.") && (!type.contains("<") || !type.contains(">"))) {
        String sourceCode = "";
        try {
            Class<?> clazz = getProjectClassLoader().loadClass(type);
            URL url = clazz != null ? getProjectClassLoader().getResource(clazz.getName().replace('.', '/') + ".class") : null;
            Artifact mainDep = project.getArtifactMap().get(getMainDepGroupId() + ":" + getMainDepArtifactId());
            if (url == null || mainDep == null || !url.toString().contains(mainDep.getFile().toURI().toString())) {
                return null;
            }
            JavaClass nestedType = new JavaClass(getProjectClassLoader()).setPackage(clazz.getPackage().getName()).setName(clazz.getSimpleName()).setEnum(clazz.isEnum()).setClass(!clazz.isInterface()).setAbstract((clazz.getModifiers() & Modifier.ABSTRACT) != 0).setStatic((clazz.getModifiers() & Modifier.STATIC) != 0).extendSuperType(clazz.getGenericSuperclass() != null ? new GenericType(clazz.getGenericSuperclass()).toString() : null);
            List<java.lang.reflect.Method> publicMethods = Stream.of(clazz.getDeclaredMethods()).filter(m -> Modifier.isPublic(m.getModifiers())).collect(Collectors.toList());
            List<java.lang.reflect.Method> allSetters = publicMethods.stream().filter(m -> m.getReturnType() == void.class || m.getReturnType() == clazz).filter(m -> m.getParameterCount() == 1).filter(m -> m.getName().matches("set[A-Z][a-zA-Z0-9]*")).collect(Collectors.toList());
            List<java.lang.reflect.Method> allGetters = publicMethods.stream().filter(m -> m.getReturnType() != void.class).filter(m -> m.getParameterCount() == 0).filter(m -> m.getName().matches("(get|is)[A-Z][a-zA-Z0-9]*")).collect(Collectors.toList());
            allSetters.stream().sorted(Comparator.<java.lang.reflect.Method>comparingInt(m -> getSetterPosition(sourceCode, m)).thenComparing(java.lang.reflect.Method::getName)).map(m -> StringUtils.uncapitalize(m.getName().substring(3))).forEach(fn -> {
                Class<?> ft;
                Type wft;
                boolean isBoolean;
                java.lang.reflect.Field field = Stream.of(clazz.getDeclaredFields()).filter(f -> f.getName().equals(fn)).findAny().orElse(null);
                List<java.lang.reflect.Method> setters = allSetters.stream().filter(m -> m.getName().equals("set" + StringUtils.capitalize(fn))).collect(Collectors.toList());
                List<java.lang.reflect.Method> getters = allGetters.stream().filter(m -> m.getName().equals("get" + StringUtils.capitalize(fn)) || m.getName().equals("is" + StringUtils.capitalize(fn))).collect(Collectors.toList());
                java.lang.reflect.Method mutator;
                java.lang.reflect.Method accessor;
                if (setters.size() == 1) {
                    mutator = setters.get(0);
                    ft = mutator.getParameterTypes()[0];
                    wft = PRIMITIVE_CLASSES.getOrDefault(ft, ft);
                    isBoolean = ft == boolean.class || ft == Boolean.class;
                    accessor = allGetters.stream().filter(m -> m.getName().equals("get" + StringUtils.capitalize(fn)) || isBoolean && m.getName().equals("is" + StringUtils.capitalize(fn))).filter(m -> PRIMITIVE_CLASSES.getOrDefault(m.getReturnType(), m.getReturnType()) == wft).findAny().orElse(null);
                } else if (field != null) {
                    ft = field.getType();
                    wft = PRIMITIVE_CLASSES.getOrDefault(ft, ft);
                    isBoolean = ft == boolean.class || ft == Boolean.class;
                    mutator = allSetters.stream().filter(m -> m.getName().equals("set" + StringUtils.capitalize(fn))).filter(m -> PRIMITIVE_CLASSES.getOrDefault(m.getParameterTypes()[0], m.getParameterTypes()[0]) == wft).findAny().orElse(null);
                    accessor = allGetters.stream().filter(m -> m.getName().equals("get" + StringUtils.capitalize(fn)) || isBoolean && m.getName().equals("is" + StringUtils.capitalize(fn))).filter(m -> PRIMITIVE_CLASSES.getOrDefault(m.getReturnType(), m.getReturnType()) == wft).findAny().orElse(null);
                } else {
                    if (getters.size() == 1) {
                        ft = getters.get(0).getReturnType();
                    } else {
                        throw new IllegalStateException("Unable to determine type for property " + fn);
                    }
                    wft = PRIMITIVE_CLASSES.getOrDefault(ft, ft);
                    mutator = setters.stream().filter(m -> PRIMITIVE_CLASSES.getOrDefault(m.getParameterTypes()[0], m.getParameterTypes()[0]) == wft).findAny().orElse(null);
                    accessor = getters.stream().filter(m -> PRIMITIVE_CLASSES.getOrDefault(m.getReturnType(), m.getReturnType()) == wft).findAny().orElse(null);
                }
                if (mutator == null) {
                    throw new IllegalStateException("Could not find mutator for property " + fn);
                }
                Property property = nestedType.addProperty(new GenericType(wft), fn);
                property.getMutator().getJavaDoc().setText(getSetterJavaDoc(sourceCode, fn));
                for (java.lang.annotation.Annotation ann : mutator.getAnnotations()) {
                    addAnnotation(ac -> property.getMutator().addAnnotation(ac), ann);
                }
                if (accessor != null) {
                    for (java.lang.annotation.Annotation ann : accessor.getAnnotations()) {
                        addAnnotation(ac -> property.getAccessor().addAnnotation(ac), ann);
                    }
                } else {
                    property.removeAccessor();
                }
                if (field != null) {
                    for (java.lang.annotation.Annotation ann : field.getAnnotations()) {
                        addAnnotation(ac -> property.getField().addAnnotation(ac), ann);
                    }
                } else {
                    property.removeField();
                }
            });
            return nestedType;
        } catch (ClassNotFoundException e) {
            return null;
        }
    }
    return null;
}
Also used : DataFormatModel(org.apache.camel.tooling.model.DataFormatModel) URL(java.net.URL) Autowired(org.springframework.beans.factory.annotation.Autowired) JarFile(java.util.jar.JarFile) Parameter(org.apache.maven.plugins.annotations.Parameter) StringUtils(org.apache.commons.lang3.StringUtils) Method(org.apache.camel.tooling.util.srcgen.Method) ComponentModel(org.apache.camel.tooling.model.ComponentModel) Generated(javax.annotation.Generated) Duration(java.time.Duration) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Artifact(org.apache.maven.artifact.Artifact) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) ZipEntry(java.util.zip.ZipEntry) PackageHelper(org.apache.camel.tooling.util.PackageHelper) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) LanguageOptionModel(org.apache.camel.tooling.model.LanguageModel.LanguageOptionModel) GenericType(org.apache.camel.tooling.util.srcgen.GenericType) Property(org.apache.camel.tooling.util.srcgen.Property) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Configuration(org.springframework.context.annotation.Configuration) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) ComponentOptionModel(org.apache.camel.tooling.model.ComponentModel.ComponentOptionModel) Lazy(org.springframework.context.annotation.Lazy) Conditional(org.springframework.context.annotation.Conditional) LanguageModel(org.apache.camel.tooling.model.LanguageModel) java.util(java.util) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) JavaClass(org.apache.camel.tooling.util.srcgen.JavaClass) Function(java.util.function.Function) Supplier(java.util.function.Supplier) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) Mojo(org.apache.maven.plugins.annotations.Mojo) AbstractGeneratorMojo(org.apache.camel.maven.packaging.AbstractGeneratorMojo) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) EipOptionModel(org.apache.camel.tooling.model.EipModel.EipOptionModel) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) DataFormatOptionModel(org.apache.camel.tooling.model.DataFormatModel.DataFormatOptionModel) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) Strings.camelCaseToDash(org.apache.camel.tooling.util.Strings.camelCaseToDash) MojoFailureException(org.apache.maven.plugin.MojoFailureException) JsonMapper(org.apache.camel.tooling.model.JsonMapper) Annotation(org.apache.camel.tooling.util.srcgen.Annotation) Strings(org.apache.camel.tooling.model.Strings) EipModel(org.apache.camel.tooling.model.EipModel) Bean(org.springframework.context.annotation.Bean) InputStream(java.io.InputStream) GenericType(org.apache.camel.tooling.util.srcgen.GenericType) Method(org.apache.camel.tooling.util.srcgen.Method) URL(java.net.URL) Artifact(org.apache.maven.artifact.Artifact) GenericType(org.apache.camel.tooling.util.srcgen.GenericType) Type(java.lang.reflect.Type) JavaClass(org.apache.camel.tooling.util.srcgen.JavaClass) Property(org.apache.camel.tooling.util.srcgen.Property) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Modifier (java.lang.reflect.Modifier)1 Type (java.lang.reflect.Type)1 URL (java.net.URL)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Duration (java.time.Duration)1 java.util (java.util)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 JarFile (java.util.jar.JarFile)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ZipEntry (java.util.zip.ZipEntry)1 Generated (javax.annotation.Generated)1 AbstractGeneratorMojo (org.apache.camel.maven.packaging.AbstractGeneratorMojo)1 ComponentModel (org.apache.camel.tooling.model.ComponentModel)1 ComponentOptionModel (org.apache.camel.tooling.model.ComponentModel.ComponentOptionModel)1 DataFormatModel (org.apache.camel.tooling.model.DataFormatModel)1