Java Code Examples for javax.lang.model.type.DeclaredType#getTypeArguments()

The following examples show how to use javax.lang.model.type.DeclaredType#getTypeArguments() . 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: ParameterInjectionPointLogic.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static TypeMirror getParameterType( TypeMirror elementType, 
        String... interfaceFqns )
{
    if ( elementType instanceof DeclaredType ){
        DeclaredType declaredType = (DeclaredType)elementType;
        Element elementDeclaredType = declaredType.asElement();
        if ( elementDeclaredType!= null && 
                elementDeclaredType.getKind() == ElementKind.INTERFACE )
        {
            String typeFqn = ((TypeElement)elementDeclaredType).
                getQualifiedName().toString();
            for (String interfaceFqn : interfaceFqns) {
                if (interfaceFqn.equals(typeFqn)) {
                    List<? extends TypeMirror> typeArguments = declaredType
                            .getTypeArguments();
                    if (typeArguments.size() > 0) {
                        return typeArguments.get(0);
                    }
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: CtTypes.java    From doma with Apache License 2.0 6 votes vote down vote up
private TypeMirror[] getConverterArgTypes(TypeMirror typeMirror) {
  for (TypeMirror supertype : ctx.getMoreTypes().directSupertypes(typeMirror)) {
    if (!ctx.getMoreTypes().isAssignableWithErasure(supertype, DomainConverter.class)) {
      continue;
    }
    if (ctx.getMoreTypes().isSameTypeWithErasure(supertype, DomainConverter.class)) {
      DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(supertype);
      assertNotNull(declaredType);
      List<? extends TypeMirror> args = declaredType.getTypeArguments();
      assertEquals(2, args.size());
      return new TypeMirror[] {args.get(0), args.get(1)};
    }
    TypeMirror[] argTypes = getConverterArgTypes(supertype);
    if (argTypes != null) {
      return argTypes;
    }
  }
  return null;
}
 
Example 3
Source File: GeneratedPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static boolean hasUncheckedWarning(TypeMirror type) {
    switch (type.getKind()) {
        case DECLARED:
            DeclaredType declared = (DeclaredType) type;
            for (TypeMirror typeParam : declared.getTypeArguments()) {
                if (hasUncheckedWarning(typeParam)) {
                    return true;
                }
            }
            return false;
        case TYPEVAR:
            return true;
        case WILDCARD:
            return ((WildcardType) type).getExtendsBound() != null;
        case ARRAY:
            return hasUncheckedWarning(((ArrayType) type).getComponentType());
        default:
            return false;
    }
}
 
Example 4
Source File: CtTypes.java    From doma with Apache License 2.0 6 votes vote down vote up
private MapCtType newMapCtType(TypeMirror type) {
  if (!ctx.getMoreTypes().isSameTypeWithErasure(type, Map.class)) {
    return null;
  }
  DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(type);
  if (declaredType == null) {
    return null;
  }
  List<? extends TypeMirror> typeArgs = declaredType.getTypeArguments();
  if (typeArgs.size() != 2) {
    return null;
  }
  if (!ctx.getMoreTypes().isSameTypeWithErasure(typeArgs.get(0), String.class)) {
    return null;
  }
  if (!ctx.getMoreTypes().isSameTypeWithErasure(typeArgs.get(1), Object.class)) {
    return null;
  }
  return new MapCtType(ctx, type);
}
 
Example 5
Source File: MakeSafeTypeVisitor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror visitDeclared(DeclaredType t, Types types) {
    if (TypeModeler.isSubElement((TypeElement) t.asElement(), collectionType)
            || TypeModeler.isSubElement((TypeElement) t.asElement(), mapType)) {
        Collection<? extends TypeMirror> args = t.getTypeArguments();
        TypeMirror[] safeArgs = new TypeMirror[args.size()];
        int i = 0;
        for (TypeMirror arg : args) {
            safeArgs[i++] = visit(arg, types);
        }
        return types.getDeclaredType((TypeElement) t.asElement(), safeArgs);
    }
    return types.erasure(t);
}
 
Example 6
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean containsTypeVar(DeclaredType dt, boolean typeVar) {
    List<? extends TypeMirror> args = dt.getTypeArguments();
    for (TypeMirror arg : args) {
        if (arg.getKind() == TypeKind.TYPEVAR) {
            typeVar = true;
        }
        if (arg.getKind() == TypeKind.DECLARED) {
            typeVar = typeVar || containsTypeVar((DeclaredType) arg, typeVar);
        }
    }
    return typeVar;
}
 
Example 7
Source File: HashCodeValidator.java    From epoxy with Apache License 2.0 5 votes vote down vote up
private void validateIterableType(DeclaredType declaredType) throws EpoxyProcessorException {
  for (TypeMirror typeParameter : declaredType.getTypeArguments()) {
    // check that the type implements hashCode
    try {
      validateImplementsHashCode(typeParameter);
    } catch (EpoxyProcessorException e) {
      throwError("Type in Iterable does not implement hashCode. Type: %s",
          typeParameter.toString());
    }
  }

  // Assume that the iterable class implements hashCode and just return
}
 
Example 8
Source File: SerializationProcessorUtil.java    From bazel with Apache License 2.0 5 votes vote down vote up
static TypeMirror sanitizeTypeParameter(TypeMirror type, ProcessingEnvironment env) {
  if (isVariableOrWildcardType(type)) {
    return env.getTypeUtils().erasure(type);
  }
  if (!(type instanceof DeclaredType)) {
    return type;
  }
  DeclaredType declaredType = (DeclaredType) type;
  for (TypeMirror typeMirror : declaredType.getTypeArguments()) {
    if (isVariableOrWildcardType(typeMirror)) {
      return env.getTypeUtils().erasure(type);
    }
  }
  return type;
}
 
Example 9
Source File: TypeKey.java    From paperparcel with Apache License 2.0 5 votes vote down vote up
@Override public TypeKey visitDeclared(DeclaredType t, Set<TypeParameterElement> visited) {
  ClassKey rawType =
      ClassKey.get(((TypeElement) t.asElement()).getQualifiedName().toString());
  if (t.getTypeArguments().isEmpty()) {
    return rawType;
  }
  ImmutableList.Builder<TypeKey> typeArguments = ImmutableList.builder();
  for (TypeMirror mirror : t.getTypeArguments()) {
    typeArguments.add(mirror.accept(this, visited));
  }
  return ParameterizedKey.get(rawType, typeArguments.build());
}
 
Example 10
Source File: Overrides.java    From auto with Apache License 2.0 5 votes vote down vote up
@Override
public TypeMirror visitDeclared(DeclaredType t, Void p) {
  if (t.getTypeArguments().isEmpty()) {
    return t;
  }
  List<TypeMirror> newArgs = Lists.newArrayList();
  for (TypeMirror arg : t.getTypeArguments()) {
    newArgs.add(visit(arg));
  }
  return typeUtils.getDeclaredType(asTypeElement(t), newArgs.toArray(new TypeMirror[0]));
}
 
Example 11
Source File: SupportedTypes.java    From ParcelablePlease with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the variabel element has generics arguments that matches the expected type
 */
public static TypeMirror hasGenericsTypeArgumentOf(Element element, String typeToCheck,
                                                   Elements elements, Types types) {

  if (element.asType().getKind() != TypeKind.DECLARED
      || !(element.asType() instanceof DeclaredType)) {
    ProcessorMessage.error(element, "The field %s in %s doesn't have generic type arguments!",
        element.getSimpleName(), element.asType().toString());
  }

  DeclaredType declaredType = (DeclaredType) element.asType();
  List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();

  if (typeArguments.isEmpty()) {
    ProcessorMessage.error(element, "The field %s in %s doesn't have generic type arguments!",
        element.getSimpleName(), element.asType().toString());
  }

  if (typeArguments.size() > 1) {
    ProcessorMessage.error(element, "The field %s in %s has more than 1 generic type argument!",
        element.getSimpleName(), element.asType().toString());
  }

  // Ok it has a generic argument, check if this extends Parcelable
  TypeMirror argument = typeArguments.get(0);

  if (typeToCheck != null) {
    if (!isOfType(argument, typeToCheck, elements, types)) {
      ProcessorMessage.error(element,
          "The fields %s  generic type argument is not of type  %s! (in %s )",
          element.getSimpleName(), typeToCheck, element.asType().toString());
    }
  }

  // everything is like expected
  return argument;
}
 
Example 12
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
void appendTypeArguments(DeclaredType type, StringBuilder sb) {
  List<? extends TypeMirror> arguments = type.getTypeArguments();
  if (!arguments.isEmpty()) {
    sb.append("<");
    String sep = "";
    for (TypeMirror argument : arguments) {
      sb.append(sep);
      sep = ", ";
      visit(argument, sb);
    }
    sb.append(">");
  }
}
 
Example 13
Source File: GremlinDslProcessor.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private List<? extends TypeMirror> getTypeArguments(final ExecutableElement templateMethod) {
    final DeclaredType returnTypeMirror = (DeclaredType) templateMethod.getReturnType();
    return returnTypeMirror.getTypeArguments();
}
 
Example 14
Source File: BeanModelBuilder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void addEventSource(ExecutableElement m) {
    if (consumed) {
        return;
        
    }
    String sn = m.getSimpleName().toString();
    
    if (!isAccessible(m, false)) {
        return;
    }
    
    if (!sn.startsWith(EVENT_PREFIX) || sn.length() == EVENT_PREFIX_LEN) {
        return;
    }
    
    if (m.getParameters().size() != 1) {
        return;
    }
    VariableElement param = m.getParameters().get(0);
    TypeMirror varType = param.asType();
    
    // the type must be assignable to the event handler
    if (compilationInfo.getTypes().isAssignable(varType, getHandlerBaseType())) {
        return;
    }
    ElementHandle<TypeElement> eventHandle = null;
    String eventClassName = null;
    
    if (varType.getKind() == TypeKind.DECLARED) {
        // extract event type as the type of event / argument for the event handler method
        DeclaredType dt = (DeclaredType)varType;
        List<? extends TypeMirror> tParams = dt.getTypeArguments();
        if (tParams.size() != 1) {
            // something very wrong, the event handler has just 1 type parameter
            //throw new IllegalStateException();
            return;
        }
        TypeMirror eventType = tParams.get(0);
        if (eventType.getKind() == TypeKind.WILDCARD) {
            TypeMirror t = ((WildcardType)eventType).getSuperBound();
            if (t == null) {
                t = ((WildcardType)eventType).getExtendsBound();
            }
            eventType = t;
        }
        if (eventType.getKind() != TypeKind.DECLARED) {
            throw new IllegalStateException();
        }
        TypeElement te = (TypeElement)compilationInfo.getTypes().asElement(eventType);
        eventClassName = te.getQualifiedName().toString();
        eventHandle = ElementHandle.create(te);
        addDependency(eventType);
    }

    String eventName = Character.toLowerCase(sn.charAt(EVENT_PREFIX_LEN)) + sn.substring(EVENT_PREFIX_LEN + 1);
    FxEvent ei = new FxEvent(eventName);
    ei.setEventClassName(eventClassName);
    ei.setEventType(eventHandle);
    
    addEvent(ei);
    
    consumed = true;
}
 
Example 15
Source File: NoPrivateTypesExported.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void verifyTypeAcceptable(TypeMirror type, Set<String> acceptable) {
    if (type == null) return ;

    verifyAnnotations(type.getAnnotationMirrors(), acceptable);

    switch (type.getKind()) {
        case BOOLEAN: case BYTE: case CHAR: case DOUBLE: case FLOAT:
        case INT: case LONG: case SHORT: case VOID: case NONE: case NULL:
            return ;
        case DECLARED:
            DeclaredType dt = (DeclaredType) type;
            TypeElement outermostTypeElement = outermostTypeElement(dt.asElement());
            String outermostType = outermostTypeElement.getQualifiedName().toString();
            boolean isAcceptable = false;
            for (String acceptablePackage : acceptable) {
                if (outermostType.startsWith(acceptablePackage)) {
                    isAcceptable = true;
                    break;
                }
            }
            if (!isAcceptable) {
                error("Type not acceptable for this API: " + dt.toString());
            }

            for (TypeMirror bound : dt.getTypeArguments()) {
                verifyTypeAcceptable(bound, acceptable);
            }
            break;
        case ARRAY:
            verifyTypeAcceptable(((ArrayType) type).getComponentType(), acceptable);
            break;
        case INTERSECTION:
            for (TypeMirror element : ((IntersectionType) type).getBounds()) {
                verifyTypeAcceptable(element, acceptable);
            }
            break;
        case TYPEVAR:
            verifyTypeAcceptable(((TypeVariable) type).getLowerBound(), acceptable);
            verifyTypeAcceptable(((TypeVariable) type).getUpperBound(), acceptable);
            break;
        case WILDCARD:
            verifyTypeAcceptable(((WildcardType) type).getExtendsBound(), acceptable);
            verifyTypeAcceptable(((WildcardType) type).getSuperBound(), acceptable);
            break;
        default:
            error("Type not acceptable for this API: " + type.toString());
            break;

    }
}
 
Example 16
Source File: Util.java    From dagger2-sample with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the key type for a {@link Map} type like Map<K, Provider<V>>}
 */
public static DeclaredType getKeyTypeOfMap(DeclaredType mapType) {
  checkState(MoreTypes.isTypeOf(Map.class, mapType), "%s is not a Map.", mapType);
  List<? extends TypeMirror> mapArgs = mapType.getTypeArguments();
  return MoreTypes.asDeclared(mapArgs.get(0));
}
 
Example 17
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
@Override public Void visitDeclared(DeclaredType type, Set<String> visited) {
  for (TypeMirror arg : type.getTypeArguments()) {
    arg.accept(this, visited);
  }
  return null;
}
 
Example 18
Source File: TypeMonikerFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public DeclaredTypeMoniker(DeclaredType type) {
    typeDeclName = ((TypeElement) type.asElement()).getQualifiedName();
    for (TypeMirror arg : type.getTypeArguments())
        typeArgs.add(TypeMonikerFactory.getTypeMoniker(arg));
}
 
Example 19
Source File: TypeUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Note: may return {@code null}, if an intersection type is encountered, to indicate a 
 * real type cannot be created.
 */
private static TypeMirror resolveCapturedTypeInt(CompilationInfo info, TypeMirror tm) {
    if (tm == null) return tm;
    
    TypeMirror orig = SourceUtils.resolveCapturedType(tm);

    if (orig != null) {
        tm = orig;
    }
    
    if (tm.getKind() == TypeKind.WILDCARD) {
        TypeMirror extendsBound = ((WildcardType) tm).getExtendsBound();
        TypeMirror superBound = ((WildcardType) tm).getSuperBound();
        if (extendsBound != null || superBound != null) {
            TypeMirror rct = resolveCapturedTypeInt(info, extendsBound != null ? extendsBound : superBound);
            if (rct != null) {
                switch (rct.getKind()) {
                    case WILDCARD:
                        return rct;
                    case ARRAY:
                    case DECLARED:
                    case ERROR:
                    case TYPEVAR:
                    case OTHER:
                        return info.getTypes().getWildcardType(
                                extendsBound != null ? rct : null, superBound != null ? rct : null);
                }
            } else {
                // propagate failure out of all wildcards
                return null;
            }
        }
    } else if (tm.getKind() == TypeKind.INTERSECTION) {
        return null;
    }
    
    if (tm.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) tm;
        List<TypeMirror> typeArguments = new LinkedList<TypeMirror>();
        
        for (TypeMirror t : dt.getTypeArguments()) {
            TypeMirror targ = resolveCapturedTypeInt(info, t);
            if (targ == null) {
                // bail out, if the type parameter is a wildcard, it's probably not possible
                // to create a proper parametrized type from it
                if (t.getKind() == TypeKind.WILDCARD || t.getKind() == TypeKind.INTERSECTION) {
                    return null;
                }
                // use rawtype
                typeArguments.clear();
                break;
            }
            typeArguments.add(targ);
        }
        
        final TypeMirror enclosingType = dt.getEnclosingType();
        if (enclosingType.getKind() == TypeKind.DECLARED) {
            return info.getTypes().getDeclaredType((DeclaredType) enclosingType, (TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        } else {
            if (dt.asElement() == null) return dt;
            return info.getTypes().getDeclaredType((TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        }
    }

    if (tm.getKind() == TypeKind.ARRAY) {
        ArrayType at = (ArrayType) tm;
        TypeMirror tm2 = resolveCapturedTypeInt(info, at.getComponentType());
        return info.getTypes().getArrayType(tm2 != null ? tm2 : tm);
    }
    
    return tm;
}
 
Example 20
Source File: TypeMonikerFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public DeclaredTypeMoniker(DeclaredType type) {
    typeDeclName = ((TypeElement) type.asElement()).getQualifiedName();
    for (TypeMirror arg : type.getTypeArguments())
        typeArgs.add(TypeMonikerFactory.getTypeMoniker(arg));
}