Search in sources :

Example 16 with ExecutableType

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

the class MethodRetrievalProcessor method getReferencedMethod.

private SourceMethod getReferencedMethod(TypeElement usedMapper, ExecutableType methodType, ExecutableElement method, TypeElement mapperToImplement, List<Parameter> parameters) {
    Type returnType = typeFactory.getReturnType(methodType);
    List<Type> exceptionTypes = typeFactory.getThrownTypes(methodType);
    Type usedMapperAsType = typeFactory.getType(usedMapper);
    Type mapperToImplementAsType = typeFactory.getType(mapperToImplement);
    if (!mapperToImplementAsType.canAccess(usedMapperAsType, method)) {
        return null;
    }
    Type definingType = typeFactory.getType(method.getEnclosingElement().asType());
    return new SourceMethod.Builder().setDeclaringMapper(usedMapper.equals(mapperToImplement) ? null : usedMapperAsType).setDefininingType(definingType).setExecutable(method).setParameters(parameters).setReturnType(returnType).setExceptionTypes(exceptionTypes).setTypeUtils(typeUtils).setTypeFactory(typeFactory).build();
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 17 with ExecutableType

use of javax.lang.model.type.ExecutableType in project j2objc by google.

the class MethodTranslator method visitSuperReferenceExpression.

@Override
public TreeNode visitSuperReferenceExpression(SuperReferenceExpression node, Void data) {
    TypeMirror objType = typeUtil.getJavaObject().asType();
    TypeMirror nodeType = typeDecl.getTypeElement().asType();
    assert !parserEnv.typeUtilities().isSameType(objType, nodeType);
    TypeElement superClass = TranslationUtil.getSuperType(typeDecl);
    SuperConstructorInvocation superCall = null;
    for (ExecutableElement exec : ElementUtil.getConstructors(superClass)) {
        if (exec.getParameters().size() == 0) {
            ExecutableType execType = typeUtil.asMemberOf((DeclaredType) superClass.asType(), exec);
            superCall = new SuperConstructorInvocation().setExecutablePair(new ExecutablePair(exec, execType));
            break;
        }
    }
    return superCall;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation)

Example 18 with ExecutableType

use of javax.lang.model.type.ExecutableType in project j2objc by google.

the class TreeConverter method convertFunctionalExpression.

private TreeNode convertFunctionalExpression(JCFunctionalExpression node, TreePath parent, FunctionalExpression newNode) {
    List<? extends TypeMirror> targets = getTargets(node, parent);
    for (TypeMirror type : targets) {
        newNode.addTargetType(type);
    }
    Types types = Types.instance(((com.sun.tools.javac.api.BasicJavacTask) env.task()).getContext());
    return newNode.setTypeMirror(targets.iterator().next()).setDescriptor(new ExecutablePair((ExecutableElement) types.findDescriptorSymbol(((com.sun.tools.javac.code.Type) targets.get(0)).tsym), (ExecutableType) node.getDescriptorType(types)));
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) Types(com.sun.tools.javac.code.Types) ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 19 with ExecutableType

use of javax.lang.model.type.ExecutableType in project j2objc by google.

the class AbstractMethodRewriter method addReturnTypeNarrowingDeclarations.

// Adds declarations for any methods where the known return type is more
// specific than what is already declared in inherited types.
private void addReturnTypeNarrowingDeclarations(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    // No need to run this if the entire class is dead.
    if (deadCodeMap != null && deadCodeMap.containsClass(type, elementUtil)) {
        return;
    }
    Map<String, ExecutablePair> newDeclarations = new HashMap<>();
    Map<String, TypeMirror> resolvedReturnTypes = new HashMap<>();
    for (DeclaredType inheritedType : typeUtil.getObjcOrderedInheritedTypes(type.asType())) {
        TypeElement inheritedElem = (TypeElement) inheritedType.asElement();
        for (ExecutableElement methodElem : ElementUtil.getMethods(inheritedElem)) {
            if (ElementUtil.isPrivate(methodElem)) {
                continue;
            }
            TypeMirror declaredReturnType = typeUtil.erasure(methodElem.getReturnType());
            if (!TypeUtil.isReferenceType(declaredReturnType)) {
                // Short circuit
                continue;
            }
            String selector = nameTable.getMethodSelector(methodElem);
            ExecutableType methodType = typeUtil.asMemberOf(inheritedType, methodElem);
            TypeMirror returnType = typeUtil.erasure(methodType.getReturnType());
            TypeMirror resolvedReturnType = resolvedReturnTypes.get(selector);
            if (resolvedReturnType == null) {
                resolvedReturnType = declaredReturnType;
                resolvedReturnTypes.put(selector, resolvedReturnType);
            } else if (!typeUtil.isSubtype(returnType, resolvedReturnType)) {
                continue;
            }
            if (resolvedReturnType != returnType && !nameTable.getObjCType(resolvedReturnType).equals(nameTable.getObjCType(returnType))) {
                newDeclarations.put(selector, new ExecutablePair(methodElem, methodType));
                resolvedReturnTypes.put(selector, returnType);
            }
        }
    }
    for (Map.Entry<String, ExecutablePair> newDecl : newDeclarations.entrySet()) {
        if (deadCodeMap != null && deadCodeMap.containsMethod(newDecl.getValue().element(), typeUtil)) {
            continue;
        }
        node.addBodyDeclaration(newReturnTypeNarrowingDeclaration(newDecl.getKey(), newDecl.getValue(), type));
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) HashMap(java.util.HashMap) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) HashMap(java.util.HashMap) Map(java.util.Map) CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) DeclaredType(javax.lang.model.type.DeclaredType)

Example 20 with ExecutableType

use of javax.lang.model.type.ExecutableType in project tiger by google.

the class HubInjectorGenerator method generateProvisionAndInjectionMethods.

private void generateProvisionAndInjectionMethods() {
    // Injection methods and non-injection methods.
    Set<String> miscMethodNames = new HashSet<>();
    DeclaredType eitherComponentType = (DeclaredType) eitherComponent.asType();
    final int[] count = { 0 };
    utils.generateDebugInfoMethod(injectorBuilder, "generateProvisionAndInjectionMethods");
    utils.traverseAndDo(types, eitherComponentType, eitherComponent, p -> {
        Element element = p.second;
        // logger.n("" + element);
        if (!element.getKind().equals(ElementKind.METHOD)) {
            return null;
        }
        ExecutableElement method = (ExecutableElement) element;
        ExecutableType methodType = (ExecutableType) processingEnv.getTypeUtils().asMemberOf(eitherComponentType, method);
        // Injection methods.
        if (utils.isInjectionMethod(element)) {
            // TODO: add duplicate check for provision method also.
            if (injectionMethodsDone.add(Pair.of(method.getSimpleName().toString(), TypeName.get(Iterables.getOnlyElement(methodType.getParameterTypes())))) == false) {
                // logger.w("injection method: " + method);
                return null;
            }
            TypeMirror typeMirror = Iterables.getOnlyElement(methodType.getParameterTypes());
            TypeElement cls = (TypeElement) ((DeclaredType) typeMirror).asElement();
            // logger.n(methodType.toString());
            injectorBuilder.addMethod(MethodSpec.methodBuilder(method.getSimpleName().toString()).addModifiers(Modifier.PUBLIC).addParameter(ClassName.get(cls), "arg").addStatement("inject(arg)").build());
            count[0]++;
        } else if (utils.isComponentProvisionMethod(element)) {
            logger.l(Kind.ERROR, "Injecting components is not supported: " + element);
        } else if (utils.isSubcomponentProvisionMethod(element)) {
            /**
             * TODO: handle this in {@link #generateProvisionMethodIfNeeded(BindingKey)}
             */
            generateGetSubcomponentMethod((ExecutableElement) element, injectorBuilder);
            count[0]++;
        } else if (utils.isProvisionMethodInInjector(element)) {
            if (!miscMethodNames.add(method.getSimpleName().toString())) {
                return null;
            }
            MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getSimpleName().toString()).addModifiers(Modifier.PUBLIC).returns(TypeName.get(method.getReturnType()));
            BindingKey providedKey = utils.getKeyProvidedByMethod(method);
            // ClassName packagedInjectorClassName = null;
            // for (ClassName className : keyToPackagedInjectorMap.get(providedKey)) {
            // if (isInjectorOfScope(className, coreInjectorInfo.getScope())) {
            // packagedInjectorClassName = className;
            // break;
            // }
            // }
            // if (packagedInjectorClassName == null) {
            // logger.l(Kind.WARNING,
            // String.format(
            // "PackagedInjector or multiBindingInjector not found for key: %s "
            // + "from provisionMethod: %s. Probably it is not used.",
            // providedKey, method));
            // // Create a dumb method
            // String statement = "return ";
            // TypeKind typeKind = method.getReturnType().getKind();
            // if (typeKind.equals(TypeKind.BOOLEAN)) {
            // statement += "false";
            // } else if (typeKind.equals(TypeKind.CHAR)) {
            // statement += "\'0\'";
            // } else if (typeKind.isPrimitive()) {
            // statement += "0";
            // } else {
            // statement += "null";
            // }
            // methodBuilder.addStatement(statement);
            // } else {
            String statement = "return $L()";
            methodBuilder.addStatement(statement, Utils.getProvisionMethodName(dependencies, providedKey));
            // }
            // logger.n("method added: " + methodBuilder.build());
            injectorBuilder.addMethod(methodBuilder.build());
            count[0]++;
        // } else if (utils.isEitherComponentProvisionMethod(element)) {
        // // TODO: support get component method.
        // if(utils.isComponentProvisionMethod(element)) {
        // throw new RuntimeException("component provision method is not suported yet.");
        // }
        // generateGetSubcomponentMethod((ExecutableElement) element, injectorBuilder);
        // } else if (utils.isEitherComponentBuilderProvisionMethod(element)) {
        // /**
        // * TODO: handle it in the way consistent with other {@link DependencySourceType} in
        // * {@link #generateProvisionMethodIfNeeded(BindingKey, TypeElement)}
        // */
        // generateExplicitProvisionMethodForEitherComponentBuilder(
        // (ExecutableElement) element, injectorBuilder);
        } else if (utils.isIrrelevantMethodInInjector(element)) {
        // do nothing
        } else {
            logger.l(Kind.WARNING, "Element %s ignored from injector %s.", element, eitherComponentType);
        }
        return null;
    });
    logger.w("count: %d", count[0]);
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Builder(com.squareup.javapoet.TypeSpec.Builder) HashSet(java.util.HashSet) 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