下面列出了怎么用org.objectweb.asm.commons.EmptyVisitor的API类实例代码及写法,或者点击链接到github查看源代码。
private void assertFiledTypes(final Class<?> type, String... expected)
throws IOException {
final ClassReader reader = new ClassReader(type.getName());
final Set<String> actual = new HashSet<String>();
reader.accept(new EmptyVisitor() {
@Override
public FieldVisitor visitField(int access, String name,
String desc, String signature, Object value) {
if ((access & Opcodes.ACC_SYNTHETIC) == 0) {
if (signature == null) {
signature = desc;
}
cyclicRefsTest.getTypesFromSignature(signature, actual);
}
return null;
}
}, 0);
assertEquals(new HashSet<String>(Arrays.asList(expected)), actual);
}
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
final String className = Type.getType(desc).getClassName();
final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
return new EmptyVisitor() {
public void visit(String name, Object value) {
// Explicitly defined annotation attribute value.
attributes.put(name, value);
}
public void visitEnd() {
try {
Class annotationClass = classLoader.loadClass(className);
// Check declared default values of attributes in the annotation type.
Method[] annotationAttributes = annotationClass.getMethods();
for (int i = 0; i < annotationAttributes.length; i++) {
Method annotationAttribute = annotationAttributes[i];
String attributeName = annotationAttribute.getName();
Object defaultValue = annotationAttribute.getDefaultValue();
if (defaultValue != null && !attributes.containsKey(attributeName)) {
attributes.put(attributeName, defaultValue);
}
}
// Register annotations that the annotation type is annotated with.
Annotation[] metaAnnotations = annotationClass.getAnnotations();
Set<String> metaAnnotationTypeNames = new HashSet<String>();
for (Annotation metaAnnotation : metaAnnotations) {
metaAnnotationTypeNames.add(metaAnnotation.annotationType().getName());
}
metaAnnotationMap.put(className, metaAnnotationTypeNames);
}
catch (ClassNotFoundException ex) {
// Class not found - can't determine meta-annotations.
}
attributesMap.put(className, attributes);
}
};
}
private Thread createWeaverThread() {
return new Thread(new Runnable() {
public void run() {
Cursor oldCursor = getTopLevelAncestor().getCursor();
getTopLevelAncestor().setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
status.setText("Running");
RetroWeaver weaver = new RetroWeaver(version);
messages.setText("");
weaver.setListener(RetroWeaverGui.this);
String refCp = refClassPath.getText();
if (refCp.length() != 0) {
java.util.List<String> classpath = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(refCp,
File.pathSeparator);
while (st.hasMoreTokens()) {
classpath.add(st.nextToken());
}
RefVerifier verifier = new RefVerifier(version, new EmptyVisitor(), classpath,
RetroWeaverGui.this);
weaver.setVerifier(verifier);
}
weaver.weave(path);
status.setText("Done");
} catch (Exception ex) {
status.setText("Error: " + ex.getMessage());
} finally {
getTopLevelAncestor().setCursor(oldCursor);
}
}
});
}