use of javax.lang.model.util.SimpleElementVisitor8 in project revapi by revapi.
the class TypeElement method getModel.
private JavaModelElement getModel(Element element, int indexInParent) {
return element.accept(new SimpleElementVisitor8<JavaModelElement, Void>() {
@Override
public JavaModelElement visitVariable(VariableElement e, Void ignored) {
if (e.getEnclosingElement() instanceof javax.lang.model.element.TypeElement) {
// this is a field
TypeElement type = environment.getTypeMap().get(e.getEnclosingElement());
if (type == null) {
return null;
}
List<FieldElement> fs = type.searchChildren(FieldElement.class, false, FlatFilter.by(f -> f.getDeclaringElement().equals(e)));
return fs.get(0);
} else if (e.getEnclosingElement() instanceof javax.lang.model.element.ExecutableElement) {
// this is a method parameter
Element methodEl = e.getEnclosingElement();
TypeElement type = environment.getTypeMap().get(methodEl.getEnclosingElement());
if (type == null) {
return null;
}
List<MethodElement> ms = type.searchChildren(MethodElement.class, false, FlatFilter.by(m -> m.getDeclaringElement().equals(methodEl)));
MethodElement method = ms.get(0);
// now look for the parameter
List<MethodParameterElement> params = method.searchChildren(MethodParameterElement.class, false, FlatFilter.by(p -> true));
return params.get(indexInParent);
} else {
return null;
}
}
@Override
public JavaModelElement visitType(javax.lang.model.element.TypeElement e, Void ignored) {
return environment.getTypeMap().get(e);
}
@Override
public JavaModelElement visitExecutable(ExecutableElement e, Void ignored) {
TypeElement type = environment.getTypeMap().get(e.getEnclosingElement());
if (type == null) {
return null;
}
List<MethodElement> ms = type.searchChildren(MethodElement.class, false, FlatFilter.by(m -> m.getDeclaringElement().equals(e)));
return ms.get(0);
}
}, null);
}
Aggregations