javax.lang.model.type.ExecutableType Java Examples

The following examples show how to use javax.lang.model.type.ExecutableType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeyTest.java    From dagger2-sample with Apache License 2.0 6 votes vote down vote up
@Test public void qualifiedKeyEquivalents() {
  TypeElement moduleElement =
      elements.getTypeElement(QualifiedProvidesMethodModule.class.getCanonicalName());
  ExecutableElement providesMethod =
      Iterables.getOnlyElement(ElementFilter.methodsIn(moduleElement.getEnclosedElements()));
  Key provisionKey =
      keyFactory.forProvidesMethod((ExecutableType) providesMethod.asType(), providesMethod);

  TypeMirror type = elements.getTypeElement(String.class.getCanonicalName()).asType();
  TypeElement injectableElement =
      elements.getTypeElement(QualifiedFieldHolder.class.getCanonicalName());
  Element injectionField =
      Iterables.getOnlyElement(ElementFilter.fieldsIn(injectableElement.getEnclosedElements()));
  AnnotationMirror qualifier = Iterables.getOnlyElement(injectionField.getAnnotationMirrors());
  Key injectionKey = keyFactory.forQualifiedType(Optional.<AnnotationMirror>of(qualifier), type);

  assertThat(provisionKey).isEqualTo(injectionKey);
}
 
Example #2
Source File: ElementHandle.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
@Override 
protected ExecutableElement doGet(ProcessingEnvironment env) { 
  TypeElement typeElt = env.getElementUtils().getTypeElement(fqn.getName()); 
  if (typeElt != null) { 
    next: 
    for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElt.getEnclosedElements())) {
      if (executableElement.getSimpleName().toString().equals(name)) { 
        List<? extends TypeMirror> parameterTypes = ((ExecutableType)executableElement.asType()).getParameterTypes(); 
        int len = parameterTypes.size(); 
        if (len == this.parameterTypes.size()) { 
          for (int i = 0;i < len;i++) { 
            if (!parameterTypes.get(i).toString().equals(this.parameterTypes.get(i))) { 
              continue next; 
            } 
          } 
          return executableElement; 
        } 
      } 
    } 
  } 
  return null; 
}
 
Example #3
Source File: T6468404.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example #4
Source File: T6468404.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example #5
Source File: DelegateMethodAnalyzer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkDelegateType( VariableElement element, int i,
        ExecutableElement method, TypeElement parent,
        CdiAnalysisResult result  )
{
    ExecutableType methodType = (ExecutableType) result.getInfo().getTypes()
            .asMemberOf((DeclaredType) parent.asType(), method);
    List<? extends TypeMirror> parameterTypes = methodType
            .getParameterTypes();
    TypeMirror parameterType = parameterTypes.get(i);
    Collection<TypeMirror> decoratedTypes = DelegateFieldAnalizer
            .getDecoratedTypes(parent, result.getInfo());
    for (TypeMirror decoratedType : decoratedTypes) {
        if (!result.getInfo().getTypes().isSubtype(parameterType, decoratedType)) {
            result.addError(element,  NbBundle.getMessage(
                            DelegateMethodAnalyzer.class,
                            "ERR_DelegateTypeHasNoDecoratedType")); // NOI18N
            return;
        }
    }
}
 
Example #6
Source File: DecoratorInterceptorLogic.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Triple<VariableElement, TypeMirror, Void> getDelegate(
        ExecutableElement method, TypeElement decorator )
{
    List<? extends VariableElement> parameters = method.getParameters();
    int index =0;
    VariableElement delegate = null;
    for (VariableElement variableElement : parameters) {
        if ( AnnotationObjectProvider.hasAnnotation(variableElement, 
                DELEGATE_ANNOTATION, getModel().getHelper()))
        {
            delegate = variableElement;
            break;
        }
        index ++;
    }
    if ( delegate == null ){
        return null;
    }
    ExecutableType methodType = (ExecutableType)getCompilationController().
        getTypes().asMemberOf((DeclaredType)decorator.asType(), method );
    List<? extends TypeMirror> parameterTypes = methodType.getParameterTypes();
    TypeMirror typeMirror = parameterTypes.get(index);
    return new Triple<VariableElement, TypeMirror, Void>(delegate, typeMirror, null);
}
 
Example #7
Source File: MoreTypes.java    From auto-parcel with Apache License 2.0 6 votes vote down vote up
private static boolean equal(TypeMirror a, TypeMirror b, Set<ComparedElements> visiting) {
    // TypeMirror.equals is not guaranteed to return true for types that are equal, but we can
    // assume that if it does return true then the types are equal. This check also avoids getting
    // stuck in infinite recursion when Eclipse decrees that the upper bound of the second K in
    // <K extends Comparable<K>> is a distinct but equal K.
    // The javac implementation of ExecutableType, at least in some versions, does not take thrown
    // exceptions into account in its equals implementation, so avoid this optimization for
    // ExecutableType.
    if (Objects.equal(a, b) && !(a instanceof ExecutableType)) {
        return true;
    }
    EqualVisitorParam p = new EqualVisitorParam();
    p.type = b;
    p.visiting = visiting;
    if (INTERSECTION_TYPE != null) {
        if (isIntersectionType(a)) {
            return equalIntersectionTypes(a, b, visiting);
        } else if (isIntersectionType(b)) {
            return false;
        }
    }
    return (a == b) || (a != null && b != null && a.accept(EQUAL_VISITOR, p));
}
 
Example #8
Source File: ElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isHidden(Element member, List<? extends Element> members, Elements elements, Types types) {
    for (ListIterator<? extends Element> it = members.listIterator(); it.hasNext();) {
        Element hider = it.next();
        if (hider == member)
            return true;
        if (hider.getSimpleName().contentEquals(member.getSimpleName())) {
            if (elements.hides(member, hider)) {
                it.remove();
            } else {
                if (member instanceof VariableElement && hider instanceof VariableElement
                        && (!member.getKind().isField() || hider.getKind().isField()))
                    return true;
                TypeMirror memberType = member.asType();
                TypeMirror hiderType = hider.asType();
                if (memberType.getKind() == TypeKind.EXECUTABLE && hiderType.getKind() == TypeKind.EXECUTABLE) {
                    if (types.isSubsignature((ExecutableType)hiderType, (ExecutableType)memberType))
                        return true;
                } else {
                    return false;
                }
            }
        }
    }
    return false;
}
 
Example #9
Source File: AnnotationsAnalyzer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkInitializerMethod( ExecutableElement element, 
        TypeElement parent, CdiAnalysisResult result )
{
    Set<Modifier> modifiers = element.getModifiers();
    boolean isAbstract = modifiers.contains( Modifier.ABSTRACT );
    boolean isStatic = modifiers.contains( Modifier.STATIC );
    if (  isAbstract || isStatic ){
        String key = isAbstract? "ERR_AbstractInitMethod":
            "ERR_StaticInitMethod";           // NOI18N
        result.addError( element, NbBundle.getMessage(
            AnnotationsAnalyzer.class, key ));
    }    
    TypeMirror method = result.getInfo().getTypes().asMemberOf(
            (DeclaredType)parent.asType() , element);
    if ( method instanceof ExecutableType ){
        List<? extends TypeVariable> typeVariables = 
            ((ExecutableType)method).getTypeVariables();
        if ( typeVariables != null && typeVariables.size() > 0 ){
            result.addError( element, NbBundle.getMessage(
                        AnnotationsAnalyzer.class, "ERR_GenericInitMethod" ));// NOI18N
        }
    }
}
 
Example #10
Source File: T6468404.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example #11
Source File: MethodElementAnalyzer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void analyze( Element element, TypeElement parent,
        AtomicBoolean cancel, CdiAnalysisResult result )
{
    ExecutableElement method = (ExecutableElement) element;
    TypeMirror methodType = result.getInfo().getTypes().asMemberOf( 
            (DeclaredType)parent.asType(),  method );
    if ( methodType instanceof ExecutableType ){
        if ( cancel.get()){
            return;
        }
        TypeMirror returnType = ((ExecutableType) methodType).getReturnType();
        for (MethodAnalyzer analyzer : ANALYZERS) {
            if ( cancel.get() ){
                return;
            }
            analyzer.analyze(method, returnType, parent, cancel, 
                    result );
        }
    }
}
 
Example #12
Source File: FitProcessor.java    From fit with Apache License 2.0 6 votes vote down vote up
private boolean isGetter(Element method) {
  Name methodName = method.getSimpleName();
  if ((!methodName.toString().startsWith("get")) && !methodName.toString().startsWith("is")) {
    return false;
  }
  ExecutableType type = (ExecutableType) method.asType();
  //返回值为void
  if (TypeKind.VOID.equals(type.getReturnType().getKind())) {
    return false;
  }
  //有参数
  if (type.getParameterTypes().size() > 0) {
    return false;
  }

  if (methodName.length() < 4) {
    return false;
  }
  return true;
}
 
Example #13
Source File: Elements2.java    From auto with Apache License 2.0 6 votes vote down vote up
/**
 * Given an executable element in a supertype, returns its ExecutableType when it is viewed as a
 * member of a subtype.
 */
static ExecutableType getExecutableElementAsMemberOf(
    Types types, ExecutableElement executableElement, TypeElement subTypeElement) {
  checkNotNull(types);
  checkNotNull(executableElement);
  checkNotNull(subTypeElement);
  TypeMirror subTypeMirror = subTypeElement.asType();
  if (!subTypeMirror.getKind().equals(TypeKind.DECLARED)) {
    throw new IllegalStateException(
        "Expected subTypeElement.asType() to return a class/interface type.");
  }
  TypeMirror subExecutableTypeMirror = types.asMemberOf(
      (DeclaredType) subTypeMirror, executableElement);
  if (!subExecutableTypeMirror.getKind().equals(TypeKind.EXECUTABLE)) {
    throw new IllegalStateException("Expected subExecutableTypeMirror to be an executable type.");
  }
  return (ExecutableType) subExecutableTypeMirror;
}
 
Example #14
Source File: MethodModelAnalyzer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void analyze( Element element, TypeElement parent,
        WebBeansModel model, AtomicBoolean cancel, 
        Result result )
{
    ExecutableElement method = (ExecutableElement) element;
    TypeMirror methodType = model.getCompilationController().getTypes().
        asMemberOf( (DeclaredType)parent.asType(),  method );
    if ( methodType instanceof ExecutableType ){
        if ( cancel.get()){
            return;
        }
        TypeMirror returnType = ((ExecutableType) methodType).getReturnType();
        for (MethodAnalyzer analyzer : ANALYZERS) {
            if ( cancel.get() ){
                return;
            }
            analyzer.analyze(method, returnType, parent, model, 
                    cancel, result );
        }
    }
}
 
Example #15
Source File: SignatureFactory.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the JVM spec requires a signature to be emitted for this method. See JVMS8
 * 4.7.9.1
 */
private static boolean signatureRequired(ExecutableType type) {
  if (!type.getTypeVariables().isEmpty()) {
    return true;
  }

  if (usesGenerics(type.getReturnType())) {
    return true;
  }

  for (TypeMirror parameterType : type.getParameterTypes()) {
    if (usesGenerics(parameterType)) {
      return true;
    }
  }

  return throwsATypeVar(type);
}
 
Example #16
Source File: MethodSpec.java    From JReFrameworker with MIT License 6 votes vote down vote up
/**
 * Returns a new method spec builder that overrides {@code method} as a member of {@code
 * enclosing}. This will resolve type parameters: for example overriding {@link
 * Comparable#compareTo} in a type that implements {@code Comparable<Movie>}, the {@code T}
 * parameter will be resolved to {@code Movie}.
 *
 * <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
 * throws declarations. An {@link Override} annotation will be added.
 *
 * <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and
 * parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately.
 */
public static Builder overriding(
    ExecutableElement method, DeclaredType enclosing, Types types) {
  ExecutableType executableType = (ExecutableType) types.asMemberOf(enclosing, method);
  List<? extends TypeMirror> resolvedParameterTypes = executableType.getParameterTypes();
  TypeMirror resolvedReturnType = executableType.getReturnType();

  Builder builder = overriding(method);
  builder.returns(TypeName.get(resolvedReturnType));
  for (int i = 0, size = builder.parameters.size(); i < size; i++) {
    ParameterSpec parameter = builder.parameters.get(i);
    TypeName type = TypeName.get(resolvedParameterTypes.get(i));
    builder.parameters.set(i, parameter.toBuilder(type, parameter.name).build());
  }

  return builder;
}
 
Example #17
Source File: T6468404.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example #18
Source File: PropertyBuilderClassifier.java    From auto with Apache License 2.0 6 votes vote down vote up
private Optional<ExecutableElement> addAllPutAll(
    TypeElement barBuilderTypeElement,
    DeclaredType barBuilderDeclaredType,
    TypeMirror barTypeMirror) {
  return MoreElements.getLocalAndInheritedMethods(barBuilderTypeElement, typeUtils, elementUtils)
      .stream()
      .filter(
          method ->
              ADD_ALL_PUT_ALL.contains(method.getSimpleName().toString())
                  && method.getParameters().size() == 1)
      .filter(
          method -> {
            ExecutableType methodMirror =
                MoreTypes.asExecutable(typeUtils.asMemberOf(barBuilderDeclaredType, method));
            return typeUtils.isAssignable(barTypeMirror, methodMirror.getParameterTypes().get(0));
          })
      .findFirst();
}
 
Example #19
Source File: JavaReference.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ExecutableElement findExecutableMember(TypeElement clazz, String methodName, List<TypeMirror> paramTypes, Types types) {
    List<ExecutableElement> members = methodName.contentEquals(clazz.getSimpleName())
            ? ElementFilter.constructorsIn(clazz.getEnclosedElements())
            : ElementFilter.methodsIn(clazz.getEnclosedElements());
    outer: for (ExecutableElement ee : members) {
        if (ee.getKind() == ElementKind.CONSTRUCTOR || methodName.contentEquals(ee.getSimpleName())) {
            List<? extends TypeMirror> memberParamTypes = ((ExecutableType) ee.asType()).getParameterTypes();
            if (memberParamTypes.size() == paramTypes.size()) {
                Iterator<TypeMirror> it = paramTypes.iterator();
                for (TypeMirror memberParamType : memberParamTypes) {
                    TypeMirror type = it.next();
                    if (types.isSameType(type, memberParamType)) {
                        continue outer;
                    }
                }
                return ee;
            }
        }
    }
    return null;
}
 
Example #20
Source File: MoreTypes.java    From auto-parcel with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves a {@link VariableElement} parameter to a method or constructor based on the given
 * container, or a member of a class. For parameters to a method or constructor, the variable's
 * enclosing element must be a supertype of the container type. For example, given a
 * {@code container} of type {@code Set<String>}, and a variable corresponding to the {@code E e}
 * parameter in the {@code Set.add(E e)} method, this will return a TypeMirror for {@code String}.
 */
public static TypeMirror asMemberOf(Types types, DeclaredType container,
                                    VariableElement variable) {
    if (variable.getKind().equals(ElementKind.PARAMETER)) {
        ExecutableElement methodOrConstructor =
                MoreElements.asExecutable(variable.getEnclosingElement());
        ExecutableType resolvedMethodOrConstructor = MoreTypes.asExecutable(
                types.asMemberOf(container, methodOrConstructor));
        List<? extends VariableElement> parameters = methodOrConstructor.getParameters();
        List<? extends TypeMirror> parameterTypes =
                resolvedMethodOrConstructor.getParameterTypes();
        checkState(parameters.size() == parameterTypes.size());
        for (int i = 0; i < parameters.size(); i++) {
            // We need to capture the parameter type of the variable we're concerned about,
            // for later printing.  This is the only way to do it since we can't use
            // types.asMemberOf on variables of methods.
            if (parameters.get(i).equals(variable)) {
                return parameterTypes.get(i);
            }
        }
        throw new IllegalStateException("Could not find variable: " + variable);
    } else {
        return types.asMemberOf(container, variable);
    }
}
 
Example #21
Source File: ProcessorUtils.java    From Witch-Android with Apache License 2.0 6 votes vote down vote up
public static BindMethod getBindMethod(Element bindMethod) throws WitchException {

        if(!isAccessibleMethod(bindMethod)) {
            throw WitchException.bindMethodNotAccessible(bindMethod);
        }

        List<? extends TypeMirror> parameters = ((ExecutableType) bindMethod.asType()).getParameterTypes();
        if (isEmpty(parameters)) {
            return new BindMethod(bindMethod, BindMethod.Type.EMPTY);
        } else if (isView(parameters)) {
            return new BindMethod(bindMethod, BindMethod.Type.VIEW);
        } else if (isViewData(parameters)) {
            return new BindMethod(bindMethod, BindMethod.Type.VIEW_DATA);
        } else if (isViewDataHistory(parameters)) {
            return new BindMethod(bindMethod, BindMethod.Type.VIEW_DATA_HISTORY);
        } else if (isData(parameters)) {
            return new BindMethod(bindMethod, BindMethod.Type.DATA);
        } else if (isDataHistory(parameters)) {
            return new BindMethod(bindMethod, BindMethod.Type.DATA_HISTORY);
        }

        throw WitchException.invalidBindMethod(bindMethod);
    }
 
Example #22
Source File: T6468404.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example #23
Source File: IsInvalidTypeVisitor.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitExecutable(ExecutableType t, Void p) {
  return any(t.getParameterTypes(), this)
      || apply(t.getReturnType())
      || any(t.getThrownTypes(), this)
      || any(t.getTypeVariables(), this);
}
 
Example #24
Source File: JDIWrappersTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private TypeMirror adjustTypeMirror(TypeMirror tm) {
    if (tm.getKind() == TypeKind.EXECUTABLE) {
        tm = ((ExecutableType) tm).getReturnType();
        tm = adjustTypeMirror(tm);
    } else if (tm.getKind() == TypeKind.ARRAY) {
        tm = ((ArrayType) tm).getComponentType();
        tm = adjustTypeMirror(tm);
    }
    return tm;
}
 
Example #25
Source File: BasicAnnoTests.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public R visitExecutable(ExecutableType t, P p) {
    scan(t.getReceiverType());
    //out.println("  params: " + t.getParameterTypes());
    scan(t.getParameterTypes(), p);
    //out.println("  return: " + t.getReturnType());
    scan(t.getReturnType(), p);
    //out.println("  throws: " + t.getThrownTypes());
    scan(t.getThrownTypes(), p);
    return super.visitExecutable(t, p);
}
 
Example #26
Source File: Overrides.java    From auto with Apache License 2.0 5 votes vote down vote up
private boolean isSubsignature(
    ExecutableElement overrider, ExecutableElement overridden, TypeElement in) {
  DeclaredType inType = MoreTypes.asDeclared(in.asType());
  try {
    ExecutableType overriderExecutable =
        MoreTypes.asExecutable(typeUtils.asMemberOf(inType, overrider));
    ExecutableType overriddenExecutable =
        MoreTypes.asExecutable(typeUtils.asMemberOf(inType, overridden));
    return typeUtils.isSubsignature(overriderExecutable, overriddenExecutable);
  } catch (IllegalArgumentException e) {
    // This might mean that at least one of the methods is not in fact declared in or inherited
    // by `in` (in which case we should indeed return false); or it might mean that we are
    // tickling an Eclipse bug such as https://bugs.eclipse.org/bugs/show_bug.cgi?id=499026
    // (in which case we fall back on explicit code to find the parameters).
    int nParams = overrider.getParameters().size();
    if (overridden.getParameters().size() != nParams) {
      return false;
    }
    List<TypeMirror> overriderParams = erasedParameterTypes(overrider, in);
    List<TypeMirror> overriddenParams = erasedParameterTypes(overridden, in);
    if (overriderParams == null || overriddenParams == null) {
      // This probably means that one or other of the methods is not in `in`.
      return false;
    }
    for (int i = 0; i < nParams; i++) {
      if (!typeUtils.isSameType(overriderParams.get(i), overriddenParams.get(i))) {
        // If the erasures of the parameters don't correspond, return false. We erase so we
        // don't get any confusion about different type variables not comparing equal.
        return false;
      }
    }
    return true;
  }
}
 
Example #27
Source File: Descriptor.java    From auto-matter with Apache License 2.0 5 votes vote down vote up
private void enumerateFields(final Types types) {
  final List<ExecutableElement> methods = methods(valueTypeElement);
  for (final Element member : methods) {
    if (member.getKind() != ElementKind.METHOD ||
        isStaticOrDefault(member)) {
      continue;
    }
    final ExecutableElement method = (ExecutableElement) member;
    if (member.getSimpleName().toString().equals("builder")) {
      final TypeMirror returnType = (method).getReturnType();
      // TODO: javac does not seem to want to provide the name of the return type if it is not yet present and generic
      if (!isGeneric &&
          !returnType.toString().equals(concreteBuilderName) &&
          !returnType.toString().equals(fullyQualifiedBuilderName)) {
        throw new AutoMatterProcessorException(
            "builder() return type must be " + concreteBuilderName, valueTypeElement);
      }
      toBuilder = true;
      continue;
    }

    verifyResolved(method.getReturnType());

    fields.add(method);

    // Resolve inherited members
    final ExecutableType methodType = (ExecutableType) types.asMemberOf(valueType, member);
    final TypeMirror fieldType = methodType.getReturnType();


    // Resolve types
    fieldTypes.put(method, TypeName.get(fieldType));
  }
}
 
Example #28
Source File: TypeUtils.java    From Witch-Android with Apache License 2.0 5 votes vote down vote up
public TypeMirror getBoxedReturnTypeMirror(Element element) {
    if (element.getKind().isField()) {
        return boxed(element.asType());
    } else if (element.getKind() == ElementKind.METHOD) {
        ExecutableType ext = (ExecutableType) element.asType();
        return boxed(ext.getReturnType());
    }
    return null;
}
 
Example #29
Source File: MethodSignatureFormatter.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
/**
 * Formats an ExecutableElement as if it were contained within the container, if the container is
 * present.
 */
public String format(ExecutableElement method, Optional<DeclaredType> container) {
  StringBuilder builder = new StringBuilder();
  TypeElement type = MoreElements.asType(method.getEnclosingElement());
  ExecutableType executableType = MoreTypes.asExecutable(method.asType());
  if (container.isPresent()) {
    executableType = MoreTypes.asExecutable(types.asMemberOf(container.get(), method));
    type = MoreElements.asType(container.get().asElement());
  }

  // TODO(cgruber): AnnotationMirror formatter.
  List<? extends AnnotationMirror> annotations = method.getAnnotationMirrors();
  if (!annotations.isEmpty()) {
    Iterator<? extends AnnotationMirror> annotationIterator = annotations.iterator();
    for (int i = 0; annotationIterator.hasNext(); i++) {
      if (i > 0) {
        builder.append(' ');
      }
      builder.append(ErrorMessages.format(annotationIterator.next()));
    }
    builder.append(' ');
  }
  builder.append(nameOfType(executableType.getReturnType()));
  builder.append(' ');
  builder.append(type.getQualifiedName());
  builder.append('.');
  builder.append(method.getSimpleName());
  builder.append('(');
  checkState(method.getParameters().size() == executableType.getParameterTypes().size());
  Iterator<? extends VariableElement> parameters = method.getParameters().iterator();
  Iterator<? extends TypeMirror> parameterTypes = executableType.getParameterTypes().iterator();
  for (int i = 0; parameters.hasNext(); i++) {
    if (i > 0) {
      builder.append(", ");
    }
    appendParameter(builder, parameters.next(), parameterTypes.next());
  }
  builder.append(')');
  return builder.toString();
}
 
Example #30
Source File: MoreTypes.java    From auto-parcel with Apache License 2.0 5 votes vote down vote up
@Override
public Integer visitExecutable(ExecutableType t, Set<Element> visiting) {
    int result = hashKind(HASH_SEED, t);
    result *= HASH_MULTIPLIER;
    result += hashList(t.getParameterTypes(), visiting);
    result *= HASH_MULTIPLIER;
    result += t.getReturnType().accept(this, visiting);
    result *= HASH_MULTIPLIER;
    result += hashList(t.getThrownTypes(), visiting);
    result *= HASH_MULTIPLIER;
    result += hashList(t.getTypeVariables(), visiting);
    return result;
}