Search in sources :

Example 51 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 52 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)

Example 53 with ExecutableType

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

the class MethodRetrievalProcessor method getMethod.

private SourceMethod getMethod(TypeElement usedMapper, ExecutableElement method, TypeElement mapperToImplement, MapperConfiguration mapperConfig, List<SourceMethod> prototypeMethods) {
    ExecutableType methodType = typeFactory.getMethodType((DeclaredType) usedMapper.asType(), method);
    List<Parameter> parameters = typeFactory.getParameters(methodType, method);
    Type returnType = typeFactory.getReturnType(methodType);
    boolean methodRequiresImplementation = method.getModifiers().contains(Modifier.ABSTRACT);
    boolean containsTargetTypeParameter = SourceMethod.containsTargetTypeParameter(parameters);
    // add method with property mappings if an implementation needs to be generated
    if ((usedMapper.equals(mapperToImplement)) && methodRequiresImplementation) {
        return getMethodRequiringImplementation(methodType, method, parameters, containsTargetTypeParameter, mapperConfig, prototypeMethods, mapperToImplement);
    } else // otherwise add reference to existing mapper method
    if (isValidReferencedMethod(parameters) || isValidFactoryMethod(method, parameters, returnType) || isValidLifecycleCallbackMethod(method, returnType)) {
        return getReferencedMethod(usedMapper, methodType, method, mapperToImplement, parameters);
    } else {
        return null;
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 54 with ExecutableType

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

the class MethodRetrievalProcessor method getMethodRequiringImplementation.

private SourceMethod getMethodRequiringImplementation(ExecutableType methodType, ExecutableElement method, List<Parameter> parameters, boolean containsTargetTypeParameter, MapperConfiguration mapperConfig, List<SourceMethod> prototypeMethods, TypeElement mapperToImplement) {
    Type returnType = typeFactory.getReturnType(methodType);
    List<Type> exceptionTypes = typeFactory.getThrownTypes(methodType);
    List<Parameter> sourceParameters = Parameter.getSourceParameters(parameters);
    List<Parameter> contextParameters = Parameter.getContextParameters(parameters);
    Parameter targetParameter = extractTargetParameter(parameters);
    Type resultType = selectResultType(returnType, targetParameter);
    boolean isValid = checkParameterAndReturnType(method, sourceParameters, targetParameter, contextParameters, resultType, returnType, containsTargetTypeParameter);
    if (!isValid) {
        return null;
    }
    ParameterProvidedMethods contextProvidedMethods = retrieveLifecycleMethodsFromContext(contextParameters, mapperToImplement, mapperConfig);
    return new SourceMethod.Builder().setExecutable(method).setParameters(parameters).setReturnType(returnType).setExceptionTypes(exceptionTypes).setMappings(getMappings(method)).setIterableMapping(IterableMapping.fromPrism(IterableMappingPrism.getInstanceOn(method), method, messager, typeUtils)).setMapMapping(MapMapping.fromPrism(MapMappingPrism.getInstanceOn(method), method, messager, typeUtils)).setBeanMapping(BeanMapping.fromPrism(BeanMappingPrism.getInstanceOn(method), method, messager, typeUtils)).setValueMappings(getValueMappings(method)).setTypeUtils(typeUtils).setMessager(messager).setTypeFactory(typeFactory).setMapperConfiguration(mapperConfig).setPrototypeMethods(prototypeMethods).setContextProvidedMethods(contextProvidedMethods).build();
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType) ParameterProvidedMethods(org.mapstruct.ap.internal.model.source.ParameterProvidedMethods) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 55 with ExecutableType

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

the class BuilderGenerator method buildStaticStepMethodMethod.

@Nonnull
private static MethodSpec buildStaticStepMethodMethod(@Nonnull final ProcessingEnvironment processingEnv, @Nonnull final ViewDescriptor descriptor, @Nonnull final Step step, @Nonnull final StepMethod stepMethod) {
    final MethodSpec.Builder method = MethodSpec.methodBuilder(stepMethod.getName()).addAnnotation(GeneratorUtil.NONNULL_CLASSNAME);
    method.addModifiers(Modifier.STATIC);
    if (descriptor.getDeclaredType().asElement().getModifiers().contains(Modifier.PUBLIC)) {
        method.addModifiers(Modifier.PUBLIC);
    }
    GeneratorUtil.copyTypeParameters(descriptor.getElement(), method);
    if (stepMethod.isBuildIntrinsic()) {
        method.addStatement("return newBuilder().build()");
    } else {
        final TypeName type = stepMethod.getType();
        final ParameterSpec.Builder parameter = ParameterSpec.builder(type, stepMethod.getName(), Modifier.FINAL);
        final ExecutableElement inputMethod = stepMethod.getMethod();
        if (null != inputMethod) {
            GeneratorUtil.copyWhitelistedAnnotations(inputMethod, parameter);
            final ExecutableType methodType = stepMethod.getMethodType();
            assert null != methodType;
            SuppressWarningsUtil.addSuppressWarningsIfRequired(processingEnv, parameter, methodType.getReturnType());
        } else if (stepMethod.isChildrenStreamIntrinsic()) {
            parameter.addAnnotation(GeneratorUtil.NONNULL_CLASSNAME);
        }
        if (type instanceof ArrayTypeName) {
            method.varargs();
        }
        method.addParameter(parameter.build());
        final String infix = asTypeArgumentsInfix(descriptor.getDeclaredType());
        if (infix.isEmpty()) {
            // No type parameters
            method.addStatement("return newBuilder().$N( $N )", stepMethod.getName(), stepMethod.getName());
        } else {
            method.addStatement("return $T." + infix + "newBuilder().$N( $N )", descriptor.getBuilderClassName(), stepMethod.getName(), stepMethod.getName());
        }
    }
    configureStepMethodReturns(descriptor, method, step, stepMethod.getStepMethodType());
    return method.build();
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ExecutableElement(javax.lang.model.element.ExecutableElement) Nonnull(javax.annotation.Nonnull)

Aggregations

ExecutableType (javax.lang.model.type.ExecutableType)83 ExecutableElement (javax.lang.model.element.ExecutableElement)64 DeclaredType (javax.lang.model.type.DeclaredType)47 TypeMirror (javax.lang.model.type.TypeMirror)45 TypeElement (javax.lang.model.element.TypeElement)44 Element (javax.lang.model.element.Element)33 VariableElement (javax.lang.model.element.VariableElement)33 Nonnull (javax.annotation.Nonnull)22 ArrayList (java.util.ArrayList)18 MethodSpec (com.squareup.javapoet.MethodSpec)15 HashMap (java.util.HashMap)15 Map (java.util.Map)15 AnnotationMirror (javax.lang.model.element.AnnotationMirror)15 TypeSpec (com.squareup.javapoet.TypeSpec)14 HashSet (java.util.HashSet)14 TypeKind (javax.lang.model.type.TypeKind)14 IOException (java.io.IOException)13 Nullable (javax.annotation.Nullable)13 PackageElement (javax.lang.model.element.PackageElement)13 List (java.util.List)12