Search in sources :

Example 1 with SimpleElementVisitor8

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);
}
Also used : ProbingEnvironment(org.revapi.java.compilation.ProbingEnvironment) JavaTypeElement(org.revapi.java.spi.JavaTypeElement) JavaModelElement(org.revapi.java.spi.JavaModelElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Set(java.util.Set) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) FlatFilter(org.revapi.java.FlatFilter) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) Util(org.revapi.java.spi.Util) DeclaredType(javax.lang.model.type.DeclaredType) Archive(org.revapi.Archive) ClassPathUseSite(org.revapi.java.compilation.ClassPathUseSite) UseSite(org.revapi.java.spi.UseSite) SimpleElementVisitor8(javax.lang.model.util.SimpleElementVisitor8) Collections(java.util.Collections) Nonnull(javax.annotation.Nonnull) JavaModelElement(org.revapi.java.spi.JavaModelElement) JavaTypeElement(org.revapi.java.spi.JavaTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JavaTypeElement(org.revapi.java.spi.JavaTypeElement) JavaModelElement(org.revapi.java.spi.JavaModelElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) List(java.util.List)

Aggregations

Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Nonnull (javax.annotation.Nonnull)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 VariableElement (javax.lang.model.element.VariableElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1 TypeMirror (javax.lang.model.type.TypeMirror)1 SimpleElementVisitor8 (javax.lang.model.util.SimpleElementVisitor8)1 Archive (org.revapi.Archive)1 FlatFilter (org.revapi.java.FlatFilter)1 ClassPathUseSite (org.revapi.java.compilation.ClassPathUseSite)1 ProbingEnvironment (org.revapi.java.compilation.ProbingEnvironment)1 JavaModelElement (org.revapi.java.spi.JavaModelElement)1 JavaTypeElement (org.revapi.java.spi.JavaTypeElement)1 UseSite (org.revapi.java.spi.UseSite)1 Util (org.revapi.java.spi.Util)1