List of usage examples for org.objectweb.asm Opcodes ACC_PRIVATE
int ACC_PRIVATE
To view the source code for org.objectweb.asm Opcodes ACC_PRIVATE.
Click Source Link
From source file:com.bit.learning.java.asm.UDFByteCodeVerifier.java
License:Apache License
public Set<String> verify(byte[] bytes) { final Set<String> errors = new TreeSet<String>(); // it's a TreeSet for unit tests ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM5) { public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { errors.add("field declared: " + name); return null; }//from w w w. j a v a 2 s . co m public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if (true) { return new EvaluateVisitor(errors); } if ("<init>".equals(name)) { if (Opcodes.ACC_PUBLIC != access) errors.add("constructor not public"); // allowed constructor - JavaUDF(TypeCodec returnCodec, TypeCodec[] argCodecs) return new ConstructorVisitor(errors); } if ("<init>".equals(name) && CTOR_SIG.equals("()V")) { if (Opcodes.ACC_PUBLIC != access) errors.add("constructor not public"); // allowed constructor - JavaUDF(TypeCodec returnCodec, TypeCodec[] argCodecs) return new ConstructorVisitor(errors); } if ("evaluate".equals(name)) { if (Opcodes.ACC_PRIVATE == access) { //This should be right, because user can use private evaluate method errors.add("evaluate is private"); } return new EvaluateVisitor(errors); } if ("executeImpl".equals(name) && "(ILjava/util/List;)Ljava/nio/ByteBuffer;".equals(desc)) { if (Opcodes.ACC_PROTECTED != access) errors.add("executeImpl not protected"); // the executeImpl method - ByteBuffer executeImpl(int protocolVersion, List<ByteBuffer> params) return new ExecuteImplVisitor(errors); } if ("<clinit>".equals(name)) { errors.add("static initializer declared"); } else { // errors.add("not allowed method declared: " + name + desc); // return new ExecuteImplVisitor(errors); } return null; } public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { // if (!JAVA_UDF_NAME.equals(superName)) { // errors.add("class does not extend " + JavaUDF.class.getName()); // } // if (access != (Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER)) { // errors.add("class not public final"); // } super.visit(version, access, name, signature, superName, interfaces); } public void visitInnerClass(String name, String outerName, String innerName, int access) { errors.add("class declared as inner class"); super.visitInnerClass(name, outerName, innerName, access); } }; ClassReader classReader = new ClassReader(bytes); classReader.accept(classVisitor, ClassReader.SKIP_DEBUG); return errors; }
From source file:com.changingbits.Builder.java
License:Apache License
public LongRangeCounter getCounter2() { finish(false);//from w ww . ja va 2 s. c o m // Maps each range to the leaf counts that contribute to it: Map<Integer, List<Integer>> rangeToLeaf = new HashMap<>(); buildRangeToLeaf(root, new ArrayList<Integer>(), rangeToLeaf); StringBuilder sb = new StringBuilder(); sb.append('\n'); sb.append("public void add(long v) {\n"); int count = 0; for (LongRange range : ranges) { sb.append(" // range "); sb.append(count++); sb.append(": "); sb.append(range); sb.append('\n'); } buildJavaCounter2Source(root, 1, sb, false); sb.append("}\n\n"); sb.append("public int[] getCounts() {\n"); sb.append(" int[] counts = new int["); sb.append(ranges.length); sb.append("];\n"); for (int range = 0; range < ranges.length; range++) { List<Integer> elements = rangeToLeaf.get(range); if (elements != null) { sb.append(" counts["); sb.append(range); sb.append("] = count"); sb.append(elements.get(0)); for (int i = 1; i < elements.size(); i++) { sb.append(" + count"); sb.append(elements.get(i)); } sb.append(";\n"); } } sb.append(" return counts;\n}\n"); String javaSource = sb.toString(); //System.out.println("counter2 javaSource:\n" + javaSource); ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC, COMPILED_COUNTER_CLASS2.replace('.', '/'), null, LONG_RANGE_COUNTER_TYPE.getInternalName(), null); classWriter.visitSource(javaSource, null); // Define "int countN" members: int numLeaves = elementaryIntervals.size(); for (int i = 0; i < numLeaves; i++) { classWriter.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC, "count" + i, "I", null, null); } // init: Method m = Method.getMethod("void <init> ()"); GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, m, null, null, classWriter); // Init all counters to 0: for (int i = 0; i < numLeaves; i++) { constructor.loadThis(); constructor.push(0); constructor.putField(COMPILED_COUNTER_CLASS2_TYPE, "count" + i, Type.INT_TYPE); } constructor.loadThis(); constructor.invokeConstructor(LONG_RANGE_COUNTER_TYPE, m); constructor.returnValue(); constructor.endMethod(); // void add(long v): GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, ADD_METHOD, null, null, classWriter); buildCounterAsm2(gen, root, false); gen.returnValue(); gen.endMethod(); // int[] getCounts(): gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, GET_COUNTS_METHOD, null, null, classWriter); int countsLocal = gen.newLocal(INT_ARRAY_TYPE); gen.push(ranges.length); gen.newArray(Type.INT_TYPE); gen.storeLocal(countsLocal); for (int range = 0; range < ranges.length; range++) { List<Integer> elements = rangeToLeaf.get(range); if (elements != null) { gen.loadLocal(countsLocal); gen.push(range); gen.loadThis(); gen.getField(COMPILED_COUNTER_CLASS2_TYPE, "count" + elements.get(0), Type.INT_TYPE); for (int i = 1; i < elements.size(); i++) { gen.loadThis(); gen.getField(COMPILED_COUNTER_CLASS2_TYPE, "count" + elements.get(i), Type.INT_TYPE); gen.visitInsn(Opcodes.IADD); } gen.arrayStore(Type.INT_TYPE); } } gen.loadLocal(countsLocal); gen.returnValue(); gen.endMethod(); classWriter.visitEnd(); byte[] bytes = classWriter.toByteArray(); // javap -c /x/tmp/my.class /* try { FileOutputStream fos = new FileOutputStream(new File("/x/tmp/counter2.class")); fos.write(bytes); fos.close(); } catch (Exception e) { throw new RuntimeException(e); } */ // nocommit allow changing the class loader Class<? extends LongRangeCounter> cl = new CounterLoader(LongRangeCounter.class.getClassLoader()) .define(COMPILED_COUNTER_CLASS2, classWriter.toByteArray()); try { return cl.getConstructor().newInstance(); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new RuntimeException(e); } }
From source file:com.codename1.tools.ikvm.Parser.java
/** * Parses an InputStream containing a class. * @param input The input stream with a class. * @return If a transformation occurred, the bytes for the changed class will be returned. Otherwise null will be returned. * @throws Exception /* w w w . j a v a2 s .co m*/ */ public static byte[] parse(InputStream input, ClassLoader classLoader) throws Exception { ClassReader r = new ClassReader(input); Parser p = new Parser(); //ClassWriter w = new ClassWriter(r, 0); ClassNode classNode = new ClassNode(); //p.classNode = classNode; r.accept(classNode, 0); //r.accept(p, ClassReader.EXPAND_FRAMES) List<MethodNode> methodsToAdd = new ArrayList<MethodNode>(); int methodNum = 0; for (Object o : classNode.methods) { methodNum++; MethodNode methodNode = (MethodNode) o; boolean synchronizedMethod = (methodNode.access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED; if (synchronizedMethod) { // Check for a try statement final boolean[] tryCatchFound = new boolean[1]; //System.out.println("Found sync method "+methodNode.name+". Checking for try blocks"); methodNode.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitTryCatchBlock(Label label, Label label1, Label label2, String string) { tryCatchFound[0] = true; } }); if (!tryCatchFound[0]) { continue; } //System.out.println("Instructions: "+Arrays.toString(methodNode.instructions.toArray())); System.out.println("Transforming method " + methodNode.name + " of class " + classNode.name); MethodDescriptor md = new MethodDescriptor(methodNode.access, methodNode.name, methodNode.desc); //methodNode.access = methodNode.access & ~Opcodes.ACC_SYNCHRONIZED; String privateMethodName = (md.constructor ? "___cn1init__" : methodNode.name) + "___cn1sync" + (methodNum); MethodNode syncMethod = new MethodNode(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()])); methodNode.name = privateMethodName; methodNode.access = (methodNode.access | Opcodes.ACC_PRIVATE) & ~Opcodes.ACC_PUBLIC & ~Opcodes.ACC_PROTECTED & ~Opcodes.ACC_SYNCHRONIZED; LabelNode startLabel = new LabelNode(); syncMethod.instructions.add(startLabel); LabelNode endLabel = new LabelNode(); int argIndex = 0; if (!md.staticMethod) { //System.out.println(methodNode.name + " is not static"); syncMethod.localVariables.add(new LocalVariableNode("arg" + (argIndex), "L" + classNode.name + ";", null, startLabel, endLabel, argIndex)); syncMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, argIndex++)); } for (ByteCodeMethodArg arg : md.arguments) { char typeChar = arg.type; if (arg.dim > 0) { typeChar = 'L'; } if (arg.desc == null || arg.desc.isEmpty()) { throw new RuntimeException( "Invalid arg description for arg " + argIndex + " of method " + methodNode.name); } syncMethod.localVariables.add(new LocalVariableNode("arg" + (argIndex), arg.desc, arg.desc, startLabel, endLabel, argIndex)); switch (typeChar) { case 'L': syncMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, argIndex++)); //syncMethod.localVariables.add(new LocalVariableNode("arg"+(argIndex-1), arg.desc, null, startLabel, endLabel, argIndex-1)); break; case 'S': case 'I': case 'B': case 'Z': case 'C': syncMethod.instructions.add(new VarInsnNode(Opcodes.ILOAD, argIndex++)); break; case 'J': syncMethod.instructions.add(new VarInsnNode(Opcodes.LLOAD, argIndex++)); argIndex++; // arg index increments 2 for double size args break; case 'F': syncMethod.instructions.add(new VarInsnNode(Opcodes.FLOAD, argIndex++)); break; case 'D': syncMethod.instructions.add(new VarInsnNode(Opcodes.DLOAD, argIndex++)); argIndex++;// arg index increments 2 for double size args break; default: throw new IllegalArgumentException("Unsupported argument type " + arg.type); } } if (md.staticMethod) { syncMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, privateMethodName, methodNode.desc)); } else { syncMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.name, privateMethodName, methodNode.desc)); } if (md.returnType != null) { char typeChar = md.returnType.type; if (md.returnType.dim > 0) { typeChar = 'L'; } switch (typeChar) { case 'L': syncMethod.instructions.add(new InsnNode(Opcodes.ARETURN)); break; case 'S': case 'I': case 'B': case 'Z': case 'C': syncMethod.instructions.add(new InsnNode(Opcodes.IRETURN)); break; case 'J': syncMethod.instructions.add(new InsnNode(Opcodes.LRETURN)); break; case 'F': syncMethod.instructions.add(new InsnNode(Opcodes.FRETURN)); break; case 'D': syncMethod.instructions.add(new InsnNode(Opcodes.DRETURN)); break; case 'V': syncMethod.instructions.add(new InsnNode(Opcodes.RETURN)); break; default: throw new IllegalArgumentException("Unsupported argument type " + md.returnType.type); } } else { syncMethod.instructions.add(new InsnNode(Opcodes.DRETURN)); } syncMethod.instructions.add(endLabel); methodsToAdd.add(syncMethod); } } if (!methodsToAdd.isEmpty()) { changed = true; System.out .println("Transforming " + methodsToAdd.size() + " synchronized methods in " + classNode.name); classNode.methods.addAll(methodsToAdd); ClassWriter w = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(w); byte[] out = w.toByteArray(); if (verify) { verify(out, classLoader); } return out; } else { ClassWriter w = new ClassWriter(0); classNode.accept(w); byte[] out = w.toByteArray(); return out; } }
From source file:com.codename1.tools.translator.ByteCodeField.java
License:Open Source License
public ByteCodeField(String clsName, int access, String name, String desc, String signature, Object value) { this.clsName = clsName; this.value = value; privateField = (access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE; if (value != null && value instanceof String) { Parser.addToConstantPool((String) value); }/* w ww.ja v a2 s. co m*/ staticField = (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC; finalField = (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL; fieldName = name.replace('$', '_'); arrayDimensions = 0; while (desc.startsWith("[")) { desc = desc.substring(1); arrayDimensions++; } char currentType = desc.charAt(0); switch (currentType) { case 'L': // Object skip until ; int idx = desc.indexOf(';'); String objectType = desc.substring(1, idx); objectType = objectType.replace('/', '_').replace('$', '_'); if (!dependentClasses.contains(objectType)) { dependentClasses.add(objectType); } //if (!privateField && !exportedClasses.contains(objectType)) { // exportedClasses.add(objectType); //} type = objectType; break; case 'I': primitiveType = Integer.TYPE; break; case 'J': primitiveType = Long.TYPE; break; case 'B': primitiveType = Byte.TYPE; break; case 'S': primitiveType = Short.TYPE; break; case 'F': primitiveType = Float.TYPE; break; case 'D': primitiveType = Double.TYPE; break; case 'Z': primitiveType = Boolean.TYPE; break; case 'C': primitiveType = Character.TYPE; break; } }
From source file:com.codename1.tools.translator.BytecodeMethod.java
License:Open Source License
public BytecodeMethod(String clsName, int access, String name, String desc, String signature, String[] exceptions) {/*from ww w. j a v a2 s . co m*/ methodName = name; this.clsName = clsName; this.desc = desc; privateMethod = (access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE; nativeMethod = (access & Opcodes.ACC_NATIVE) == Opcodes.ACC_NATIVE; staticMethod = (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC; finalMethod = (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL; synchronizedMethod = (access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED; int pos = desc.lastIndexOf(')'); if (!staticMethod) { if (!dependentClasses.contains("java_lang_NullPointerException")) { dependentClasses.add("java_lang_NullPointerException"); } } // if (methodName.equals("<init>")) { methodName = "__INIT__"; constructor = true; returnType = new ByteCodeMethodArg(Void.TYPE, 0); } else { if (methodName.equals("<clinit>")) { methodName = "__CLINIT__"; returnType = new ByteCodeMethodArg(Void.TYPE, 0); staticMethod = true; } else { String retType = desc.substring(pos + 1); if (retType.equals("V")) { returnType = new ByteCodeMethodArg(Void.TYPE, 0); } else { int dim = 0; while (retType.startsWith("[")) { retType = retType.substring(1); dim++; } char currentType = retType.charAt(0); switch (currentType) { case 'L': // Object skip until ; int idx = retType.indexOf(';'); String objectType = retType.substring(1, idx); objectType = objectType.replace('/', '_').replace('$', '_'); if (!dependentClasses.contains(objectType)) { dependentClasses.add(objectType); } //if (!this.isPrivate() && !exportedClasses.contains(objectType)) { // exportedClasses.add(objectType); //} returnType = new ByteCodeMethodArg(objectType, dim); break; case 'I': returnType = new ByteCodeMethodArg(Integer.TYPE, dim); break; case 'J': returnType = new ByteCodeMethodArg(Long.TYPE, dim); break; case 'B': returnType = new ByteCodeMethodArg(Byte.TYPE, dim); break; case 'S': returnType = new ByteCodeMethodArg(Short.TYPE, dim); break; case 'F': returnType = new ByteCodeMethodArg(Float.TYPE, dim); break; case 'D': returnType = new ByteCodeMethodArg(Double.TYPE, dim); break; case 'Z': returnType = new ByteCodeMethodArg(Boolean.TYPE, dim); break; case 'C': returnType = new ByteCodeMethodArg(Character.TYPE, dim); break; } } } } int currentArrayDim = 0; desc = desc.substring(1, pos); for (int i = 0; i < desc.length(); i++) { char currentType = desc.charAt(i); switch (currentType) { case '[': // array of... currentArrayDim++; continue; case 'L': // Object skip until ; int idx = desc.indexOf(';', i); String objectType = desc.substring(i + 1, idx); objectType = objectType.replace('/', '_').replace('$', '_'); if (!dependentClasses.contains(objectType)) { dependentClasses.add(objectType); } //if (!this.isPrivate() && !exportedClasses.contains(objectType)) { // exportedClasses.contains(objectType); //} i = idx; arguments.add(new ByteCodeMethodArg(objectType, currentArrayDim)); break; case 'I': arguments.add(new ByteCodeMethodArg(Integer.TYPE, currentArrayDim)); break; case 'J': arguments.add(new ByteCodeMethodArg(Long.TYPE, currentArrayDim)); break; case 'B': arguments.add(new ByteCodeMethodArg(Byte.TYPE, currentArrayDim)); break; case 'S': arguments.add(new ByteCodeMethodArg(Short.TYPE, currentArrayDim)); break; case 'F': arguments.add(new ByteCodeMethodArg(Float.TYPE, currentArrayDim)); break; case 'D': arguments.add(new ByteCodeMethodArg(Double.TYPE, currentArrayDim)); break; case 'Z': arguments.add(new ByteCodeMethodArg(Boolean.TYPE, currentArrayDim)); break; case 'C': arguments.add(new ByteCodeMethodArg(Character.TYPE, currentArrayDim)); break; } currentArrayDim = 0; } }
From source file:com.facebook.buck.java.abi.ClassMirror.java
License:Apache License
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if ((access & Opcodes.ACC_PRIVATE) > 0) { return super.visitField(access, name, desc, signature, value); }/*from w w w . ja v a 2s .c o m*/ FieldMirror mirror = new FieldMirror(access, name, desc, signature, value); fields.add(mirror); return mirror; }
From source file:com.facebook.buck.java.abi.ClassMirror.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if ((access & Opcodes.ACC_PRIVATE) > 0) { return super.visitMethod(access, name, desc, signature, exceptions); }/*from w ww .j av a 2 s . c o m*/ // Bridge methods are created by the compiler, and don't appear in source. It would be nice to // skip them, but they're used by the compiler to cover the fact that type erasure has occurred. // Normally the compiler adds these as public methods, but if you're compiling against a stub // produced using our ABI generator, we don't want people calling it accidentally. Oh well, I // guess it happens IRL too. // // Synthetic methods are also generated by the compiler, unless it's one of the methods named in // section 4.7.8 of the JVM spec, which are "<init>" and "Enum.valueOf()" and "Enum.values". // None of these are actually harmful to the ABI, so we allow synthetic methods through. // http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.8 MethodMirror mirror = new MethodMirror(access, name, desc, signature, exceptions); methods.add(mirror); return mirror; }
From source file:com.facebook.buck.java.abi.ClassMirror.java
License:Apache License
@Override public void visitInnerClass(String name, String outerName, String innerName, int access) { if ((access & Opcodes.ACC_PRIVATE) > 0) { return;/*from w ww. ja v a2 s. co m*/ } innerClasses.add(new InnerClass(name, outerName, innerName, access)); super.visitInnerClass(name, outerName, innerName, access); }
From source file:com.facebook.buck.jvm.java.abi.AbiFilteringClassVisitor.java
License:Apache License
@Override public void visitEnd() { if (!hasVisibleConstructor && !isInterface(classAccess) && !isAnnotation(classAccess)) { String desc;/* w w w .jav a 2 s . co m*/ if (isEnum(classAccess)) { desc = Type.getMethodType(Type.VOID_TYPE, Type.getObjectType("java/lang/String"), Type.INT_TYPE) .getDescriptor(); } else { desc = outerName == null ? Type.getMethodType(Type.VOID_TYPE).getDescriptor() : Type.getMethodType(Type.VOID_TYPE, Type.getObjectType(outerName)).getDescriptor(); } super.visitMethod(Opcodes.ACC_PRIVATE, "<init>", desc, null, null); } super.visitEnd(); }
From source file:com.facebook.buck.jvm.java.abi.AbiFilteringClassVisitor.java
License:Apache License
private boolean shouldInclude(int access) { if ((access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) { return false; }//from w w w .j a v a2 s .c om if ((access & (Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE)) == Opcodes.ACC_SYNTHETIC) { return false; } return true; }