下面列出了javax.lang.model.type.UnknownTypeException#javax.lang.model.util.SimpleTypeVisitor7 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void process(Element element) throws Exception {
final Element enclosingElement = element.getEnclosingElement();
if (enclosingElement.getAnnotation(Entry.class) == null) {
throw new ElementException("Class containing @Relation must be annotated with @Entry", enclosingElement);
}
element.asType().accept(new SimpleTypeVisitor7<Void, Void>() {
@Override
public Void visitDeclared(DeclaredType t, Void unused) {
final List<? extends TypeMirror> args = t.getTypeArguments();
if (args.isEmpty()) {
processOneToOne(element, t.asElement());
} else {
processOneToMany(element, (TypeElement) t.asElement(), args.get(0));
}
return super.visitDeclared(t, unused);
}
}, null);
}
private void processOneToMany(Element field, Element collectionType, TypeMirror relation) {
if (!mTypeUtils.isAssignable(collectionType.asType(), List.class)) {
throw new ElementException("Relation type must be subclass of List<E>", field);
}
relation.accept(new SimpleTypeVisitor7<Void, Void>() {
@Override
public Void visitDeclared(DeclaredType t, Void unused) {
final Element element = t.asElement();
if (element.getAnnotation(Entry.class) == null) {
throw new ElementException("Related type must be annotated with @Entry", element);
}
final Element enclosingElement = field.getEnclosingElement();
final TableSpec lTable = mCompileGraph.findTableSpec(enclosingElement);
final TableSpec rTable = mCompileGraph.findTableSpec(element);
final ClassName className = makeClassName(field);
final RelationSpec relationSpec = new RelationSpec(
field, className, lTable, rTable, true);
mCompileGraph.putRelationSpec(enclosingElement, relationSpec);
return super.visitDeclared(t, unused);
}
}, null);
}
/**
* Tracks the superclass and superinterfaces for the given type and if the type inherits from
* {@link com.google.appinventor.components.runtime.Component} then it adds the class to the
* componentTypes list. This allows properties, methods, and events to use concrete Component
* types as parameters and return values.
*
* @param type a TypeMirror representing a type on the class path
*/
private void updateComponentTypes(TypeMirror type) {
if (type.getKind() == TypeKind.DECLARED) {
type.accept(new SimpleTypeVisitor7<Boolean, Set<String>>(false) {
@Override
public Boolean visitDeclared(DeclaredType t, Set<String> types) {
final String typeName = t.asElement().toString();
if ("com.google.appinventor.components.runtime.Component".equals(typeName)) {
return true;
}
if (!types.contains(typeName)) {
types.add(typeName);
final TypeElement typeElement = (TypeElement) t.asElement();
if (typeElement.getSuperclass().accept(this, types)) {
componentTypes.add(typeName);
return true;
}
for (TypeMirror iface : typeElement.getInterfaces()) {
if (iface.accept(this, types)) {
componentTypes.add(typeName);
return true;
}
}
}
return componentTypes.contains(typeName);
}
}, visitedTypes);
}
}
/**
* <p>
* Retrieve parametrized type of element (from its parent).
* </p>
*
* @param element
* the element
* @return list of typemirror or empty list
*/
public static List<TypeName> getTypeArguments(TypeElement element) {
final List<TypeName> result = new ArrayList<>();
if (element.getKind() == ElementKind.CLASS) {
if (element.getSuperclass() instanceof DeclaredType) {
result.addAll(convert(((DeclaredType) element.getSuperclass()).getTypeArguments()));
}
} else if (element.getKind() == ElementKind.INTERFACE) {
List<? extends TypeMirror> interfaces = element.getInterfaces();
for (TypeMirror item : interfaces) {
item.accept(new SimpleTypeVisitor7<Void, Void>() {
@Override
public Void visitDeclared(DeclaredType t, Void p) {
result.addAll(convert(t.getTypeArguments()));
return null;
}
}, null);
}
}
return result;
}
public TypeName forTypeMirror(TypeMirror mirror) {
return mirror.accept(new SimpleTypeVisitor7<TypeName, Void>(){
}, null);
}