下面列出了javax.lang.model.element.Element#accept ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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;
}
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;
}
/**
* 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;
}
@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;
}
@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;
}
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();
}
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);
}
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);
}
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;
}
public static boolean validateElement(Element element) {
return element.accept(ELEMENT_VALIDATING_VISITOR, null);
}
public void driveVisitor(Element fullElement, ClassVisitor visitor) {
fullElement.accept(new ElementVisitorAdapter(), visitor);
visitor.visitEnd();
}
/**
* Visitor a command fragment class.
*
* @param elt element to visit
* @return meta-model
*/
CommandFragmentMetaModel visitCommandFragment(Element elt) {
return elt.accept(new CommandFragmentVisitor(), null);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}