How to use "javax.lang.model.element.ElementVisitor"?
I experiment with java annotation processors and try to understand the usage of the classes in "javax.lang.model". For what I have read I think the ElementVisitor is to be intended as the primary way to work with the model. But I don't understand how to use it properly.
I knew the visitor pattern. Up to now I've used it to avoid iterating over the children of an element (and the children of the children ...) and avoiding ugly "instanceof" tests. But this visitor seems to be different. If I call "accept" on an model element it doesn't visit the children but only the element itself.
Could someone provide assistance on how to make use of the API?
I've found the following link: http://www.cs.bgu.ac.il/~gwiener/programming/visitors-galore/#more-113 . But using one visi开发者_高级运维tor inside the other inside the other ... just feel not right!?
Edit: To make it easier to understand the question I copy the code from the link above. The following code doesn't seem to be "right". I can't believe that an official java API is designed in this way. But how to use the ElementVisitor properly?
tElem.accept(new SimpleElementVisitor6<Void, ...>() {
public Void visitType(TypeElement e, ...) {
for (Element tSubElem : e.getEnclosedElements()) {
tSubElem.accept(new SimpleElementVisitor6<AssocEndSpec, ...>() {
public AssocEndSpec visitExecutable(ExecutableElement ex, ...) {
TypeMirror tRetTypeMirror = ex.getReturnType();
tRetTypeMirror.accept(new SimpleTypeVisitor6<TypeElement, TypeElement>() {
public TypeElement visitDeclared(DeclaredType t, TypeElement enclose) {
for (TypeMirror tTypeArgMirror : t.getTypeArguments()) {
tTypeArgMirror.accept(new SimpleTypeVisitor6<TypeElement, ...>() {
public TypeElement visitDeclared(DeclaredType t, TypeElement self) {
TypeElement tArgTypeElem = (TypeElement) t.asElement();
if (!self.equals(tArgTypeElem)) {
// found the little bugger!
}
}
}, ...);
}
}
}, ...);
}
}, ...);
}
}, ...);
This code is bollocks.
Take a look at javax.lang.model.util.ElementKindVisitor6
or javax.lang.model.util.ElementScanner6
, they might do what. In any case, you should be able to take their sources and adapt them to your needs.
NB: that being said, yes I'd say too that ElementVisitor is a rather strange implementation of a visitor.
I had the same issue, and javax.lang.model.util.ElementScanner9
was what I was looking for. Quoting the javdoc regarding ElementScanner9 (enphasis mine):
The visitXYZ methods in this class scan their component elements by calling scan on their enclosed elements, parameters, etc., as indicated in the individual method specifications
Since examples of these visitor implementation is scarse on the internet, here's what I've coded to make it work (the problem was to fetch all the fields of a given class):
import java.util.ArrayList;
import java.util.List;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementScanner9;
/**
* visitor which collects all the fields of a class
*
* <pre>{@code
* TypeElement t = ...;
* List<VariableElement> fields = t.accept(new FieldElementVisitor(), null);
* }</pre>
*
* @param <P>
*/
public class FieldElementVisitor<P> extends ElementScanner9<List<VariableElement>, P>{
protected List<VariableElement> variables;
public FieldElementVisitor() {
super(new ArrayList<>());
this.variables = this.DEFAULT_VALUE;
}
@Override
public List<VariableElement> visitVariable(VariableElement e, P p) {
if (isVariableAClassField(e)) { //to implement
this.variables.add(e);
}
return this.variables;
}
}
And here's what I've coded for using it:
TypeElement classElement = ...;
List<VariableElement> fields = new FieldElementVisitor<Void>().visit(classElement);
In the above example I didn't need it, but you can customize a visitXYZ
method and still call visit
method on its children as well by calling super.visitXYZ
, as the javadoc says (emphasis mine).
When a subclass overrides a visitXYZ method, the new method can cause the enclosed elements to be scanned in the default way by calling super.visitXYZ. In this fashion, the concrete visitor can control the ordering of traversal over the component elements with respect to the additional processing;
精彩评论