use of javax.lang.model.type.WildcardType in project mapstruct by mapstruct.
the class TypeFactory method getTypeBound.
/**
* Establishes the type bound:
* <ol>
* <li>{@code <? extends Number>}, returns Number</li>
* <li>{@code <? super Number>}, returns Number</li>
* <li>{@code <?>}, returns Object</li>
* <li>{@code <T extends Number>, returns Number}</li>
* </ol>
*
* @param typeMirror the type to return the bound for
* @return the bound for this parameter
*/
public TypeMirror getTypeBound(TypeMirror typeMirror) {
if (typeMirror.getKind() == TypeKind.WILDCARD) {
WildcardType wildCardType = (WildcardType) typeMirror;
if (wildCardType.getExtendsBound() != null) {
return wildCardType.getExtendsBound();
}
if (wildCardType.getSuperBound() != null) {
return wildCardType.getSuperBound();
}
String wildCardName = wildCardType.toString();
if ("?".equals(wildCardName)) {
return elementUtils.getTypeElement(Object.class.getCanonicalName()).asType();
}
} else if (typeMirror.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariableType = (TypeVariable) typeMirror;
if (typeVariableType.getUpperBound() != null) {
return typeVariableType.getUpperBound();
}
// Lowerbounds intentionally left out: Type variables otherwise have a lower bound of NullType.
}
return typeMirror;
}
use of javax.lang.model.type.WildcardType in project tiger by google.
the class Utils method isGenericNotSpecialized.
/**
* Returns true is the type is {@link java.lang.reflect.ParameterizedType} and has {@link
* javax.lang.model.type.TypeVariable}.
*/
public boolean isGenericNotSpecialized(TypeMirror type) {
// logger.n("type: %s", type);
switch(type.getKind()) {
case TYPEVAR:
return true;
case DECLARED:
for (TypeMirror i : ((DeclaredType) type).getTypeArguments()) {
if (isGenericNotSpecialized(i)) {
return true;
}
}
break;
case WILDCARD:
WildcardType wildcardType = (WildcardType) type;
TypeMirror extendsBound = wildcardType.getExtendsBound();
if (extendsBound != null && isGenericNotSpecialized(extendsBound)) {
return true;
}
TypeMirror superBound = wildcardType.getSuperBound();
if (superBound != null && isGenericNotSpecialized(superBound)) {
return true;
}
break;
default:
break;
}
return false;
}
use of javax.lang.model.type.WildcardType in project androidannotations by androidannotations.
the class RestAnnotationHelper method retrieveDecoratedResponseClass.
/**
* Recursive method used to find if one of the grand-parent of the
* <code>enclosingJClass</code> is {@link java.util.Map Map}, {@link Set} or
* {@link java.util.Collection Collection}.
*/
private AbstractJClass retrieveDecoratedResponseClass(DeclaredType declaredType, TypeElement typeElement, RestHolder holder) {
String classTypeBaseName = typeElement.toString();
// Looking for basic java.util interfaces to set a default
// implementation
String decoratedClassName = null;
if (typeElement.getKind() == ElementKind.INTERFACE) {
if (classTypeBaseName.equals(CanonicalNameConstants.MAP)) {
decoratedClassName = LinkedHashMap.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.SET)) {
decoratedClassName = TreeSet.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.LIST)) {
decoratedClassName = ArrayList.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.COLLECTION)) {
decoratedClassName = ArrayList.class.getCanonicalName();
}
} else {
decoratedClassName = typeElement.getQualifiedName().toString();
}
if (decoratedClassName != null) {
// Configure the super class of the final decorated class
String decoratedClassNameSuffix = "";
AbstractJClass decoratedSuperClass = getEnvironment().getJClass(decoratedClassName);
for (TypeMirror typeArgument : declaredType.getTypeArguments()) {
TypeMirror actualTypeArgument = typeArgument;
if (typeArgument instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) typeArgument;
if (wildcardType.getExtendsBound() != null) {
actualTypeArgument = wildcardType.getExtendsBound();
} else if (wildcardType.getSuperBound() != null) {
actualTypeArgument = wildcardType.getSuperBound();
}
}
AbstractJClass narrowJClass = codeModelHelper.typeMirrorToJClass(actualTypeArgument);
decoratedSuperClass = decoratedSuperClass.narrow(narrowJClass);
decoratedClassNameSuffix += plainName(narrowJClass);
}
String decoratedFinalClassName = classTypeBaseName + "_" + decoratedClassNameSuffix;
decoratedFinalClassName = decoratedFinalClassName.replaceAll("\\[\\]", "s");
String packageName = holder.getGeneratedClass()._package().name();
decoratedFinalClassName = packageName + "." + decoratedFinalClassName;
JDefinedClass decoratedJClass = getEnvironment().getDefinedClass(decoratedFinalClassName);
decoratedJClass._extends(decoratedSuperClass);
return decoratedJClass;
}
// Try to find the superclass and make a recursive call to the this
// method
TypeMirror enclosingSuperJClass = typeElement.getSuperclass();
if (enclosingSuperJClass != null && enclosingSuperJClass.getKind() == TypeKind.DECLARED) {
DeclaredType declaredEnclosingSuperJClass = (DeclaredType) enclosingSuperJClass;
return retrieveDecoratedResponseClass(declaredType, (TypeElement) declaredEnclosingSuperJClass.asElement(), holder);
}
// Falling back to the current enclosingJClass if Class can't be found
return null;
}
use of javax.lang.model.type.WildcardType in project javapoet by square.
the class AbstractTypesTest method wildcardMirrorNoBounds.
@Test
public void wildcardMirrorNoBounds() throws Exception {
WildcardType wildcard = getTypes().getWildcardType(null, null);
TypeName type = TypeName.get(wildcard);
assertThat(type.toString()).isEqualTo("?");
}
use of javax.lang.model.type.WildcardType in project immutables by immutables.
the class TypeStringProvider method caseType.
void caseType(TypeMirror type) {
if (ended) {
// to prevent additional recursive effects when using workaround
return;
}
switch(type.getKind()) {
case ERROR:
unresolvedTypeHasOccured = true;
// $FALL-THROUGH$
case DECLARED:
DeclaredType declaredType = (DeclaredType) type;
appendResolved(declaredType);
appendTypeArguments(type, declaredType);
break;
case ARRAY:
TypeMirror componentType = ((ArrayType) type).getComponentType();
int mark = buffer.length();
caseType(componentType);
cutTypeArgument(type, mark);
buffer.append('[').append(']');
break;
case WILDCARD:
WildcardType wildcard = (WildcardType) type;
@Nullable TypeMirror extendsBound = wildcard.getExtendsBound();
@Nullable TypeMirror superBound = wildcard.getSuperBound();
if (extendsBound != null) {
buffer.append("? extends ");
caseType(extendsBound);
} else if (superBound != null) {
buffer.append("? super ");
caseType(superBound);
} else {
buffer.append('?');
}
break;
case TYPEVAR:
if (allowedTypevars.length != 0) {
TypeVariable typeVariable = (TypeVariable) type;
String var = typeVariable.toString();
int indexOfVar = Arrays.asList(allowedTypevars).indexOf(var);
if (indexOfVar >= 0) {
if (typevarArguments != null) {
buffer.append(typevarArguments[indexOfVar]);
} else {
hasTypeVariables = true;
buffer.append(var);
}
break;
}
// If we don't have such parameter we consider this is the quirk
// that was witnessed in Eclipse, we let the code below deal with it.
}
// ended flag
if (tryToUseSourceAsAWorkaround()) {
ended = true;
break;
}
reporter.withElement(element).error("It is a compiler/annotation processing bug to receive type variable '%s' here." + " To avoid it — do not use not yet generated types in %s attribute", type, element.getSimpleName());
// just append as toString whatever we have
buffer.append(type);
break;
case BOOLEAN:
case CHAR:
case INT:
case DOUBLE:
case FLOAT:
case SHORT:
case LONG:
case BYTE:
String typeName = Ascii.toLowerCase(type.getKind().name());
buffer.append(typeName);
/* Just skip type annotations with primitives (for now?) too many problems/breakages
List<? extends AnnotationMirror> annotations = null;
if (processNestedTypeUseAnnotations
&& startType != type
&& !(annotations = AnnotationMirrors.from(type)).isEmpty()) {
buffer.append(typeAnnotationsToBuffer(annotations, true)).append(typeName);
} else {
buffer.append(typeName);
}*/
break;
default:
buffer.append(type);
}
// workaround for Javac problem
if (unresolvedTypeHasOccured && buffer.toString().contains("<any>")) {
if (tryToUseSourceAsAWorkaround()) {
ended = true;
}
}
}
Aggregations