use of scenelib.annotations.field.AnnotationFieldType in project checker-framework by typetools.
the class AnnotationConverter method getAnnotationFieldType.
/**
* Returns an AnnotationFieldType given an ExecutableElement or value.
*/
protected static AnnotationFieldType getAnnotationFieldType(ExecutableElement ee, Object value) {
if (value instanceof List<?>) {
AnnotationValue defaultValue = ee.getDefaultValue();
if (defaultValue == null || ((ArrayType) ((Array) defaultValue).type) == null) {
List<?> listV = (List<?>) value;
if (!listV.isEmpty()) {
ScalarAFT scalarAFT = (ScalarAFT) getAnnotationFieldType(ee, ((AnnotationValue) listV.get(0)).getValue());
if (scalarAFT != null) {
return new ArrayAFT(scalarAFT);
}
}
return null;
}
Type elemType = ((ArrayType) ((Array) defaultValue).type).elemtype;
try {
return new ArrayAFT(BasicAFT.forType(Class.forName(elemType.toString())));
} catch (ClassNotFoundException e) {
ErrorReporter.errorAbort(e.getMessage());
}
} else if (value instanceof Boolean) {
return BasicAFT.forType(boolean.class);
} else if (value instanceof Character) {
return BasicAFT.forType(char.class);
} else if (value instanceof Double) {
return BasicAFT.forType(double.class);
} else if (value instanceof Float) {
return BasicAFT.forType(float.class);
} else if (value instanceof Integer) {
return BasicAFT.forType(int.class);
} else if (value instanceof Long) {
return BasicAFT.forType(long.class);
} else if (value instanceof Short) {
return BasicAFT.forType(short.class);
} else if (value instanceof String) {
return BasicAFT.forType(String.class);
}
return null;
}
use of scenelib.annotations.field.AnnotationFieldType in project j2objc by google.
the class ExternalAnnotationInjector method generateAnnotationMirror.
private GeneratedAnnotationMirror generateAnnotationMirror(Annotation annotation) {
TypeElement element = typeUtil.resolveJavaType(annotation.def.name);
if (element == null) {
reportNoSuchClass(annotation);
return null;
}
DeclaredType type = (DeclaredType) element.asType();
GeneratedAnnotationMirror annotationMirror = new GeneratedAnnotationMirror(type);
for (Map.Entry<String, Object> entry : annotation.fieldValues.entrySet()) {
String fieldName = entry.getKey();
// For our uses cases, the scenelib library encodes the annotation value as a string.
String fieldValue = (String) entry.getValue();
AnnotationFieldType fieldType = annotation.def.fieldTypes.get(fieldName);
AnnotationField field = generateAnnotationField(annotation, fieldType, fieldName, fieldValue);
annotationMirror.addElementValue(field.element, field.value);
}
return annotationMirror;
}
use of scenelib.annotations.field.AnnotationFieldType in project checker-framework by typetools.
the class AnnotationConverter method typeMirrorToAnnotationFieldType.
/**
* Converts a TypeMirror to an AnnotationFieldType.
*
* @param tm a type for an annotation element/field: primitive, String, class, enum constant, or
* array thereof
* @return an AnnotationFieldType corresponding to the argument
*/
protected static AnnotationFieldType typeMirrorToAnnotationFieldType(TypeMirror tm) {
switch(tm.getKind()) {
case BOOLEAN:
return BasicAFT.forType(boolean.class);
// Primitves
case BYTE:
return BasicAFT.forType(byte.class);
case CHAR:
return BasicAFT.forType(char.class);
case DOUBLE:
return BasicAFT.forType(double.class);
case FLOAT:
return BasicAFT.forType(float.class);
case INT:
return BasicAFT.forType(int.class);
case LONG:
return BasicAFT.forType(long.class);
case SHORT:
return BasicAFT.forType(short.class);
case ARRAY:
TypeMirror componentType = ((ArrayType) tm).getComponentType();
AnnotationFieldType componentAFT = typeMirrorToAnnotationFieldType(componentType);
return new ArrayAFT((ScalarAFT) componentAFT);
case DECLARED:
String className = TypesUtils.getQualifiedName((DeclaredType) tm);
if (className.equals("java.lang.String")) {
return BasicAFT.forType(String.class);
} else if (className.equals("java.lang.Class")) {
return ClassTokenAFT.ctaft;
} else {
// This must be an enum constant.
return new EnumAFT(className);
}
default:
throw new BugInCF("typeMirrorToAnnotationFieldType: unexpected argument %s [%s %s]", tm, tm.getKind(), tm.getClass());
}
}
use of scenelib.annotations.field.AnnotationFieldType in project checker-framework by typetools.
the class AnnotationConverter method annotationMirrorToAnnotation.
/**
* Converts an {@link javax.lang.model.element.AnnotationMirror} into an {@link
* scenelib.annotations.Annotation}.
*
* @param am the AnnotationMirror
* @return the Annotation
*/
public static Annotation annotationMirrorToAnnotation(AnnotationMirror am) {
// TODO: bug for inner classes
@SuppressWarnings("signature:argument") AnnotationDef def = new AnnotationDef(AnnotationUtils.annotationName(am), String.format("annotationMirrorToAnnotation %s [%s] keyset=%s", am, am.getClass(), am.getElementValues().keySet()));
Map<String, AnnotationFieldType> fieldTypes = new HashMap<>(am.getElementValues().size());
// Handling cases where there are fields in annotations.
for (ExecutableElement ee : am.getElementValues().keySet()) {
AnnotationFieldType aft = getAnnotationFieldType(ee);
fieldTypes.put(ee.getSimpleName().toString(), aft);
}
def.setFieldTypes(fieldTypes);
// Now, we handle the values of those types below
Map<? extends ExecutableElement, ? extends AnnotationValue> values = am.getElementValues();
Map<String, Object> newValues = new HashMap<>(values.size());
for (ExecutableElement ee : values.keySet()) {
Object value = values.get(ee).getValue();
if (value instanceof List) {
// If we have a List here, then it is a List of AnnotationValue.
// Convert each AnnotationValue to its respective Java type.
@SuppressWarnings("unchecked") List<AnnotationValue> valueList = (List<AnnotationValue>) value;
value = CollectionsPlume.mapList(AnnotationValue::getValue, valueList);
} else if (value instanceof TypeMirror) {
try {
value = Class.forName(TypesUtils.binaryName((TypeMirror) value));
} catch (ClassNotFoundException e) {
throw new BugInCF(e, "value = %s [%s]", value, value.getClass());
}
}
newValues.put(ee.getSimpleName().toString(), value);
}
Annotation out = new Annotation(def, newValues);
return out;
}
use of scenelib.annotations.field.AnnotationFieldType in project checker-framework by typetools.
the class SceneToStubWriter method formatAnnotation.
/**
* Returns the String representation of an annotation in Java source format.
*
* @param a the annotation to print
* @return the formatted annotation
*/
public static String formatAnnotation(Annotation a) {
String fullAnnoName = a.def().name;
String simpleAnnoName = fullAnnoName.substring(fullAnnoName.lastIndexOf('.') + 1);
if (a.fieldValues.isEmpty()) {
return "@" + simpleAnnoName;
}
StringJoiner sj = new StringJoiner(", ", "@" + simpleAnnoName + "(", ")");
if (a.fieldValues.size() == 1 && a.fieldValues.containsKey("value")) {
AnnotationFieldType aft = a.def().fieldTypes.get("value");
sj.add(aft.format(a.fieldValues.get("value")));
} else {
for (Map.Entry<String, Object> f : a.fieldValues.entrySet()) {
AnnotationFieldType aft = a.def().fieldTypes.get(f.getKey());
sj.add(f.getKey() + "=" + aft.format(f.getValue()));
}
}
return sj.toString();
}
Aggregations