use of org.objectweb.asm.tree.MethodNode in project felix by apache.
the class NamesTestCase method testSpecificationRecognizedOnlyMap.
public void testSpecificationRecognizedOnlyMap() throws Exception {
MethodNode node = new MethodNode();
node.name = "handle";
node.desc = "(Ljava/util/Map;)V";
assertNull(getMethodIdentifier(node));
}
use of org.objectweb.asm.tree.MethodNode in project felix by apache.
the class NamesTestCase method testSpecificationRecognizedWithDictionary.
public void testSpecificationRecognizedWithDictionary() throws Exception {
MethodNode node = new MethodNode();
node.name = "handle";
node.desc = "(Lmy/Service;Ljava/util/Dictionary;)V";
assertEquals("my.Service", getMethodIdentifier(node));
}
use of org.objectweb.asm.tree.MethodNode in project felix by apache.
the class ClassScanner method extractAnnotation.
/**
* Extract annotations
*/
private final List<ScannedAnnotation> extractAnnotation(final ClassNode classNode, final Class<?> annotatedClass) throws SCRDescriptorException {
final List<ScannedAnnotation> descriptions = new ArrayList<ScannedAnnotation>();
// first parse class annotations
@SuppressWarnings("unchecked") final List<AnnotationNode> annotations = getAllAnnotations(classNode.invisibleAnnotations, classNode.visibleAnnotations);
if (annotations != null) {
for (final AnnotationNode annotation : annotations) {
this.parseAnnotation(descriptions, annotation, annotatedClass);
}
// second parse method annotations
@SuppressWarnings("unchecked") final List<MethodNode> methods = classNode.methods;
if (methods != null) {
for (final MethodNode method : methods) {
final String name = method.name;
// check for constructor
if (!"<init>".equals(name)) {
@SuppressWarnings("unchecked") final List<AnnotationNode> annos = getAllAnnotations(method.invisibleAnnotations, method.visibleAnnotations);
if (annos != null) {
final Type[] signature = Type.getArgumentTypes(method.desc);
final Method[] allMethods = annotatedClass.getDeclaredMethods();
Method found = null;
for (final Method m : allMethods) {
if (m.getName().equals(name)) {
if (m.getParameterTypes().length == 0 && (signature == null || signature.length == 0)) {
found = m;
}
if (m.getParameterTypes().length > 0 && signature != null && m.getParameterTypes().length == signature.length) {
found = m;
for (int index = 0; index < m.getParameterTypes().length; index++) {
String parameterTypeName = m.getParameterTypes()[index].getName();
// Name of array parameters is returned with syntax [L<name>;, convert to <name>[]
Matcher matcher = ARRAY_PARAM_TYPE_NAME.matcher(parameterTypeName);
if (matcher.matches()) {
parameterTypeName = matcher.group(1) + "[]";
}
if (!parameterTypeName.equals(signature[index].getClassName()) && !m.getParameterTypes()[index].getSimpleName().equals(signature[index].getClassName())) {
found = null;
}
}
}
// if method is found return it now, to avoid resetting 'found' to null if next method has same name but different parameters
if (found != null) {
break;
}
}
}
if (found == null) {
throw new SCRDescriptorException("Annotated method " + name + " not found.", annotatedClass.getName());
}
for (final AnnotationNode annotation : annos) {
parseAnnotation(descriptions, annotation, found);
}
}
}
}
}
// third parse field annotations
@SuppressWarnings("unchecked") final List<FieldNode> fields = classNode.fields;
if (fields != null) {
for (final FieldNode field : fields) {
@SuppressWarnings("unchecked") final List<AnnotationNode> annos = getAllAnnotations(field.invisibleAnnotations, field.visibleAnnotations);
if (annos != null) {
final String name = field.name;
final Field[] allFields = annotatedClass.getDeclaredFields();
Field found = null;
for (final Field f : allFields) {
if (f.getName().equals(name)) {
found = f;
break;
}
}
if (found == null) {
throw new SCRDescriptorException("Annotated field " + name + " not found.", annotatedClass.getName());
}
for (final AnnotationNode annotation : annos) {
parseAnnotation(descriptions, annotation, found);
}
}
}
}
}
return descriptions;
}
use of org.objectweb.asm.tree.MethodNode in project bytecode-viewer by Konloch.
the class CodeSequenceDiagram method execute.
@Override
public void execute(ArrayList<ClassNode> classNodeList) {
if (BytecodeViewer.viewer.workPane.getCurrentViewer() == null || !(BytecodeViewer.viewer.workPane.getCurrentViewer() instanceof ClassViewer)) {
BytecodeViewer.showMessage("First open a class file.");
return;
}
ClassNode c = BytecodeViewer.viewer.workPane.getCurrentViewer().cn;
if (c == null) {
BytecodeViewer.showMessage("ClassNode is null for CodeSequenceDiagram. Please report to @Konloch");
return;
}
JFrame frame = null;
if (c.name != null)
frame = new JFrame("Code Sequence Diagram - " + c.name);
else
frame = new JFrame("Code Sequence Diagram - Unknown Name");
frame.setIconImages(Resources.iconList);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400, 320);
mxGraph graph = new mxGraph();
graph.setVertexLabelsMovable(false);
graph.setGridEnabled(true);
graph.setEnabled(false);
graph.setCellsEditable(false);
graph.setCellsSelectable(false);
graph.setCellsMovable(false);
graph.setCellsLocked(true);
Object parent = graph.getDefaultParent();
Font font = UIManager.getDefaults().getFont("TabbedPane.font");
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
graph.getModel().beginUpdate();
try {
int testX = 10;
int testY = 0;
double magicNumber = 5.8;
for (MethodNode m : (ArrayList<MethodNode>) c.methods) {
String mIdentifier = c.name + "." + m.name + m.desc;
Object node = graph.insertVertex(parent, null, mIdentifier, testX, testY, mIdentifier.length() * magicNumber, 30);
Object attach = node;
testX += (int) (font.getStringBounds(mIdentifier, frc).getWidth()) + 60;
for (AbstractInsnNode i : m.instructions.toArray()) {
if (i instanceof MethodInsnNode) {
MethodInsnNode mi = (MethodInsnNode) i;
String identifier = mi.owner + "." + mi.name + mi.desc;
Object node2 = graph.insertVertex(parent, null, identifier, testX, testY, identifier.length() * 5, 30);
testX += (int) (font.getStringBounds(identifier, frc).getWidth()) + 60;
graph.insertEdge(parent, null, null, attach, node2);
attach = node2;
}
}
testY += 60;
testX = 10;
}
} finally {
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
frame.getContentPane().add(graphComponent);
frame.setVisible(true);
}
use of org.objectweb.asm.tree.MethodNode in project bytecode-viewer by Konloch.
the class ClassNodeDecompiler method decompile.
protected static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, ArrayList<String> decompiledClasses, String containerName, ClassNode cn) {
ArrayList<String> unableToDecompile = new ArrayList<String>();
decompiledClasses.add(cn.name);
sb.append(getAccessString(cn.access));
sb.append(" ");
sb.append(cn.name);
if (cn.superName != null && !cn.superName.equals("java/lang/Object")) {
sb.append(" extends ");
sb.append(cn.superName);
}
int amountOfInterfaces = cn.interfaces.size();
if (amountOfInterfaces > 0) {
sb.append(" implements ");
sb.append(cn.interfaces.get(0));
if (amountOfInterfaces > 1) {
// sb.append(",");
}
for (int i = 1; i < amountOfInterfaces; i++) {
sb.append(", ");
sb.append(cn.interfaces.get(i));
}
}
sb.append(" {");
sb.append(BytecodeViewer.nl);
for (FieldNode fn : (List<FieldNode>) cn.fields) {
sb.append(BytecodeViewer.nl);
sb.append(" ");
FieldNodeDecompiler.decompile(sb, fn);
}
if (cn.fields.size() > 0) {
sb.append(BytecodeViewer.nl);
}
for (MethodNode mn : (List<MethodNode>) cn.methods) {
sb.append(BytecodeViewer.nl);
MethodNodeDecompiler.decompile(sb, mn, cn);
}
for (Object o : cn.innerClasses) {
InnerClassNode innerClassNode = (InnerClassNode) o;
String innerClassName = innerClassNode.name;
if ((innerClassName != null) && !decompiledClasses.contains(innerClassName)) {
decompiledClasses.add(innerClassName);
ClassNode cn1 = BytecodeViewer.getClassNode(containerName, innerClassName);
if (cn1 != null) {
sb.appendPrefix(" ");
sb.append(BytecodeViewer.nl + BytecodeViewer.nl);
sb = decompile(sb, decompiledClasses, containerName, cn1);
sb.trimPrefix(5);
sb.append(BytecodeViewer.nl);
} else {
unableToDecompile.add(innerClassName);
}
}
}
if (!unableToDecompile.isEmpty()) {
sb.append("//the following inner classes couldn't be decompiled: ");
for (String s : unableToDecompile) {
sb.append(s);
sb.append(" ");
}
sb.append(BytecodeViewer.nl);
}
sb.append("}");
// " with prefix length: " + sb.prefix.length());
return sb;
}
Aggregations