List of usage examples for org.objectweb.asm Opcodes ACC_PROTECTED
int ACC_PROTECTED
To view the source code for org.objectweb.asm Opcodes ACC_PROTECTED.
Click Source Link
From source file:com.android.tools.layoutlib.create.TransformClassAdapter.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if (mDeleteReturns != null) { Type t = Type.getReturnType(desc); if (t.getSort() == Type.OBJECT) { String returnType = t.getInternalName(); if (returnType != null) { if (mDeleteReturns.contains(returnType)) { return null; }/* w ww.j a v a 2 s . c o m*/ } } } String methodSignature = mClassName.replace('/', '.') + "#" + name; // change access to public access &= ~(Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE); access |= Opcodes.ACC_PUBLIC; // remove final access = access & ~Opcodes.ACC_FINAL; // stub this method if they are all to be stubbed or if it is a native method // and don't try to stub interfaces nor abstract non-native methods. if (!mIsInterface && ((access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) != Opcodes.ACC_ABSTRACT) && (mStubAll || (access & Opcodes.ACC_NATIVE) != 0) || mStubMethods.contains(methodSignature)) { boolean isStatic = (access & Opcodes.ACC_STATIC) != 0; boolean isNative = (access & Opcodes.ACC_NATIVE) != 0; // remove abstract, final and native access = access & ~(Opcodes.ACC_ABSTRACT | Opcodes.ACC_FINAL | Opcodes.ACC_NATIVE); String invokeSignature = methodSignature + desc; mLog.debug(" Stub: %s (%s)", invokeSignature, isNative ? "native" : ""); MethodVisitor mw = super.visitMethod(access, name, desc, signature, exceptions); return new StubMethodAdapter(mw, name, returnType(desc), invokeSignature, isStatic, isNative); } else { mLog.debug(" Keep: %s %s", name, desc); return super.visitMethod(access, name, desc, signature, exceptions); } }
From source file:com.android.tools.layoutlib.create.TransformClassAdapter.java
License:Apache License
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { // change access to public access &= ~(Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE); access |= Opcodes.ACC_PUBLIC;//from w ww. ja v a 2s . co m return super.visitField(access, name, desc, signature, value); }
From source file:com.appfour.codestripper.Main.java
License:Open Source License
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: java " + Main.class.getCanonicalName() + " <source dir> <dest file>"); System.exit(1);/*from w ww .j av a 2 s.c o m*/ } File outFile = new File(args[1]); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outFile)); zipOut.setLevel(9); Set<String> writtenEntries = new HashSet<String>(); for (File file : new File(args[0]).listFiles()) { if (file.getName().toLowerCase(Locale.US).endsWith(".jar")) { System.err.println("Processing JAR: " + file.getPath()); ZipInputStream zipIn = new ZipInputStream(new FileInputStream(file)); ZipEntry entry; while ((entry = zipIn.getNextEntry()) != null) { if (entry.isDirectory()) continue; if (entry.getName().toLowerCase(Locale.US).endsWith(".class")) { if (writtenEntries.contains(entry.getName())) { System.out.println("\tIgnoring duplicate CLASS: " + entry.getName()); continue; } writtenEntries.add(entry.getName()); System.out.println("\tProcessing CLASS: " + entry.getName()); ClassReader cr = new ClassReader(zipIn); ClassWriter cw = new ClassWriter(0); final boolean[] skip = new boolean[1]; ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) { @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { if (!INCLUDE_PACKAGE_PRIVATE_CLASSES && (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) { skip[0] = true; } super.visit(version, access, name, signature, superName, interfaces); } @Override public void visitSource(String source, String debug) { // ignore } @Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) return null; return super.visitField(access, name, desc, signature, value); } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) return null; return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) { @Override public void visitCode() { // ignore super.visitCode(); super.visitInsn(Opcodes.NOP); } @Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { // ignore } @Override public void visitIincInsn(int var, int increment) { // ignore } @Override public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { // ignore } @Override public void visitInsn(int opcode) { // ignore } @Override public void visitJumpInsn(int opcode, Label label) { // ignore } @Override public void visitLabel(Label label) { // ignore } @Override public void visitLdcInsn(Object cst) { // ignore } @Override public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) { // ignore } @Override public void visitIntInsn(int opcode, int operand) { // ignore } @Override public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { // ignore return null; } @Override public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) { // ignore } @Override public void visitMethodInsn(int opcode, String owner, String name, String desc) { // ignore } @Override public void visitMultiANewArrayInsn(String desc, int dims) { // ignore } @Override public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) { // ignore } @Override public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { // ignore return null; }; @Override public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { // ignore } @Override public void visitLineNumber(int line, Label start) { // ignore } @Override public AnnotationVisitor visitLocalVariableAnnotation(int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String desc, boolean visible) { // ignore return null; } @Override public void visitParameter(String name, int access) { // ignore } @Override public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) { // ignore return null; } @Override public void visitTypeInsn(int opcode, String type) { // ignore } @Override public void visitVarInsn(int opcode, int var) { // ignore } }; } @Override public void visitAttribute(Attribute attr) { System.out.println("\t\tProcessing attr " + attr.type); if (attr.isCodeAttribute()) return; super.visitAttribute(attr); } }; cr.accept(cv, 0); if (!skip[0]) { byte[] b2 = cw.toByteArray(); // b2 represents the same class as b1 entry.setSize(b2.length); entry.setCompressedSize(-1); entry.setMethod(ZipEntry.DEFLATED); zipOut.putNextEntry(entry); new DataOutputStream(zipOut).write(b2); } } else { if (writtenEntries.contains(entry.getName())) { System.out.println("\tIgnoring duplicate RESOURCE: " + entry.getName()); continue; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); transfer(zipIn, bos, false); writtenEntries.add(entry.getName()); System.out.println("\tProcessing RESOURCE: " + entry.getName()); entry.setSize(bos.size()); entry.setCompressedSize(-1); entry.setMethod(ZipEntry.DEFLATED); zipOut.putNextEntry(entry); transfer(new ByteArrayInputStream(bos.toByteArray()), zipOut, false); } } zipIn.close(); } } zipOut.close(); }
From source file:com.asakusafw.dag.compiler.builtin.MasterJoinLikeOperatorGenerator.java
License:Apache License
private static void defineSelection(Context context, ClassWriter writer, UserOperator operator, FieldRef impl, Map<OperatorProperty, FieldRef> dependencies) { Method selector = Invariants.safe(() -> { return MasterJoinOperatorUtil.getSelection(context.getClassLoader(), operator); });// ww w. j a v a 2 s . c o m if (selector == null) { return; } MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL, "selectMaster", Type.getMethodDescriptor(typeOf(Object.class), typeOf(List.class), typeOf(Object.class)), null, null); cast(method, 2, MasterJoinOperatorUtil.getTransactionInput(operator).getDataType()); List<ValueRef> arguments = new ArrayList<>(); impl.load(method); arguments.add(new LocalVarRef(Opcodes.ALOAD, 1)); arguments.add(new LocalVarRef(Opcodes.ALOAD, 2)); arguments .addAll(Lang.project(getExtraViews(operator), v -> Invariants.requireNonNull(dependencies.get(v)))); arguments .addAll(Lang.project(operator.getArguments(), v -> Invariants.requireNonNull(dependencies.get(v)))); for (int i = 0, n = selector.getParameterCount(); i < n; i++) { arguments.get(i).load(method); } method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(selector.getDeclaringClass()).getInternalName(), selector.getName(), Type.getMethodDescriptor(selector), false); method.visitInsn(Opcodes.ARETURN); method.visitMaxs(0, 0); method.visitEnd(); }
From source file:com.asakusafw.dag.compiler.builtin.MasterJoinLikeOperatorGenerator.java
License:Apache License
private void defineProcess(Context context, ClassWriter writer, UserOperator operator, FieldRef impl, Map<OperatorProperty, FieldRef> dependencies, ClassDescription target) { MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL, "process", Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class), typeOf(Object.class)), null, null); cast(method, 1, MasterJoinOperatorUtil.getMasterInput(operator).getDataType()); cast(method, 2, MasterJoinOperatorUtil.getTransactionInput(operator).getDataType()); defineProcess(method, context, operator, new LocalVarRef(Opcodes.ALOAD, 1), new LocalVarRef(Opcodes.ALOAD, 2), impl, dependencies, target); method.visitInsn(Opcodes.RETURN);// www. ja va 2s .co m method.visitMaxs(0, 0); method.visitEnd(); }
From source file:com.asakusafw.dag.compiler.codegen.CleanupStageClientGenerator.java
License:Apache License
private static void defineString(ClassWriter writer, String name, String value) { MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED, name, Type.getMethodDescriptor(typeOf(String.class)), null, new String[0]); getConst(method, value);//from www .j ava 2 s . c o m method.visitInsn(Opcodes.ARETURN); method.visitMaxs(0, 0); method.visitEnd(); }
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 ww .ja v a 2s .com 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.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 /*from www . j a v a 2s. c o 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.devexperts.usages.ClassUsagesAnalyzer.java
License:Open Source License
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { Member usedFrom = cache.resolveMember(className, name); markTypeUse(Type.getType(desc), usedFrom, UseKind.FIELD); markSignatureUse(signature, usedFrom); if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0 && ((access & Opcodes.ACC_STATIC) == 0)) usages.getUsagesForClass(className).addInheritableMember(name); return null;/*w ww. j av a 2 s . c o m*/ }
From source file:com.devexperts.usages.ClassUsagesAnalyzer.java
License:Open Source License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { String methodMemberName = Member.methodMemberName(name, Type.getType(desc)); Member usedFrom = cache.resolveMember(className, methodMemberName); markTypeUse(Type.getType(desc), usedFrom, UseKind.UNKNOWN); // will be replaced by RETURN/ARGUMENT markSignatureUse(signature, usedFrom); if (exceptions != null) for (String ex : exceptions) makeTypeUse(toClassName(ex), usedFrom, UseKind.THROW); if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0 && ((access & Opcodes.ACC_STATIC) == 0) && !name.equals(INIT_METHOD)) usages.getUsagesForClass(className).addInheritableMember(methodMemberName); return new MethodAnalyzer(usedFrom); }