use of javax.lang.model.type.PrimitiveType in project auto by google.
the class MoreTypesTest method equivalence.
@Test
public void equivalence() {
Types types = compilationRule.getTypes();
Elements elements = compilationRule.getElements();
TypeMirror objectType = elements.getTypeElement(Object.class.getCanonicalName()).asType();
TypeMirror stringType = elements.getTypeElement(String.class.getCanonicalName()).asType();
TypeElement mapElement = elements.getTypeElement(Map.class.getCanonicalName());
TypeElement setElement = elements.getTypeElement(Set.class.getCanonicalName());
TypeElement enumElement = elements.getTypeElement(Enum.class.getCanonicalName());
TypeElement container = elements.getTypeElement(Container.class.getCanonicalName());
TypeElement contained = elements.getTypeElement(Container.Contained.class.getCanonicalName());
TypeElement funkyBounds = elements.getTypeElement(FunkyBounds.class.getCanonicalName());
TypeElement funkyBounds2 = elements.getTypeElement(FunkyBounds2.class.getCanonicalName());
TypeElement funkierBounds = elements.getTypeElement(FunkierBounds.class.getCanonicalName());
TypeMirror funkyBoundsVar = ((DeclaredType) funkyBounds.asType()).getTypeArguments().get(0);
TypeMirror funkyBounds2Var = ((DeclaredType) funkyBounds2.asType()).getTypeArguments().get(0);
TypeMirror funkierBoundsVar = ((DeclaredType) funkierBounds.asType()).getTypeArguments().get(0);
DeclaredType mapOfObjectToObjectType = types.getDeclaredType(mapElement, objectType, objectType);
TypeMirror mapType = mapElement.asType();
DeclaredType setOfSetOfObject = types.getDeclaredType(setElement, types.getDeclaredType(setElement, objectType));
DeclaredType setOfSetOfString = types.getDeclaredType(setElement, types.getDeclaredType(setElement, stringType));
DeclaredType setOfSetOfSetOfObject = types.getDeclaredType(setElement, setOfSetOfObject);
DeclaredType setOfSetOfSetOfString = types.getDeclaredType(setElement, setOfSetOfString);
WildcardType wildcard = types.getWildcardType(null, null);
DeclaredType containerOfObject = types.getDeclaredType(container, objectType);
DeclaredType containerOfString = types.getDeclaredType(container, stringType);
TypeMirror containedInObject = types.asMemberOf(containerOfObject, contained);
TypeMirror containedInString = types.asMemberOf(containerOfString, contained);
EquivalenceTester<TypeMirror> tester = EquivalenceTester.<TypeMirror>of(MoreTypes.equivalence()).addEquivalenceGroup(types.getNullType()).addEquivalenceGroup(types.getNoType(NONE)).addEquivalenceGroup(types.getNoType(VOID)).addEquivalenceGroup(objectType).addEquivalenceGroup(stringType).addEquivalenceGroup(containedInObject).addEquivalenceGroup(containedInString).addEquivalenceGroup(funkyBounds.asType()).addEquivalenceGroup(funkyBounds2.asType()).addEquivalenceGroup(funkierBounds.asType()).addEquivalenceGroup(funkyBoundsVar, funkyBounds2Var).addEquivalenceGroup(funkierBoundsVar).addEquivalenceGroup(enumElement.asType()).addEquivalenceGroup(mapType).addEquivalenceGroup(mapOfObjectToObjectType).addEquivalenceGroup(types.getDeclaredType(mapElement, wildcard, wildcard)).addEquivalenceGroup(types.erasure(mapType), types.erasure(mapOfObjectToObjectType)).addEquivalenceGroup(types.getDeclaredType(mapElement, objectType, stringType)).addEquivalenceGroup(types.getDeclaredType(mapElement, stringType, objectType)).addEquivalenceGroup(types.getDeclaredType(mapElement, stringType, stringType)).addEquivalenceGroup(setOfSetOfObject).addEquivalenceGroup(setOfSetOfString).addEquivalenceGroup(setOfSetOfSetOfObject).addEquivalenceGroup(setOfSetOfSetOfString).addEquivalenceGroup(wildcard).addEquivalenceGroup(types.getWildcardType(objectType, null)).addEquivalenceGroup(types.getWildcardType(stringType, null)).addEquivalenceGroup(types.getWildcardType(null, stringType)).addEquivalenceGroup(types.getDeclaredType(mapElement, stringType, types.getDeclaredType(mapElement, stringType, types.getDeclaredType(setElement, objectType)))).addEquivalenceGroup(FAKE_ERROR_TYPE);
for (TypeKind kind : TypeKind.values()) {
if (kind.isPrimitive()) {
PrimitiveType primitiveType = types.getPrimitiveType(kind);
TypeMirror boxedPrimitiveType = types.boxedClass(primitiveType).asType();
tester.addEquivalenceGroup(primitiveType, types.unboxedType(boxedPrimitiveType));
tester.addEquivalenceGroup(boxedPrimitiveType);
tester.addEquivalenceGroup(types.getArrayType(primitiveType));
tester.addEquivalenceGroup(types.getArrayType(boxedPrimitiveType));
}
}
ImmutableSet<Class<?>> testClasses = ImmutableSet.of(ExecutableElementsGroupA.class, ExecutableElementsGroupB.class, ExecutableElementsGroupC.class, ExecutableElementsGroupD.class, ExecutableElementsGroupE.class);
for (Class<?> testClass : testClasses) {
ImmutableList<TypeMirror> equivalenceGroup = FluentIterable.from(elements.getTypeElement(testClass.getCanonicalName()).getEnclosedElements()).transform(Element::asType).toList();
tester.addEquivalenceGroup(equivalenceGroup);
}
tester.test();
}
use of javax.lang.model.type.PrimitiveType in project lwjgl by LWJGL.
the class FieldsGenerator method validateField.
private static void validateField(VariableElement field) {
// Check if field is "public static final"
Set<Modifier> modifiers = field.getModifiers();
if (modifiers.size() != 3 || !modifiers.contains(Modifier.PUBLIC) || !modifiers.contains(Modifier.STATIC) || !modifiers.contains(Modifier.FINAL)) {
throw new RuntimeException("Field " + field.getSimpleName() + " is not declared public static final");
}
// Check suported types (int, long, float, String)
TypeMirror field_type = field.asType();
if ("java.lang.String".equals(field_type.toString())) {
} else if (field_type instanceof PrimitiveType) {
PrimitiveType field_type_prim = (PrimitiveType) field_type;
TypeKind field_kind = field_type_prim.getKind();
if (field_kind != TypeKind.INT && field_kind != TypeKind.LONG && field_kind != TypeKind.FLOAT && field_kind != TypeKind.BYTE) {
throw new RuntimeException("Field " + field.getSimpleName() + " is not of type 'int', 'long', 'float' or 'byte' " + field_kind.toString());
}
} else {
throw new RuntimeException("Field " + field.getSimpleName() + " is not a primitive type or String");
}
Object field_value = field.getConstantValue();
if (field_value == null) {
throw new RuntimeException("Field " + field.getSimpleName() + " has no initial value");
}
}
use of javax.lang.model.type.PrimitiveType in project j2objc by google.
the class Autoboxer method endVisit.
@Override
public void endVisit(CastExpression node) {
TypeMirror castType = node.getTypeMirror();
Expression expr = node.getExpression();
TypeMirror exprType = expr.getTypeMirror();
if (castType.getKind().isPrimitive() && !exprType.getKind().isPrimitive()) {
if (typeUtil.isAssignable(exprType, typeUtil.getJavaNumber().asType())) {
// Casting a Number object to a primitive, convert to value method.
unbox(expr, (PrimitiveType) castType);
} else if (exprType == typeUtil.boxedClass(typeUtil.getChar()).asType()) {
// Unboxing and casting Character, which does not have number value functions.
unbox(expr);
if (castType.getKind() != TypeKind.CHAR) {
// If the resulting type is not char - keep the cast, to preserve type information in
// case of reboxing.
CastExpression castExpr = new CastExpression(castType, null);
Expression unboxedExpression = node.getExpression();
unboxedExpression.replaceWith(castExpr);
castExpr.setExpression(unboxedExpression);
}
} else {
// Casting an object to a primitive. Convert the cast type to the wrapper
// so that we do a proper cast check, as Java would.
castType = typeUtil.boxedClass((PrimitiveType) castType).asType();
node.setType(Type.newType(castType));
boxOrUnboxExpression(expr, castType);
}
} else {
boxOrUnboxExpression(expr, castType);
}
Expression newExpr = node.getExpression();
if (newExpr != expr) {
TreeNode parent = node.getParent();
if (parent instanceof ParenthesizedExpression) {
parent.replaceWith(TreeUtil.remove(newExpr));
} else {
node.replaceWith(TreeUtil.remove(newExpr));
}
}
}
use of javax.lang.model.type.PrimitiveType in project vertx-docgen by vert-x3.
the class Helper method toString.
/**
* Compute the string representation of a type mirror.
*
* @param mirror the type mirror
* @param buffer the buffer appended with the string representation
*/
static void toString(TypeMirror mirror, StringBuilder buffer) {
if (mirror instanceof DeclaredType) {
DeclaredType dt = (DeclaredType) mirror;
TypeElement elt = (TypeElement) dt.asElement();
buffer.append(elt.getQualifiedName().toString());
List<? extends TypeMirror> args = dt.getTypeArguments();
if (args.size() > 0) {
buffer.append("<");
for (int i = 0; i < args.size(); i++) {
if (i > 0) {
buffer.append(",");
}
toString(args.get(i), buffer);
}
buffer.append(">");
}
} else if (mirror instanceof PrimitiveType) {
PrimitiveType pm = (PrimitiveType) mirror;
buffer.append(pm.getKind().name().toLowerCase());
} else if (mirror instanceof javax.lang.model.type.WildcardType) {
javax.lang.model.type.WildcardType wt = (javax.lang.model.type.WildcardType) mirror;
buffer.append("?");
if (wt.getSuperBound() != null) {
buffer.append(" super ");
toString(wt.getSuperBound(), buffer);
} else if (wt.getExtendsBound() != null) {
buffer.append(" extends ");
toString(wt.getExtendsBound(), buffer);
}
} else if (mirror instanceof javax.lang.model.type.TypeVariable) {
javax.lang.model.type.TypeVariable tv = (TypeVariable) mirror;
TypeParameterElement elt = (TypeParameterElement) tv.asElement();
buffer.append(elt.getSimpleName().toString());
if (tv.getUpperBound() != null && !tv.getUpperBound().toString().equals("java.lang.Object")) {
buffer.append(" extends ");
toString(tv.getUpperBound(), buffer);
} else if (tv.getLowerBound() != null && tv.getLowerBound().getKind() != TypeKind.NULL) {
buffer.append(" super ");
toString(tv.getUpperBound(), buffer);
}
} else if (mirror instanceof javax.lang.model.type.ArrayType) {
javax.lang.model.type.ArrayType at = (ArrayType) mirror;
toString(at.getComponentType(), buffer);
buffer.append("[]");
} else {
throw new UnsupportedOperationException("todo " + mirror + " " + mirror.getKind());
}
}
use of javax.lang.model.type.PrimitiveType in project roboguice by roboguice.
the class GuiceAnnotationProcessor method getTypeName.
private String getTypeName(Element injectionPoint) {
String injectedClassName = null;
final TypeMirror fieldTypeMirror = injectionPoint.asType();
if (fieldTypeMirror instanceof DeclaredType) {
injectedClassName = getTypeName((TypeElement) ((DeclaredType) fieldTypeMirror).asElement());
} else if (fieldTypeMirror instanceof PrimitiveType) {
injectedClassName = fieldTypeMirror.getKind().name();
}
return injectedClassName;
}
Aggregations