Search in sources :

Example 6 with ExecutableType

use of javax.lang.model.type.ExecutableType in project react4j by react4j.

the class ReactProcessor method determineRenderMethod.

private void determineRenderMethod(@Nonnull final TypeElement typeElement, @Nonnull final ComponentDescriptor descriptor) {
    /*
     * Get the render method that has been overridden by the typeElement, a parent class, or by a default
     * method method implemented by the typeElement or a parent class.
     */
    final ExecutableElement renderMethod = getComponentRenderMethod();
    final Elements elementUtils = processingEnv.getElementUtils();
    final Types typeUtils = processingEnv.getTypeUtils();
    final TypeElement componentType = elementUtils.getTypeElement(Constants.COMPONENT_CLASSNAME);
    final MethodDescriptor overriddenRenderMethod = // Get all methods on type parent classes, and default methods from interfaces
    ProcessorUtil.getMethods(typeElement, processingEnv.getTypeUtils()).stream().filter(m -> elementUtils.overrides(m, renderMethod, typeElement)).filter(m -> m.getEnclosingElement() != componentType).map(m -> new MethodDescriptor(m, (ExecutableType) typeUtils.asMemberOf(descriptor.getDeclaredType(), m))).findAny().orElse(null);
    if (null == overriddenRenderMethod) {
        throw new ReactProcessorException("The react component does not override any render methods.", typeElement);
    }
}
Also used : Arrays(java.util.Arrays) PackageElement(javax.lang.model.element.PackageElement) AbstractProcessor(javax.annotation.processing.AbstractProcessor) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) SupportedSourceVersion(javax.annotation.processing.SupportedSourceVersion) Kind(javax.tools.Diagnostic.Kind) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintWriter(java.io.PrintWriter) ElementKind(javax.lang.model.element.ElementKind) ExecutableType(javax.lang.model.type.ExecutableType) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ExecutableElement(javax.lang.model.element.ExecutableElement) Set(java.util.Set) IOException(java.io.IOException) Element(javax.lang.model.element.Element) TypeSpec(com.squareup.javapoet.TypeSpec) Processor(javax.annotation.processing.Processor) Types(javax.lang.model.util.Types) Collectors(java.util.stream.Collectors) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeKind(javax.lang.model.type.TypeKind) JavaFile(com.squareup.javapoet.JavaFile) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) RoundEnvironment(javax.annotation.processing.RoundEnvironment) AutoService(com.google.auto.service.AutoService) ExecutableType(javax.lang.model.type.ExecutableType) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements)

Example 7 with ExecutableType

use of javax.lang.model.type.ExecutableType in project react4j by react4j.

the class Generator method buildBuilderStepImpl.

@Nonnull
private static MethodSpec buildBuilderStepImpl(@Nonnull final Step step, @Nonnull final StepMethod stepMethod) {
    final MethodSpec.Builder method = MethodSpec.methodBuilder(stepMethod.getName());
    method.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
    method.addAnnotation(Override.class);
    method.addAnnotation(NONNULL_CLASSNAME);
    final ExecutableType propMethodType = stepMethod.getPropMethodType();
    if (null != propMethodType) {
        ProcessorUtil.copyTypeParameters(propMethodType, method);
    }
    final ParameterSpec.Builder parameter = ParameterSpec.builder(stepMethod.getType(), stepMethod.getName(), Modifier.FINAL);
    final ExecutableElement propMethod = stepMethod.getPropMethod();
    if (null != propMethod) {
        ProcessorUtil.copyDocumentedAnnotations(propMethod, parameter);
    } else if (stepMethod.isKeyIntrinsic() || stepMethod.isChildrenStreamIntrinsic()) {
        parameter.addAnnotation(NONNULL_CLASSNAME);
    } else if (stepMethod.isChildOfChildrenIntrinsic()) {
        parameter.addAnnotation(NULLABLE_CLASSNAME);
    }
    method.addParameter(parameter.build());
    if (stepMethod.isChildrenIntrinsic()) {
        method.varargs();
        final CodeBlock.Builder block = CodeBlock.builder();
        block.beginControlFlow("for ( final $T child : $N )", REACT_NODE_CLASSNAME, stepMethod.getName());
        block.addStatement("child( child )");
        block.endControlFlow();
        method.addCode(block.build());
    } else if (stepMethod.isChildOfChildrenIntrinsic()) {
        final CodeBlock.Builder block = CodeBlock.builder();
        block.beginControlFlow("if ( null != $N )", stepMethod.getName());
        block.addStatement("_children.push( $N )", stepMethod.getName());
        block.endControlFlow();
        method.addCode(block.build());
    } else if (stepMethod.isChildrenStreamIntrinsic()) {
        method.addStatement("children( $N.toArray( $T[]::new ) )", stepMethod.getName(), REACT_NODE_CLASSNAME);
    } else if (stepMethod.isChildIntrinsic()) {
        assert null != propMethod;
        if (null != ProcessorUtil.findAnnotationByType(propMethod, Constants.NONNULL_ANNOTATION_CLASSNAME)) {
            method.addStatement("_child = $T.requireNonNull( $N )", Objects.class, stepMethod.getName());
        } else {
            method.addStatement("_child = $N", stepMethod.getName());
        }
    } else if (stepMethod.isKeyIntrinsic() || (null != propMethod && null != ProcessorUtil.findAnnotationByType(propMethod, Constants.NONNULL_ANNOTATION_CLASSNAME))) {
        method.addStatement("_props.set( $S, $T.requireNonNull( $N ) )", stepMethod.getName(), Objects.class, stepMethod.getName());
    } else {
        method.addStatement("_props.set( $S, $N )", stepMethod.getName(), stepMethod.getName());
    }
    if (StepMethodType.TERMINATE == stepMethod.getStepMethodType()) {
        method.addStatement("return build()");
    } else {
        method.addStatement("return this");
    }
    configureStepMethodReturns(method, step, stepMethod.getStepMethodType());
    return method.build();
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeBlock(com.squareup.javapoet.CodeBlock) Objects(java.util.Objects) Nonnull(javax.annotation.Nonnull)

Example 8 with ExecutableType

use of javax.lang.model.type.ExecutableType in project hibernate-orm by hibernate.

the class AnnotationMetaEntity method isGetterOrSetter.

/**
 * Check if method respects Java Bean conventions for getter and setters.
 *
 * @param methodOfClass method element
 *
 * @return whether method respects Java Bean conventions.
 */
private boolean isGetterOrSetter(Element methodOfClass) {
    ExecutableType methodType = (ExecutableType) methodOfClass.asType();
    String methodSimpleName = methodOfClass.getSimpleName().toString();
    List<? extends TypeMirror> methodParameterTypes = methodType.getParameterTypes();
    TypeMirror returnType = methodType.getReturnType();
    if (methodSimpleName.startsWith("set") && methodParameterTypes.size() == 1 && "void".equalsIgnoreCase(returnType.toString())) {
        return true;
    } else if ((methodSimpleName.startsWith("get") || methodSimpleName.startsWith("is")) && methodParameterTypes.isEmpty() && !"void".equalsIgnoreCase(returnType.toString())) {
        return true;
    } else {
        return false;
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror)

Example 9 with ExecutableType

use of javax.lang.model.type.ExecutableType in project tutorials by eugenp.

the class BuilderProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    for (TypeElement annotation : annotations) {
        Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
        Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream().collect(Collectors.partitioningBy(element -> ((ExecutableType) element.asType()).getParameterTypes().size() == 1 && element.getSimpleName().toString().startsWith("set")));
        List<Element> setters = annotatedMethods.get(true);
        List<Element> otherMethods = annotatedMethods.get(false);
        otherMethods.forEach(element -> processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@BuilderProperty must be applied to a setXxx method with a single argument", element));
        if (setters.isEmpty()) {
            continue;
        }
        String className = ((TypeElement) setters.get(0).getEnclosingElement()).getQualifiedName().toString();
        Map<String, String> setterMap = setters.stream().collect(Collectors.toMap(setter -> setter.getSimpleName().toString(), setter -> ((ExecutableType) setter.asType()).getParameterTypes().get(0).toString()));
        try {
            writeBuilderFile(className, setterMap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return true;
}
Also used : PrintWriter(java.io.PrintWriter) ExecutableType(javax.lang.model.type.ExecutableType) Set(java.util.Set) IOException(java.io.IOException) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Collectors(java.util.stream.Collectors) javax.annotation.processing(javax.annotation.processing) JavaFileObject(javax.tools.JavaFileObject) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) AutoService(com.google.auto.service.AutoService) ExecutableType(javax.lang.model.type.ExecutableType) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) List(java.util.List) IOException(java.io.IOException)

Example 10 with ExecutableType

use of javax.lang.model.type.ExecutableType in project arez by arez.

the class ComponentDescriptor method analyzeCandidateMethods.

void analyzeCandidateMethods(@Nonnull final List<ExecutableElement> methods, @Nonnull final Types typeUtils) throws ArezProcessorException {
    final Map<String, CandidateMethod> getters = new HashMap<>();
    final Map<String, CandidateMethod> setters = new HashMap<>();
    final Map<String, CandidateMethod> trackeds = new HashMap<>();
    final Map<String, CandidateMethod> onDepsChangeds = new HashMap<>();
    for (final ExecutableElement method : methods) {
        final ExecutableType methodType = (ExecutableType) typeUtils.asMemberOf((DeclaredType) _element.asType(), method);
        if (!analyzeMethod(method, methodType)) {
            /*
         * If we get here the method was not annotated so we can try to detect if it is a
         * candidate arez method in case some arez annotations are implied via naming conventions.
         */
            if (method.getModifiers().contains(Modifier.STATIC)) {
                continue;
            }
            final CandidateMethod candidateMethod = new CandidateMethod(method, methodType);
            final boolean voidReturn = method.getReturnType().getKind() == TypeKind.VOID;
            final int parameterCount = method.getParameters().size();
            String name;
            if (!method.getModifiers().contains(Modifier.FINAL)) {
                name = ProcessorUtil.deriveName(method, SETTER_PATTERN, ProcessorUtil.SENTINEL_NAME);
                if (voidReturn && 1 == parameterCount && null != name) {
                    setters.put(name, candidateMethod);
                    continue;
                }
                name = ProcessorUtil.deriveName(method, ISSER_PATTERN, ProcessorUtil.SENTINEL_NAME);
                if (!voidReturn && 0 == parameterCount && null != name) {
                    getters.put(name, candidateMethod);
                    continue;
                }
                name = ProcessorUtil.deriveName(method, GETTER_PATTERN, ProcessorUtil.SENTINEL_NAME);
                if (!voidReturn && 0 == parameterCount && null != name) {
                    getters.put(name, candidateMethod);
                    continue;
                }
            }
            name = ProcessorUtil.deriveName(method, TrackedDescriptor.ON_DEPS_CHANGED_PATTERN, ProcessorUtil.SENTINEL_NAME);
            if (voidReturn && 0 == parameterCount && null != name) {
                onDepsChangeds.put(name, candidateMethod);
                continue;
            }
            final String methodName = method.getSimpleName().toString();
            if (!OBJECT_METHODS.contains(methodName)) {
                trackeds.put(methodName, candidateMethod);
            }
        }
    }
    linkUnAnnotatedObservables(getters, setters);
    linkUnAnnotatedTracked(trackeds, onDepsChangeds);
    linkObserverRefs();
    linkDependencies(getters.values());
    /*
     * ALl of the maps will have called remove() for all matching candidates.
     * Thus any left are the non-arez methods.
     */
    ensureNoAbstractMethods(getters.values());
    ensureNoAbstractMethods(setters.values());
    ensureNoAbstractMethods(trackeds.values());
    ensureNoAbstractMethods(onDepsChangeds.values());
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

ExecutableType (javax.lang.model.type.ExecutableType)68 ExecutableElement (javax.lang.model.element.ExecutableElement)53 DeclaredType (javax.lang.model.type.DeclaredType)41 TypeElement (javax.lang.model.element.TypeElement)39 TypeMirror (javax.lang.model.type.TypeMirror)39 Element (javax.lang.model.element.Element)29 VariableElement (javax.lang.model.element.VariableElement)27 TypeSpec (com.squareup.javapoet.TypeSpec)13 Nonnull (javax.annotation.Nonnull)13 PackageElement (javax.lang.model.element.PackageElement)13 HashMap (java.util.HashMap)12 Map (java.util.Map)12 MethodSpec (com.squareup.javapoet.MethodSpec)11 AnnotationMirror (javax.lang.model.element.AnnotationMirror)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 Nullable (javax.annotation.Nullable)10 JavaFile (com.squareup.javapoet.JavaFile)9 List (java.util.List)9