Java Code Examples for javax.lang.model.element.Element#accept()

The following examples show how to use javax.lang.model.element.Element#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Visitor.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
@Override
public CommandFragmentMetaModel visitType(TypeElement type, Void p) {
    List<MetaModel<?>> params = null;
    for (Element elt : type.getEnclosedElements()) {
        if (elt.getKind() == ElementKind.CONSTRUCTOR && elt.getAnnotation(Creator.class) != null) {
            params = elt.accept(new ConstructorVisitor(), null);
            break;
        }
    }
    if (params != null) {
        String pkg = env.getElementUtils().getPackageOf(type).getQualifiedName().toString();
        CommandFragmentMetaModel model = new CommandFragmentMetaModel(type, pkg, params);
        fragmentsByQualifiedName.put(type.getQualifiedName().toString(), model);
        return model;
    }
    env.getMessager().printMessage(Diagnostic.Kind.ERROR,
                String.format("No constructor annotated with @%s found", Creator.class.getSimpleName()),
                type);
    return null;
}
 
Example 2
Source File: JavaUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public TypeElement visitType(TypeElement typeElement, String binaryName) {
    String bName = ElementUtilities.getBinaryName(typeElement);
    if(binaryName.equals(bName)) {
        return typeElement;
    } else if(binaryName.startsWith(bName)) {
        for(Element child : typeElement.getEnclosedElements()) {
            if(!child.getKind().isClass()) {
                continue;
            }
            
            TypeElement retVal = child.accept(this, binaryName);
            if(retVal != null) {
                return retVal;
            }
        }
    }
    
    return null;
}
 
Example 3
Source File: RetainerLUTModel.java    From Akatsuki with Apache License 2.0 6 votes vote down vote up
private Map<String, RetainedStateModel> findAllTypes(final Element element,
		final Map<String, RetainedStateModel> referenceMap) {
	Map<String, RetainedStateModel> modelMap = new HashMap<>();
	element.accept(new SimpleElementVisitor8<Void, Map<String, RetainedStateModel>>() {

		@Override
		public Void visitType(TypeElement e, Map<String, RetainedStateModel> map) {
			if (e.getKind() == ElementKind.CLASS) {
				// only process class that isn't in the map
				if (!referenceMap.containsKey(e.getQualifiedName().toString())) {
					findInheritedModel(e, referenceMap.values())
							.ifPresent(m -> map.put(e.getQualifiedName().toString(), m));
				}
				e.getEnclosedElements().forEach(ee -> ee.accept(this, map));
			}
			return null;
		}
	}, modelMap);
	return modelMap;
}
 
Example 4
Source File: SignatureFactory.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the type signature of the given element. If none is required by the VM spec, returns
 * null.
 */
@Nullable
public String getSignature(Element element) {
  SignatureWriter writer = new SignatureWriter();
  element.accept(elementVisitorAdapter, writer);
  String result = writer.toString();
  return result.isEmpty() ? null : result;
}
 
Example 5
Source File: JPAEditorUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public TypeElement visitPackage(PackageElement packElem, String binaryName) {
    for (Element e : packElem.getEnclosedElements()) {
        if (e.getKind().isClass()) {
            TypeElement ret = e.accept(this, binaryName);
            if (ret != null) {
                return ret;
            }
        }
    }

    return null;
}
 
Example 6
Source File: JavaUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public TypeElement visitPackage(PackageElement packElem, String binaryName) {
    for(Element e : packElem.getEnclosedElements()) {
        if(e.getKind().isClass()) {
            TypeElement ret = e.accept(this, binaryName);
            if(ret != null) {
                return ret;
            }
        }
    }
    
    return null;
}
 
Example 7
Source File: BuckModuleAnnotationProcessor.java    From buck with Apache License 2.0 5 votes vote down vote up
private List<BuckModuleDescriptor> collectBuckModuleDescriptors(
    Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  BuckModuleVisitor visitor = new BuckModuleVisitor(processingEnv);
  for (TypeElement annotation : annotations) {
    for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
      element.accept(visitor, annotation);
    }
  }

  if (visitor.hasData()) {
    return visitor.getBuckModuleDescriptors();
  }
  return Collections.emptyList();
}
 
Example 8
Source File: MoreElements.java    From doma with Apache License 2.0 5 votes vote down vote up
public TypeParameterElement toTypeParameterElement(Element element) {
  assertNotNull(element);
  return element.accept(
      new SimpleElementVisitor8<TypeParameterElement, Void>() {

        @Override
        public TypeParameterElement visitTypeParameter(TypeParameterElement e, Void aVoid) {
          return e;
        }
      },
      null);
}
 
Example 9
Source File: MoreElements.java    From doma with Apache License 2.0 5 votes vote down vote up
public TypeElement toTypeElement(Element element) {
  assertNotNull(element);
  return element.accept(
      new SimpleElementVisitor8<TypeElement, Void>() {

        // delegate to elementUtils
        public TypeElement visitType(TypeElement e, Void p) {
          return e;
        }
      },
      null);
}
 
Example 10
Source File: ExportNonAccessibleElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public List<ErrorDescription> run(CompilationInfo compilationInfo,
                                      TreePath treePath) {
        stop = false;
        Element e = compilationInfo.getTrees().getElement(treePath);
        if (e == null) {
            return null;
        }
        Boolean b = e.accept(this, null);

        if (b) {
            Element parent = e;
            for (;;) {
                if (stop) {
                    return null;
                }

                if (parent == null || parent.getKind() == ElementKind.PACKAGE) {
                    break;
                }
                if (!parent.getModifiers().contains(Modifier.PUBLIC) && !parent.getModifiers().contains(Modifier.PROTECTED)) {
                    return null;
                }
                parent = parent.getEnclosingElement();
            }

            //#124456: disabling the fix:
//            List<Fix> fixes = Collections.<Fix>singletonList(new FixImpl(
//                "MSG_ExportNonAccessibleElementMakeNonVisible", // NOI18N
//                TreePathHandle.create(e, compilationInfo), 
//                compilationInfo.getFileObject()
//            ));

            int[] span = null;

            switch (treePath.getLeaf().getKind()) {
                case METHOD: span = compilationInfo.getTreeUtilities().findNameSpan((MethodTree) treePath.getLeaf()); break;
                case ANNOTATION_TYPE:
                case CLASS:
                case ENUM:
                case INTERFACE:
                    span = compilationInfo.getTreeUtilities().findNameSpan((ClassTree) treePath.getLeaf()); break;
                case VARIABLE: span = compilationInfo.getTreeUtilities().findNameSpan((VariableTree) treePath.getLeaf()); break;
            }

            if (span != null) {
                ErrorDescription ed = ErrorDescriptionFactory.createErrorDescription(
                        getSeverity().toEditorSeverity(),
                        NbBundle.getMessage(ExportNonAccessibleElement.class, "MSG_ExportNonAccessibleElement"),
//                        fixes,
                        compilationInfo.getFileObject(),
                        span[0],
                        span[1]
                        );

                return Collections.singletonList(ed);
            }
        }
        
        return null;
    }
 
Example 11
Source File: ClassVisitorDriverFromElement.java    From buck with Apache License 2.0 4 votes vote down vote up
public void driveVisitor(Element fullElement, ClassVisitor visitor) {
  fullElement.accept(new ElementVisitorAdapter(), visitor);
  visitor.visitEnd();
}
 
Example 12
Source File: SuperficialValidation.java    From auto with Apache License 2.0 4 votes vote down vote up
public static boolean validateElement(Element element) {
  return element.accept(ELEMENT_VALIDATING_VISITOR, null);
}
 
Example 13
Source File: MoreElements.java    From auto with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the given {@link Element} instance as {@link VariableElement}.
 *
 * <p>This method is functionally equivalent to an {@code instanceof} check and a cast, but should
 * always be used over that idiom as instructed in the documentation for {@link Element}.
 *
 * @throws NullPointerException if {@code element} is {@code null}
 * @throws IllegalArgumentException if {@code element} isn't a {@link VariableElement}.
 */
public static VariableElement asVariable(Element element) {
  return element.accept(VariableElementVisitor.INSTANCE, null);
}
 
Example 14
Source File: Visitor.java    From helidon-build-tools with Apache License 2.0 2 votes vote down vote up
/**
 * Visitor a command fragment class.
 *
 * @param elt element to visit
 * @return meta-model
 */
CommandFragmentMetaModel visitCommandFragment(Element elt) {
    return elt.accept(new CommandFragmentVisitor(), null);
}
 
Example 15
Source File: MoreElements.java    From immutables with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the given {@link Element} instance as {@link PackageElement}.
 *
 * <p>This method is functionally equivalent to an {@code instanceof} check and a cast, but should
 * always be used over that idiom as instructed in the documentation for {@link Element}.
 *
 * @throws IllegalArgumentException if {@code element} isn't a {@link TypeElement}.
 */
public static PackageElement asPackage(Element element) {
  return element.accept(PackageElementVisitor.INSTANCE, null);
}
 
Example 16
Source File: MoreElements.java    From auto-parcel with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the given {@link Element} instance as {@link VariableElement}.
 * <p/>
 * <p>This method is functionally equivalent to an {@code instanceof} check and a cast, but should
 * always be used over that idiom as instructed in the documentation for {@link Element}.
 *
 * @throws NullPointerException     if {@code element} is {@code null}
 * @throws IllegalArgumentException if {@code element} isn't a {@link VariableElement}.
 */
public static VariableElement asVariable(Element element) {
    return element.accept(VARIABLE_ELEMENT_VISITOR, null);
}
 
Example 17
Source File: MoreElements.java    From auto-parcel with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the given {@link Element} instance as {@link TypeElement}.
 * <p/>
 * <p>This method is functionally equivalent to an {@code instanceof} check and a cast, but should
 * always be used over that idiom as instructed in the documentation for {@link Element}.
 *
 * @throws NullPointerException     if {@code element} is {@code null}
 * @throws IllegalArgumentException if {@code element} isn't a {@link TypeElement}.
 */
public static TypeElement asType(Element element) {
    return element.accept(TYPE_ELEMENT_VISITOR, null);
}
 
Example 18
Source File: MoreElements.java    From auto with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the given {@link Element} instance as {@link TypeParameterElement}.
 *
 * <p>This method is functionally equivalent to an {@code instanceof} check and a cast, but should
 * always be used over that idiom as instructed in the documentation for {@link Element}.
 *
 * @throws NullPointerException if {@code element} is {@code null}
 * @throws IllegalArgumentException if {@code element} isn't a {@link TypeParameterElement}.
 */
public static TypeParameterElement asTypeParameter(Element element) {
  return element.accept(TypeParameterElementVisitor.INSTANCE, null);
}
 
Example 19
Source File: MoreElements.java    From auto with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the given {@link Element} instance as {@link ExecutableElement}.
 *
 * <p>This method is functionally equivalent to an {@code instanceof} check and a cast, but should
 * always be used over that idiom as instructed in the documentation for {@link Element}.
 *
 * @throws NullPointerException if {@code element} is {@code null}
 * @throws IllegalArgumentException if {@code element} isn't a {@link ExecutableElement}.
 */
public static ExecutableElement asExecutable(Element element) {
  return element.accept(ExecutableElementVisitor.INSTANCE, null);
}
 
Example 20
Source File: MoreElements.java    From immutables with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the given {@link Element} instance as {@link TypeElement}.
 *
 * <p>This method is functionally equivalent to an {@code instanceof} check and a cast, but should
 * always be used over that idiom as instructed in the documentation for {@link Element}.
 *
 * @throws IllegalArgumentException if {@code element} isn't a {@link TypeElement}.
 */
public static TypeElement asType(Element element) {
  return element.accept(TypeElementVisitor.INSTANCE, null);
}