Search in sources :

Example 41 with ExecutableType

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

the class Generator method buildPropMethod.

private static MethodSpec.Builder buildPropMethod(@Nonnull final ComponentDescriptor descriptor, @Nonnull final PropDescriptor prop) {
    final ExecutableElement methodElement = prop.getMethod();
    final ExecutableType methodType = prop.getMethodType();
    final TypeMirror returnType = methodType.getReturnType();
    final MethodSpec.Builder method = MethodSpec.methodBuilder(methodElement.getSimpleName().toString()).returns(TypeName.get(returnType));
    ProcessorUtil.copyTypeParameters(methodType, method);
    ProcessorUtil.copyAccessModifiers(methodElement, method);
    ProcessorUtil.copyDocumentedAnnotations(methodElement, method);
    method.addAnnotation(Override.class);
    final String name = prop.getName();
    if (descriptor.isArezComponent()) {
        final AnnotationSpec.Builder annotation = AnnotationSpec.builder(OBSERVABLE_ANNOTATION_CLASSNAME).addMember("name", "$S", name).addMember("expectSetter", "false");
        method.addAnnotation(annotation.build());
    }
    final Element propType = prop.getPropType();
    if (null != propType && descriptor.isArezComponent() && ElementKind.CLASS == propType.getKind()) {
        if (null != ProcessorUtil.findAnnotationByType(propType, Constants.AREZ_COMPONENT_ANNOTATION_CLASSNAME)) {
            method.addAnnotation(AnnotationSpec.builder(AREZ_DEPENDENCY_CLASSNAME).build());
        }
    }
    final String convertMethodName = getConverter(returnType, methodElement, "Prop");
    final String key = "child".equals(name) ? "children" : name;
    if (null == ProcessorUtil.findAnnotationByType(methodElement, Constants.NONNULL_ANNOTATION_CLASSNAME)) {
        method.addStatement("return props().has( $S ) ? props().getAny( $S ).$N() : null", key, key, convertMethodName);
    } else {
        method.addStatement("return props().getAny( $S ).$N()", key, convertMethodName);
    }
    return method;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) MethodSpec(com.squareup.javapoet.MethodSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) AnnotationSpec(com.squareup.javapoet.AnnotationSpec)

Example 42 with ExecutableType

use of javax.lang.model.type.ExecutableType in project lombok by rzwitserloot.

the class HandleDelegate method addMethodBindings.

public void addMethodBindings(List<MethodSig> signatures, ClassType ct, JavacTypes types, Set<String> banList) throws DelegateRecursion {
    TypeSymbol tsym = ct.asElement();
    if (tsym == null)
        return;
    for (Symbol member : tsym.getEnclosedElements()) {
        for (Compound am : member.getAnnotationMirrors()) {
            String name = null;
            try {
                name = am.type.tsym.flatName().toString();
            } catch (Exception ignore) {
            }
            if ("lombok.Delegate".equals(name) || "lombok.experimental.Delegate".equals(name)) {
                throw new DelegateRecursion(ct.tsym.name.toString(), member.name.toString());
            }
        }
        if (member.getKind() != ElementKind.METHOD)
            continue;
        if (member.isStatic())
            continue;
        if (member.isConstructor())
            continue;
        ExecutableElement exElem = (ExecutableElement) member;
        if (!exElem.getModifiers().contains(Modifier.PUBLIC))
            continue;
        ExecutableType methodType = (ExecutableType) types.asMemberOf(ct, member);
        String sig = printSig(methodType, member.name, types);
        // If add returns false, it was already in there
        if (!banList.add(sig))
            continue;
        boolean isDeprecated = (member.flags() & DEPRECATED) != 0;
        signatures.add(new MethodSig(member.name, methodType, isDeprecated, exElem));
    }
    if (ct.supertype_field instanceof ClassType)
        addMethodBindings(signatures, (ClassType) ct.supertype_field, types, banList);
    if (ct.interfaces_field != null)
        for (Type iface : ct.interfaces_field) {
            if (iface instanceof ClassType)
                addMethodBindings(signatures, (ClassType) iface, types, banList);
        }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ExecutableType(javax.lang.model.type.ExecutableType) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Compound(com.sun.tools.javac.code.Attribute.Compound) ClassType(com.sun.tools.javac.code.Type.ClassType) TypeNotConvertibleException(lombok.javac.JavacResolution.TypeNotConvertibleException)

Example 43 with ExecutableType

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

the class TypeFactory method getReturnType.

public Type getReturnType(DeclaredType includingType, Accessor accessor) {
    Type type;
    TypeMirror accessorType = getMethodType(includingType, accessor.getElement());
    if (isExecutableType(accessorType)) {
        type = getType(((ExecutableType) accessorType).getReturnType());
    } else {
        type = getType(accessorType);
    }
    return type;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) ExecutableType(javax.lang.model.type.ExecutableType) PrimitiveType(javax.lang.model.type.PrimitiveType) TypeMirror(javax.lang.model.type.TypeMirror)

Example 44 with ExecutableType

use of javax.lang.model.type.ExecutableType in project st-js by st-js.

the class JavaNodes method getEnclosingType.

/**
 * <p>getEnclosingType.</p>
 *
 * @param type a {@link javax.lang.model.type.TypeMirror} object.
 * @return a {@link javax.lang.model.type.DeclaredType} object.
 */
public static DeclaredType getEnclosingType(TypeMirror type) {
    if (!(type instanceof DeclaredType)) {
        return null;
    }
    DeclaredType declaredType = (DeclaredType) type;
    TypeMirror enclosingType = declaredType.asElement().getEnclosingElement().asType();
    if (enclosingType instanceof ExecutableType) {
        // get the type that encloses this method
        enclosingType = declaredType.asElement().getEnclosingElement().getEnclosingElement().asType();
    }
    if (enclosingType instanceof DeclaredType) {
        return (DeclaredType) enclosingType;
    }
    return null;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 45 with ExecutableType

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

the class MethodRetrievalProcessor method retrievePrototypeMethods.

private List<SourceMethod> retrievePrototypeMethods(TypeElement mapperTypeElement, MapperConfiguration mapperConfig) {
    if (mapperConfig.config() == null) {
        return Collections.emptyList();
    }
    TypeElement typeElement = asTypeElement(mapperConfig.config());
    List<SourceMethod> methods = new ArrayList<SourceMethod>();
    for (ExecutableElement executable : getAllEnclosedExecutableElements(elementUtils, typeElement)) {
        ExecutableType methodType = typeFactory.getMethodType(mapperConfig.config(), executable);
        List<Parameter> parameters = typeFactory.getParameters(methodType, executable);
        boolean containsTargetTypeParameter = SourceMethod.containsTargetTypeParameter(parameters);
        // prototype methods don't have prototypes themselves
        List<SourceMethod> prototypeMethods = Collections.emptyList();
        SourceMethod method = getMethodRequiringImplementation(methodType, executable, parameters, containsTargetTypeParameter, mapperConfig, prototypeMethods, mapperTypeElement);
        if (method != null) {
            methods.add(method);
        }
    }
    return methods;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

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