List of usage examples for org.objectweb.asm Opcodes ACC_NATIVE
int ACC_NATIVE
To view the source code for org.objectweb.asm Opcodes ACC_NATIVE.
Click Source Link
From source file:com.sun.tdk.jcov.instrument.DataMethod.java
License:Open Source License
/** * Checks whether this method has 'native' modifier * * @return true if method is native//from www . j a v a2 s.com */ public boolean hasNativeModifier() { return (access & Opcodes.ACC_NATIVE) != 0; }
From source file:edu.ubc.mirrors.holograms.HologramClassGenerator.java
License:Open Source License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if (name.equals("<clinit>")) { hasClinit = true;//from w w w .j a v a2 s. co m } // TODO-RS: Remove me - avoiding a race condition in ZipFileInflaterInputStream... if (name.equals("finalize")) { return null; } if (name.equals("<init>")) { // Add the implicit mirror argument desc = addMirrorParam(desc); } // toString() is a special case - it's defined in java.lang.Object, which this class must ultimately // extend, so we have to return a real String rather than a hologram. boolean isToString = name.equals("toString") && desc.equals(Type.getMethodDescriptor(getHologramType(Type.getType(String.class)))); if (isToString) { desc = Type.getMethodDescriptor(Type.getType(String.class)); } if (name.equals("equals") && desc.equals(Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Hologram.class)))) { desc = Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class)); } boolean isGetStackTrace = this.name.equals(hologramThrowableType.getInternalName()) && name.equals("getStackTrace") && desc.equals(Type.getMethodDescriptor(getHologramType(Type.getType(StackTraceElement[].class)))); if (isGetStackTrace) { desc = Type.getMethodDescriptor(Type.getType(StackTraceElement[].class)); } if (name.equals("fillInStackTrace") && this.name.equals(hologramThrowableType.getInternalName())) { // Omit this - we'll use the Throwable superclass version return null; } // Take off the native keyword if it's there - we're going to fill in an actual // method (even if it's a stub that throws an exception). int hologramAccess = ~Opcodes.ACC_NATIVE & access; // Mild hack: generated method accessors are defined using ClassDefiner and Unsafe, // allowing them to make illegal access to this package-private constructor. if (this.name.equals("hologram/sun/reflect/MethodAccessorImpl") && name.equals("<init>")) { hologramAccess = forcePublic(hologramAccess); } MethodVisitor superVisitor = super.visitMethod(hologramAccess, name, desc, signature, exceptions); HologramMethodGenerator generator = new HologramMethodGenerator(this.name, hologramAccess, name, desc, superVisitor, isToString, isGetStackTrace); LocalVariablesSorter lvs = new LocalVariablesSorter(access, desc, generator); generator.setLocalVariablesSorter(lvs); boolean needsThunk = (Opcodes.ACC_NATIVE & access) != 0; if (!needsThunk && !name.startsWith("<")) { if ((Opcodes.ACC_ABSTRACT & access) == 0) { MethodMirror method; try { method = Reflection.getDeclaredMethod(classMirror, name, getOriginalType(Type.getMethodType(desc))); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } MirrorInvocationHandler handler = ClassHolograph.getMethodHandler(method); if (handler != null) { needsThunk = true; } } } if (needsThunk) { generator.generateNativeThunk(); return null; } return lvs; }
From source file:edu.ubc.mirrors.test.NativeMethodCounter.java
License:Open Source License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if ((access & Opcodes.ACC_NATIVE) != 0) { nativeMethodCount++;/*from w w w. ja v a 2 s . c o m*/ Set<MethodNode> methods = classesWithNativeMethods.get(currentClass); if (methods == null) { methods = new HashSet<MethodNode>(); classesWithNativeMethods.put(currentClass, methods); } methods.add(new MethodNode(access, name, desc, signature, exceptions)); } return super.visitMethod(access, name, desc, signature, exceptions); }
From source file:graph.Modifier.java
License:Apache License
/** * Returns an integer bit field for the EnumSet, where the bit values are * defined as in the class file format.// w ww . j av a2 s . c o m * * @param set Set of modifiers to convert to a bit field. * @return Integer bit field. */ public static int getBitField(EnumSet<Modifier> set) { int access = 0; if (set.contains(PUBLIC)) { access |= Opcodes.ACC_PUBLIC; } if (set.contains(PROTECTED)) { access |= Opcodes.ACC_PROTECTED; } if (set.contains(PRIVATE)) { access |= Opcodes.ACC_PRIVATE; } if (set.contains(STATIC)) { access |= Opcodes.ACC_STATIC; } if (set.contains(FINAL)) { access |= Opcodes.ACC_FINAL; } if (set.contains(SUPER)) { access |= Opcodes.ACC_SUPER; } if (set.contains(INTERFACE)) { access |= Opcodes.ACC_INTERFACE; } if (set.contains(ABSTRACT)) { access |= Opcodes.ACC_ABSTRACT; } if (set.contains(SYNTHETIC)) { access |= Opcodes.ACC_SYNTHETIC; } if (set.contains(ANNOTATION)) { access |= Opcodes.ACC_ANNOTATION; } if (set.contains(ENUM)) { access |= Opcodes.ACC_ENUM; } if (set.contains(VOLATILE)) { access |= Opcodes.ACC_VOLATILE; } if (set.contains(TRANSIENT)) { access |= Opcodes.ACC_TRANSIENT; } if (set.contains(SYNCHRONIZED)) { access |= Opcodes.ACC_SYNCHRONIZED; } if (set.contains(BRIDGE)) { access |= Opcodes.ACC_BRIDGE; } if (set.contains(VARARGS)) { access |= Opcodes.ACC_VARARGS; } if (set.contains(NATIVE)) { access |= Opcodes.ACC_NATIVE; } return access; }
From source file:graph.Modifier.java
License:Apache License
/** * Returns an EnumSet of the modifiers, based on the given bit field (where * the bit values are defined as in the class file format). * * @param access Integer bit field giving access modifiers. * @return Set of modifiers./* w w w.j av a 2 s . co m*/ */ public static EnumSet<Modifier> getSet(int access) { EnumSet<Modifier> modifiers = EnumSet.noneOf(Modifier.class); if ((access & Opcodes.ACC_PUBLIC) != 0) { modifiers.add(Modifier.PUBLIC); } if ((access & Opcodes.ACC_PROTECTED) != 0) { modifiers.add(Modifier.PROTECTED); } if ((access & Opcodes.ACC_PRIVATE) != 0) { modifiers.add(Modifier.PRIVATE); } if ((access & Opcodes.ACC_STATIC) != 0) { modifiers.add(Modifier.STATIC); } if ((access & Opcodes.ACC_FINAL) != 0) { modifiers.add(Modifier.FINAL); } if ((access & Opcodes.ACC_SUPER) != 0) { modifiers.add(Modifier.SUPER); } if ((access & Opcodes.ACC_INTERFACE) != 0) { modifiers.add(Modifier.INTERFACE); } if ((access & Opcodes.ACC_ABSTRACT) != 0) { modifiers.add(Modifier.ABSTRACT); } if ((access & Opcodes.ACC_SYNTHETIC) != 0) { modifiers.add(Modifier.SYNTHETIC); } if ((access & Opcodes.ACC_ANNOTATION) != 0) { modifiers.add(Modifier.ANNOTATION); } if ((access & Opcodes.ACC_ENUM) != 0) { modifiers.add(Modifier.ENUM); } if ((access & Opcodes.ACC_VOLATILE) != 0) { modifiers.add(Modifier.VOLATILE); } if ((access & Opcodes.ACC_TRANSIENT) != 0) { modifiers.add(Modifier.TRANSIENT); } if ((access & Opcodes.ACC_SYNCHRONIZED) != 0) { modifiers.add(Modifier.SYNCHRONIZED); } if ((access & Opcodes.ACC_BRIDGE) != 0) { modifiers.add(Modifier.BRIDGE); } if ((access & Opcodes.ACC_VARARGS) != 0) { modifiers.add(Modifier.VARARGS); } if ((access & Opcodes.ACC_NATIVE) != 0) { modifiers.add(Modifier.NATIVE); } if ((access & Opcodes.ACC_STRICT) != 0) { modifiers.add(Modifier.STRICT); } return modifiers; }
From source file:io.awacs.plugin.stacktrace.FilteredClassTransformer.java
License:Apache License
/** * // w ww. j a v a2s . c om * (!?)&&(!)&&(!)&&(!?)&&(!?)&&(!main) */ @Override protected boolean filterMethod(ClassNode cn, MethodNode mn) { return ((cn.access & Opcodes.ACC_INTERFACE) != Opcodes.ACC_INTERFACE && (cn.access & Opcodes.ACC_ABSTRACT) != Opcodes.ACC_ABSTRACT && (mn.access & Opcodes.ACC_NATIVE) != Opcodes.ACC_NATIVE && (mn.access & Opcodes.ACC_ABSTRACT) != Opcodes.ACC_ABSTRACT && !isConstructor(mn) && !isMainMethod(mn)); }
From source file:jaspex.speculation.CreateSpeculativeMethodVisitor.java
License:Open Source License
private static MethodVisitor createNextMethodVisitor(int access, String name, String desc, String signature, String[] exceptions, ClassVisitor cv) { if (name.endsWith("$transactional")) { name = name.replace("$transactional", "$speculative"); // Mtodo original podia ser nativo, mas o $speculative no access &= ~Opcodes.ACC_NATIVE; // Todos os mtodos $speculative devem ser public (seno o codegen falha com java.lang.IllegalAccessError) access = access & ~Opcodes.ACC_PRIVATE & ~Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC; }/* w ww . j a v a2 s. com*/ return cv.visitMethod(access, name, desc, signature, exceptions); }
From source file:jp.co.dgic.testing.common.virtualmock.asm2x.AsmClassVisitor2x.java
License:Open Source License
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { boolean isStatic = false; if ((access & Opcodes.ACC_STATIC) > 0) { isStatic = true;/*from w w w. j av a2 s. c o m*/ } InternalMockObjectManager.printConsole("#################################################################"); InternalMockObjectManager.printConsole("#################################################################"); InternalMockObjectManager .printConsole("### " + access + (isStatic ? " static " : " ") + name + " " + signature); InternalMockObjectManager.printConsole("#################################################################"); InternalMockObjectManager.printConsole("#################################################################"); MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); // is abstract or native if ((access & Opcodes.ACC_ABSTRACT) > 0) return mv; if ((access & Opcodes.ACC_NATIVE) > 0) return mv; if ((access & Opcodes.ACC_BRIDGE) > 0) return mv; int maxLocals = acc.getMaxLocals(name, desc); return createMethodVisitor(mv, name, desc, signature, isStatic, exceptions, maxLocals); }
From source file:net.nexustools.jvm.compiler.Compiler.java
License:Open Source License
public void compile(String rawClassname) throws IOException { if (processed.contains(rawClassname)) return;// ww w . jav a 2 s.co m processed.add(rawClassname); System.out.println("Resolving class " + rawClassname); final String classname, runtimeClassname = convertRuntime(rawClassname); for (String builtin : BUILT_IN) if (builtin.equals(runtimeClassname)) { usedbuiltins.add(builtin); return; // Skip } if (javaClass.matcher(rawClassname).find()) classname = "net/nexustools/jvm/runtime/" + rawClassname; else classname = rawClassname; File findFile = resolve(classname + ".class"); ClassReader reader; try { if (findFile.exists()) { if (!classname.equals(rawClassname)) { if (processed.contains(classname)) return; processed.add(classname); } reader = new ClassReader(new FileInputStream(findFile)); } else { throw new CompileError("No implementation found: " + classname); //reader = new ClassReader(rawClassname); //System.err.println("\tUsing system provided class impl"); } } catch (IOException ex) { throw new CompileError(ex); } int offset = outputFolder.getPath().length() + 1; File output = resolveOutput(findFile, runtimeClassname + ".js"); File parentFile = output.getParentFile(); if (!parentFile.isDirectory() && !parentFile.mkdirs()) throw new RuntimeException("Cannot create directory: " + parentFile); File nativeFile = resolve(classname + ".native.js"); if (nativeFile.exists()) { File outputResolvedPath = new File(parentFile, nativeFile.getName()); copy(new FileInputStream(nativeFile), new FileOutputStream(outputResolvedPath)); natives.add(outputResolvedPath.getPath().substring(offset)); } compiled.add(output.getPath().substring(offset)); final List<String> references = new ArrayList(); final Referencer referencer = new Referencer() { @Override public boolean add(String reference) { reference = convertRuntime(reference); if (references.contains(reference)) return false; references.add(reference); return true; } }; final SignatureConverter converter = new SignatureConverter() { @Override public String convert(String input) { String ref = input; while (ref.startsWith("[")) ref = ref.substring(1); if (ref.startsWith("L") && ref.endsWith(";")) referencer.add(ref.substring(1, ref.length() - 1)); else if (ref.length() > 1) referencer.add(ref); return convertSignature(input); } }; final OutputStreamWriter oSw = new OutputStreamWriter(new FileOutputStream(output)); final BufferedWriter bw = new BufferedWriter(oSw); try { bw.append("(function JVM_"); bw.append(runtimeClassname.replaceAll("\\W", "_")); bw.append("($JVM, JVM){\n\t$JVM.ClassLoader.defineClass(\""); bw.append(runtimeClassname); bw.append("\", ["); String[] interfaces = reader.getInterfaces(); references.addAll(Arrays.asList(interfaces)); for (int i = 0; i < interfaces.length; i++) { if (i > 0) bw.append(','); bw.append('\"'); bw.append(convertRuntime(interfaces[i])); bw.append('\"'); } bw.append("], "); String parent = reader.getSuperName(); if (parent != null && !references.contains(parent)) { bw.append('\"'); bw.append(convertRuntime(parent)); bw.append('\"'); references.add(parent); } else bw.append("null"); bw.append(", [\n"); final List<String> fields = new ArrayList(); final List<String> methods = new ArrayList(); System.out.println("\tVisiting class " + classname); final int[] methodAccess = new int[1]; final MethodVisitor methodVisitor = new MethodVisitor(Opcodes.ASM4) { @Override public void visitEnd() { try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"end\"\n"); bw.append("\t\t\t\t}\n"); bw.append("\t\t\t],\n"); writeAccess(methodAccess[0], bw); bw.append("\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { System.out.println( "\t\t\tvisitTryCatchBlock: " + start + ", " + end + ", " + handler + ", " + type); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"try\",\n"); bw.append("\t\t\t\t\t\"start\": \""); bw.append(start.toString()); bw.append("\",\n"); bw.append("\t\t\t\t\t\"end\": \""); bw.append(end.toString()); bw.append("\",\n"); bw.append("\t\t\t\t\t\"handler\": \""); bw.append(handler.toString()); bw.append("\",\n"); bw.append("\t\t\t\t\t\"catch\": \""); bw.append(type); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { System.out.println("\t\t\tvisitMethodInsn: " + nameForOpcode(opcode) + ", " + owner + ", " + name + ", " + desc + ", " + itf); /*if(name.equals("<init>") && desc.equals("()V") && runtimeClassname.equals("java/lang/Object")) { try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"initobject\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } return; }*/ try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"method\",\n"); bw.append("\t\t\t\t\t\"opcode\": JVM.Opcodes."); bw.append(nameForOpcode(opcode)); bw.append(",\n"); bw.append("\t\t\t\t\t\"owner\": "); bw.append(converter.convert(owner)); bw.append(",\n"); bw.append("\t\t\t\t\t\"name\": \""); bw.append(name); bw.append("\",\n"); bw.append("\t\t\t\t\t\"signature\": {\n"); bw.append("\t\t\t\t\t\t\"raw\": \""); bw.append(convertRuntime(desc)); bw.append("\",\n"); bw.append("\t\t\t\t\t\t\"return\": "); Matcher matcher = methodSignature.matcher(desc); if (!matcher.matches()) throw new IllegalArgumentException("Corrupt or invalid method signature: " + desc); bw.append(converter.convert(matcher.group(2))); bw.append(",\n"); String args = matcher.group(1); if (args != null) { bw.append("\t\t\t\t\t\t\"args\": [\n"); String[] argsl = splitArguments(args); /*matcher = classSignature.matcher(args); while(matcher.find()) argsl.add(converter.convert(matcher.group(1)));*/ for (int i = 0; i < argsl.length; i++) { bw.append("\t\t\t\t\t\t\t"); bw.append(converter.convert(argsl[i])); if (i < argsl.length - 1) bw.append(','); bw.append('\n'); } bw.append("\t\t\t\t\t\t]\n"); } else bw.append("\t\t\t\t\t\t\"args\": []\n"); bw.append("\t\t\t\t\t},\n"); bw.append("\t\t\t\t\t\"interface\": "); bw.append(itf ? "true" : "false"); bw.append("\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) { System.out.println("\t\t\tvisitTableSwitchInsn: " + min + ", " + max + ", " + dflt + ", " + Arrays.toString(labels)); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"tableSwitch\",\n"); bw.append("\t\t\t\t\t\"min\": \""); bw.append(String.valueOf(min)); bw.append("\",\n"); bw.append("\t\t\t\t\t\"max\": \""); bw.append(String.valueOf(max)); bw.append("\",\n"); bw.append("\t\t\t\t\t\"default\": \""); bw.append(dflt.toString()); bw.append("\",\n"); bw.append("\t\t\t\t\t\"jumps\": [\n"); for (int i = 0; i < labels.length; i++) { bw.append("\t\t\t\t\t\t\""); bw.append(labels[i].toString()); bw.append('"'); if (i < labels.length - 1) bw.append(','); bw.append('\n'); } bw.append("\t\t\t\t\t]\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitMultiANewArrayInsn(String desc, int dims) { System.out.println("\t\t\tvisitMultiANewArrayInsn: " + desc + ", " + dims); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"array\",\n"); bw.append("\t\t\t\t\t\"desc\": \""); bw.append(desc); bw.append("\",\n"); bw.append("\t\t\t\t\t\"size\": \""); bw.append(String.valueOf(dims)); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitIincInsn(int var, int increment) { System.out.println("\t\t\tvisitIincInsn: " + var + ", " + increment); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"iinc\",\n"); bw.append("\t\t\t\t\t\"index\": \""); bw.append(String.valueOf(var)); bw.append("\",\n"); bw.append("\t\t\t\t\t\"by\": \""); bw.append(String.valueOf(increment)); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) { System.out.println("\t\t\tvisitLookupSwitchInsn: " + dflt + ", " + Arrays.toString(keys) + ", " + Arrays.toString(labels)); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"switch\",\n"); if (dflt != null) { bw.append("\t\t\t\t\t\"default\": \""); bw.append(dflt.toString()); bw.append("\",\n"); } bw.append("\t\t\t\t\t\"keys\": [\n"); for (int i = 0; i < keys.length; i++) { bw.append("\t\t\t\t\t\t"); bw.append(String.valueOf(keys[i])); if (i < keys.length - 1) bw.append(','); bw.append('\n'); } bw.append("\t\t\t\t\t],\n"); bw.append("\t\t\t\t\t\"jumps\": [\n"); for (int i = 0; i < labels.length; i++) { bw.append("\t\t\t\t\t\t\""); bw.append(labels[i].toString()); bw.append('"'); if (i < labels.length - 1) bw.append(','); bw.append('\n'); } bw.append("\t\t\t\t\t]\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { System.out.println("\t\t\tvisitLocalVariable: " + name + ", " + desc + ", " + start + ", " + end + ", " + index); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"declare\",\n"); bw.append("\t\t\t\t\t\"name\": \""); bw.append(String.valueOf(name)); bw.append("\",\n"); bw.append("\t\t\t\t\t\"signature\": "); bw.append(converter.convert(desc)); bw.append(",\n"); bw.append("\t\t\t\t\t\"index\": \""); bw.append(String.valueOf(index)); bw.append("\",\n"); bw.append("\t\t\t\t\t\"start\": \""); bw.append(start.toString()); bw.append("\",\n"); bw.append("\t\t\t\t\t\"end\": \""); bw.append(end.toString()); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitParameter(String name, int access) { System.out.println("\t\t\tvisitParameter: " + name + ", " + access); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"arg\",\n"); bw.append("\t\t\t\t\t\"name\": \""); bw.append(name); bw.append("\",\n"); bw.append("\t\t\t\t\t\"access\": \""); bw.append(String.valueOf(access)); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitVarInsn(int opcode, int var) { System.out.println("\t\t\tvisitVarInsn: " + nameForOpcode(opcode) + ", " + var); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"var\",\n"); bw.append("\t\t\t\t\t\"opcode\": JVM.Opcodes."); bw.append(nameForOpcode(opcode)); bw.append(",\n"); bw.append("\t\t\t\t\t\"index\": \""); bw.append(String.valueOf(var)); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitTypeInsn(int opcode, String type) { System.out.println("\t\t\tvisitTypeInsn: " + nameForOpcode(opcode) + ", " + type); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"type\",\n"); bw.append("\t\t\t\t\t\"opcode\": JVM.Opcodes."); bw.append(nameForOpcode(opcode)); bw.append(",\n"); bw.append("\t\t\t\t\t\"signature\": "); bw.append(converter.convert(type)); bw.append('\n'); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitLdcInsn(Object cst) { System.out.println("\t\t\tvisitLdcInsn: " + cst); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"ldc\",\n"); if (cst instanceof String) { bw.append("\t\t\t\t\t\"stringValue\": "); bw.append(new Gson().toJson((String) cst)); bw.append("\n"); } else if (cst instanceof Number) { bw.append("\t\t\t\t\t\"numericValue\": "); bw.append(String.valueOf((Number) cst)); bw.append("\n"); } else if (cst instanceof org.objectweb.asm.Type) { org.objectweb.asm.Type type = (org.objectweb.asm.Type) cst; switch (type.getSort()) { case Type.OBJECT: System.out.println("OBJECT REFERENCE"); String ref = type.getInternalName(); bw.append("\t\t\t\t\t\"objectRef\": "); bw.append(converter.convert(ref)); bw.append("\n"); break; default: throw new UnsupportedOperationException("Cannot handle type: " + type.getSort()); } /*System.out.println("asm type: " + type.getInternalName()); System.out.println(type.getReturnType()); System.out.println(type.getDescriptor()); System.out.println(type.getElementType()); System.out.println(type.getDimensions()); System.out.println(type.getClassName()); throw new UnsupportedOperationException("Cannot handle types yet...");*/ } else throw new UnsupportedOperationException( "Unsupported type for LDC: " + cst.getClass().getName()); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) { throw new UnsupportedOperationException(); /*System.out.println("\t\t\tvisitInvokeDynamicInsn: " + name + ", " + desc + ", " + bsm + ", " + Arrays.toString(bsmArgs)); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"invokeDynamic\",\n"); bw.append("\t\t\t\t\t\"name\": \""); bw.append(name); bw.append("\",\n"); bw.append("\t\t\t\t\t\"signature\": \""); bw.append(desc); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); }*/ } @Override public void visitLabel(Label label) { System.out.println("\t\t\tvisitLabel: " + label.toString()); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"label\",\n"); bw.append("\t\t\t\t\t\"name\": \""); bw.append(label.toString()); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitJumpInsn(int opcode, Label label) { System.out.println("\t\t\tvisitJumpInsn: " + nameForOpcode(opcode) + ", " + label.toString()); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"jump\",\n"); bw.append("\t\t\t\t\t\"opcode\": JVM.Opcodes."); bw.append(nameForOpcode(opcode)); bw.append(",\n"); bw.append("\t\t\t\t\t\"name\": \""); bw.append(label.toString()); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitInsn(int opcode) { System.out.println("\t\t\tvisitInsn: " + nameForOpcode(opcode)); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"insn\",\n"); bw.append("\t\t\t\t\t\"opcode\": JVM.Opcodes."); bw.append(nameForOpcode(opcode)); bw.append("\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitIntInsn(int opcode, int operand) { System.out.println("\t\t\tvisitIntInsn: " + nameForOpcode(opcode) + ", " + operand); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"int\",\n"); bw.append("\t\t\t\t\t\"opcode\": JVM.Opcodes."); bw.append(nameForOpcode(opcode)); bw.append(",\n"); bw.append("\t\t\t\t\t\"operand\": \""); bw.append(String.valueOf(operand)); bw.append("\"\n"); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } @Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { System.out.println("\t\t\tvisitFieldInsn: " + nameForOpcode(opcode) + ", " + owner + ", " + name + ", " + desc); try { bw.append("\t\t\t\t{\n"); bw.append("\t\t\t\t\t\"type\": \"field\",\n"); bw.append("\t\t\t\t\t\"opcode\": JVM.Opcodes."); bw.append(nameForOpcode(opcode)); bw.append(",\n"); bw.append("\t\t\t\t\t\"class\": "); bw.append(converter.convert(owner)); bw.append(",\n"); bw.append("\t\t\t\t\t\"name\": \""); bw.append(name); bw.append("\",\n"); bw.append("\t\t\t\t\t\"signature\": "); bw.append(converter.convert(desc)); bw.append('\n'); bw.append("\t\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } }; final ClassOptimizer[] classOptimizer = new ClassOptimizer[1]; ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM4) { @Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { System.out.println("\t\tField: " + name + ", " + desc + ", " + value + ", " + access); if (!fields.contains(name)) { fields.add(name); } try { bw.append("\t\t{\n"); bw.append("\t\t\t\"type\": \"field\",\n"); bw.append("\t\t\t\"name\": \""); bw.append(name); bw.append("\",\n"); bw.append("\t\t\t\"signature\": "); bw.append(converter.convert(desc)); bw.append(",\n"); if (value instanceof String) { bw.append("\t\t\t\"stringValue\": \""); bw.append(((String) value).replace("\n", "\\n").replace("\"", "\\\"")); bw.append("\",\n"); } else if (value instanceof Number) { bw.append("\t\t\t\"numericValue\": "); bw.append(String.valueOf((Number) value)); bw.append(",\n"); } else if (value != null) throw new RuntimeException("Unhandled initial value: " + value); writeAccess(access, bw); bw.append("\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } //if(signature != null) { Matcher matcher = classSignature.matcher(desc); if (matcher.matches() && !references.contains(matcher.group(1))) references.add(matcher.group(1)); //} return super.visitField(access, name, desc, signature, value); } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { System.out.println("\t\tMethod: " + name + ", " + desc + ", " + signature + ", " + access + ", " + Arrays.toString(exceptions)); if (!methods.contains(name)) methods.add(name); try { bw.append("\t\t{\n"); bw.append("\t\t\t\"type\": \"method\",\n"); bw.append("\t\t\t\"name\": \""); bw.append(name); bw.append("\",\n"); bw.append("\t\t\t\"signature\": \""); bw.append(convertRuntime(desc)); bw.append("\",\n"); bw.append("\t\t\t\"sigparts\": {\n"); bw.append("\t\t\t\t\"return\": "); Matcher matcher = methodSignature.matcher(desc); if (!matcher.matches()) throw new IllegalArgumentException("Corrupt or invalid method signature: " + desc); bw.append(converter.convert(matcher.group(2))); bw.append(",\n"); String args = matcher.group(1); if (args != null) { bw.append("\t\t\t\t\"args\": [\n"); String[] argsl = splitArguments(args); /*matcher = classSignature.matcher(args); while(matcher.find()) argsl.add(converter.convert(matcher.group(1)));*/ for (int i = 0; i < argsl.length; i++) { bw.append("\t\t\t\t\t"); bw.append(converter.convert(argsl[i])); if (i < argsl.length - 1) bw.append(','); bw.append('\n'); } bw.append("\t\t\t\t]\n"); } else bw.append("\t\t\t\t\"args\": []\n"); bw.append("\t\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } if (exceptions != null) { try { bw.append("\t\t\t\"exceptions\": [\n"); } catch (IOException ex) { throw new RuntimeException(ex); } for (int i = 0; i < exceptions.length; i++) { String exception = exceptions[i]; if (!references.contains(exception)) references.add(exception); try { bw.append("\t\t\t\t\""); bw.append(convertRuntime(exception)); bw.append('"'); if (i < exceptions.length - 1) bw.append(','); bw.append('\n'); } catch (IOException ex) { throw new RuntimeException(ex); } } try { bw.append("\t\t\t],\n"); } catch (IOException ex) { throw new RuntimeException(ex); } } Matcher matcher = methodSignature.matcher(desc); matcher.matches(); String args = matcher.group(1); String ret = matcher.group(2); matcher = classSignature.matcher(ret); if (matcher.matches() && !references.contains(matcher.group(1))) references.add(matcher.group(1)); if (args != null) { matcher = classSignature.matcher(args); while (matcher.find()) if (!references.contains(matcher.group(1))) references.add(matcher.group(1)); } if ((access & Opcodes.ACC_NATIVE) != 0) { try { bw.append("\t\t\t\"implementation\": \""); bw.append(runtimeClassname + ".native.js"); bw.append("\",\n"); writeAccess(access, bw); bw.append("\t\t},\n"); } catch (IOException ex) { throw new RuntimeException(ex); } return super.visitMethod(access, name, desc, signature, exceptions); } try { bw.append("\t\t\t\"implementation\": [\n"); bw.flush(); } catch (IOException ex) { throw new RuntimeException(ex); } methodAccess[0] = access; //return new MethodOptimizer(classOptimizer[0], access, desc, methodVisitor, new Remapper() {}); return methodVisitor; } }; //classOptimizer[0] = new ClassOptimizer(classVisitor, new Remapper() {}); //reader.accept(classOptimizer[0], 0); reader.accept(classVisitor, 0); bw.append("\t\t{\n"); bw.append("\t\t\t\"type\": \"references\",\n"); bw.append("\t\t\t\"value\": [\n"); List<String> written = new ArrayList(); for (int i = 0; i < references.size(); i++) { String ref = convertRuntime(references.get(i)); if (written.contains(ref)) continue; written.add(ref); bw.append("\t\t\t\t\""); bw.append(ref); bw.append('"'); if (i < references.size() - 1) bw.append(','); bw.append('\n'); } bw.append("\t\t\t]\n"); bw.append("\t\t}\n"); bw.append("\t]);\n"); bw.append("})($currentJVM, JVM);"); } finally { bw.close(); } System.out.println("\tProcessing references: " + references); for (String ref : references) compile(ref); referenceMap.put(runtimeClassname, references); }
From source file:net.sf.maven.plugin.autotools.NativeMethodsFinder.java
License:Apache License
/** * Visits a method of the class./*from w w w . ja v a 2s . co m*/ */ public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if ((access & Opcodes.ACC_NATIVE) != 0) { nativeMethods = true; } return null; }