Search in sources :

Example 31 with ExecutableType

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

the class ProxyGenerator method addMethodCallingMethodOrCtor.

/**
 * Handles ctor, class methods including module methods.
 */
private void addMethodCallingMethodOrCtor(Builder typeBuilder, TypeMirror returnType, TypeElement container, ExecutableElement executableElement, String generatedMethodName) {
    boolean isCtor = executableElement.getKind().equals(ElementKind.CONSTRUCTOR);
    boolean isStatic = executableElement.getModifiers().contains(Modifier.STATIC);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(generatedMethodName).addModifiers(Modifier.PUBLIC);
    boolean hasReturnValue = !returnType.getKind().equals(TypeKind.VOID);
    if (hasReturnValue) {
        methodSpecBuilder.returns(ClassName.get(returnType));
    }
    if (isCtor) {
        addTypeVariablesToMethod(container, methodSpecBuilder);
    }
    TypeName moduleTypeName = isCtor ? null : ClassName.get(container.asType());
    List<TypeName> types = new ArrayList<>();
    TypeName returnTypeName = ClassName.get(returnType);
    types.add(returnTypeName);
    if (isCtor) {
        types.add(returnTypeName);
    } else if (isStatic) {
        types.add(moduleTypeName);
    }
    StringBuilder builder = new StringBuilder();
    if (hasReturnValue) {
        builder.append("$T result = ");
    }
    if (isCtor) {
        builder.append("new $T(");
    } else if (isStatic) {
        builder.append("$T.").append(executableElement.getSimpleName()).append("(");
    } else {
        methodSpecBuilder.addParameter(moduleTypeName, "module");
        builder.append("module.").append(executableElement.getSimpleName()).append("(");
    }
    String varNameBase = "var";
    int varOrdinal = 1;
    List<? extends TypeMirror> arguments = ((ExecutableType) executableElement.asType()).getParameterTypes();
    for (TypeMirror parameter : arguments) {
        String varName = varNameBase + varOrdinal;
        TypeName typeName = TypeName.get(parameter);
        methodSpecBuilder.addParameter(toAccessibleType(typeName), varName);
        if (!utils.isPublicallyAccessible(typeName)) {
            types.add(typeName);
            builder.append("($T) ");
        }
        builder.append(varName).append(", ");
        varOrdinal++;
    }
    if (arguments.size() > 0) {
        builder.delete(builder.length() - 2, builder.length());
    }
    builder.append(")");
    methodSpecBuilder.addStatement(builder.toString(), types.toArray());
    if (hasReturnValue) {
        methodSpecBuilder.addStatement("return result");
    }
    typeBuilder.addMethod(methodSpecBuilder.build());
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList)

Example 32 with ExecutableType

use of javax.lang.model.type.ExecutableType in project ceylon-compiler by ceylon.

the class P method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (!ran) {
        ran = true;
        ExecutableElement m = getFirstMethodIn("C");
        System.err.println("method: " + m);
        TypeMirror type = (DeclaredType) m.getParameters().get(0).asType();
        System.err.println("parameters[0]: " + type);
        if (!isParameterized(type))
            throw new AssertionError(type);
        type = ((ExecutableType) m.asType()).getParameterTypes().get(0);
        System.err.println("parameterTypes[0]: " + type);
        if (!isParameterized(type))
            throw new AssertionError(type);
        System.err.println();
    }
    return true;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 33 with ExecutableType

use of javax.lang.model.type.ExecutableType in project vertx-docgen by vert-x3.

the class JavaDocGenerator method toExecutableLink.

private String toExecutableLink(ExecutableElement elt, String name) {
    TypeElement typeElt = (TypeElement) elt.getEnclosingElement();
    String link = resolveTypeLink(typeElt);
    StringBuilder anchor = new StringBuilder("#");
    anchor.append(name).append('-');
    TypeMirror type = elt.asType();
    ExecutableType methodType = (ExecutableType) processingEnv.getTypeUtils().erasure(type);
    List<? extends TypeMirror> parameterTypes = methodType.getParameterTypes();
    for (int i = 0; i < parameterTypes.size(); i++) {
        if (i > 0) {
            anchor.append('-');
        }
        // We need to check whether or not the parameter is annotated. In this case, we must use the unannotated type.
        TypeMirror typeOfParameter = parameterTypes.get(i);
        String s = typeOfParameter.toString();
        Matcher matcher = A.matcher(s);
        if (matcher.matches()) {
            String t = matcher.group(1);
            anchor.append(t);
        } else {
            anchor.append(s);
        }
    }
    anchor.append('-');
    return link + anchor;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) Matcher(java.util.regex.Matcher) TypeElement(javax.lang.model.element.TypeElement)

Example 34 with ExecutableType

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

the class TreeConverter method convertMethodInvocation.

private TreeNode convertMethodInvocation(JCTree.JCMethodInvocation node) {
    JCTree.JCExpression method = node.getMethodSelect();
    String methodName = getMemberName(method);
    ExecutableType type = (ExecutableType) method.type;
    Symbol.MethodSymbol sym = (Symbol.MethodSymbol) getMemberSymbol(method);
    JCTree.JCExpression target = method.getKind() == Kind.MEMBER_SELECT ? ((JCTree.JCFieldAccess) method).selected : null;
    if ("this".equals(methodName)) {
        ConstructorInvocation newNode = new ConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    if ("super".equals(methodName)) {
        SuperConstructorInvocation newNode = new SuperConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
        if (target != null) {
            newNode.setExpression((Expression) convert(target));
        }
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    if (target != null && "super".equals(getMemberName(target))) {
        SuperMethodInvocation newNode = new SuperMethodInvocation().setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement).setName(convertSimpleName(sym, type, getPosition(node)));
        if (target.getKind() == Kind.MEMBER_SELECT) {
            // foo.bar.MyClass.super.print(...):
            //   target: foo.bar.MyClass.super
            //   target.selected: foo.bar.MyClass
            newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) target).selected));
        }
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    MethodInvocation newNode = new MethodInvocation();
    newNode.setName(convertSimpleName(sym, type, getPosition(method)));
    if (target != null) {
        newNode.setExpression((Expression) convert(target));
    }
    for (JCTree.JCExpression arg : node.getArguments()) {
        newNode.addArgument((Expression) convert(arg));
    }
    return newNode.setTypeMirror(node.type).setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement);
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ConstructorInvocation(com.google.devtools.j2objc.ast.ConstructorInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCTree(com.sun.tools.javac.tree.JCTree) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Example 35 with ExecutableType

use of javax.lang.model.type.ExecutableType in project epoxy by airbnb.

the class ClassToGenerateInfo method collectMethodsReturningClassType.

/**
   * Get information about methods returning class type of the original class so we can duplicate
   * them in the generated class for chaining purposes
   */
private void collectMethodsReturningClassType(TypeElement originalClass) {
    TypeElement clazz = originalClass;
    while (clazz.getSuperclass().getKind() != TypeKind.NONE) {
        for (Element subElement : clazz.getEnclosedElements()) {
            Set<Modifier> modifiers = subElement.getModifiers();
            if (subElement.getKind() == ElementKind.METHOD && !modifiers.contains(Modifier.PRIVATE) && !modifiers.contains(Modifier.FINAL) && !modifiers.contains(Modifier.STATIC)) {
                TypeMirror methodReturnType = ((ExecutableType) subElement.asType()).getReturnType();
                if (methodReturnType.equals(clazz.asType()) || typeUtils.isSubtype(clazz.asType(), methodReturnType)) {
                    ExecutableElement castedSubElement = ((ExecutableElement) subElement);
                    List<? extends VariableElement> params = castedSubElement.getParameters();
                    String methodName = subElement.getSimpleName().toString();
                    if (methodName.equals(RESET_METHOD) && params.isEmpty()) {
                        continue;
                    }
                    methodsReturningClassType.add(new MethodInfo(methodName, modifiers, buildParamList(params), castedSubElement.isVarArgs()));
                }
            }
        }
        clazz = (TypeElement) typeUtils.asElement(clazz.getSuperclass());
    }
}
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) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Modifier(javax.lang.model.element.Modifier)

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