List of usage examples for org.objectweb.asm Opcodes V1_7
int V1_7
To view the source code for org.objectweb.asm Opcodes V1_7.
Click Source Link
From source file:br.usp.each.saeg.badua.core.internal.ContentTypeDetector.java
License:Open Source License
private static int determineType(final InputStream in) throws IOException { final int header = readInt(in); switch (header) { case ZIPFILE: return ZIPFILE; case PACK200FILE: return PACK200FILE; case CLASSFILE: // also verify version to distinguish from Mach Object files: switch (readInt(in)) { case Opcodes.V1_1: case Opcodes.V1_2: case Opcodes.V1_3: case Opcodes.V1_4: case Opcodes.V1_5: case Opcodes.V1_6: case Opcodes.V1_7: case Opcodes.V1_8: return CLASSFILE; }//from w w w . ja va 2 s .c o m } if ((header & 0xffff0000) == GZFILE) { return GZFILE; } return UNKNOWN; }
From source file:co.cask.cdap.internal.app.runtime.batch.distributed.ContainerLauncherGenerator.java
License:Apache License
/** * Generates the bytecode for a main class and writes to the given {@link JarOutputStream}. * The generated class looks like this://from ww w .java2 s . c om * * <pre>{@code * class className { * public static void main(String[] args) { * MapReduceContainerLauncher.launch(launcherClassPath, classLoaderName, className, args); * } * } * } * </pre> * * The {@code launcherClassPath}, {@code classLoaderName} and {@code className} are represented as * string literals in the generated class. */ private static void generateLauncherClass(String launcherClassPath, String classLoaderName, String className, JarOutputStream output) throws IOException { String internalName = className.replace('.', '/'); ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES); classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, internalName, null, Type.getInternalName(Object.class), null); Method constructor = Methods.getMethod(void.class, "<init>"); // Constructor // MRAppMaster() GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, classWriter); mg.loadThis(); mg.invokeConstructor(Type.getType(Object.class), constructor); mg.returnValue(); mg.endMethod(); // Main method. // public static void main(String[] args) { // MapReduceContainerLauncher.launch(launcherClassPath, classLoaderName, className, args); // } Method mainMethod = Methods.getMethod(void.class, "main", String[].class); mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, mainMethod, null, new Type[] { Type.getType(Exception.class) }, classWriter); mg.getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class)); mg.visitLdcInsn("Launch class " + className); mg.invokeVirtual(Type.getType(PrintStream.class), Methods.getMethod(void.class, "println", String.class)); // The Launcher classpath, classloader name and main classname are stored as string literal in the generated class mg.visitLdcInsn(launcherClassPath); mg.visitLdcInsn(classLoaderName); mg.visitLdcInsn(className); mg.loadArg(0); mg.invokeStatic(Type.getType(MapReduceContainerLauncher.class), Methods.getMethod(void.class, "launch", String.class, String.class, String.class, String[].class)); mg.returnValue(); mg.endMethod(); classWriter.visitEnd(); output.putNextEntry(new JarEntry(internalName + ".class")); output.write(classWriter.toByteArray()); }
From source file:co.cask.cdap.internal.app.runtime.batch.distributed.ContainerLauncherGenerator.java
License:Apache License
/** * Generates a class that has a static main method which delegates the call to a static method in the given delegator * class with method signature {@code public static void launch(String className, String[] args)} * * @param className the classname of the generated class * @param mainDelegator the class to delegate the main call to * @param output for writing the generated bytes *///from ww w . ja v a 2 s . c o m private static void generateMainClass(String className, Type mainDelegator, JarOutputStream output) throws IOException { String internalName = className.replace('.', '/'); ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES); classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, internalName, null, Type.getInternalName(Object.class), null); // Generate the default constructor, which just call super() Method constructor = Methods.getMethod(void.class, "<init>"); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, classWriter); mg.loadThis(); mg.invokeConstructor(Type.getType(Object.class), constructor); mg.returnValue(); mg.endMethod(); // Generate the main method // public static void main(String[] args) { // System.out.println("Launch class ....."); // <MainDelegator>.launch(<className>, args); // } Method mainMethod = Methods.getMethod(void.class, "main", String[].class); mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, mainMethod, null, new Type[] { Type.getType(Exception.class) }, classWriter); mg.getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class)); mg.visitLdcInsn("Launch class " + className + " by calling " + mainDelegator.getClassName() + ".launch"); mg.invokeVirtual(Type.getType(PrintStream.class), Methods.getMethod(void.class, "println", String.class)); // The main classname is stored as string literal in the generated class mg.visitLdcInsn(className); mg.loadArg(0); mg.invokeStatic(mainDelegator, Methods.getMethod(void.class, "launch", String.class, String[].class)); mg.returnValue(); mg.endMethod(); classWriter.visitEnd(); output.putNextEntry(new JarEntry(internalName + ".class")); output.write(classWriter.toByteArray()); }
From source file:com.android.build.gradle.shrinker.parser.ProguardFlags.java
License:Apache License
public void target(@NonNull String target) { int version;/*from w ww . j av a 2 s .c o m*/ switch (target) { case "8": case "1.8": version = Opcodes.V1_8; break; case "7": case "1.7": version = Opcodes.V1_7; break; case "6": case "1.6": version = Opcodes.V1_6; break; case "5": case "1.5": version = Opcodes.V1_5; break; case "1.4": version = Opcodes.V1_4; break; case "1.3": version = Opcodes.V1_3; break; case "1.2": version = Opcodes.V1_2; break; case "1.1": version = Opcodes.V1_1; break; default: throw new AssertionError("Unknown target " + target); } this.bytecodeVersion = new BytecodeVersion(version); }
From source file:com.changingbits.Builder.java
License:Apache License
/** Build a {@link LongRangeMultiSet} implementation to * lookup intervals for a given point./*w w w .j a va2 s . c o m*/ * * @param useAsm If true, the tree will be compiled to * java bytecodes using the {@code asm} library; typically * this results in a faster (~3X) implementation. */ public LongRangeMultiSet getMultiSet(boolean useAsm, boolean useArrayImpl) { finish(useArrayImpl); if (useAsm) { StringBuilder sb = new StringBuilder(); sb.append('\n'); int count = 0; for (LongRange range : ranges) { sb.append("// range "); sb.append(count++); sb.append(": "); sb.append(range); sb.append('\n'); } sb.append('\n'); sb.append("int upto = 0;\n"); buildJavaSource(root, 0, sb); String javaSource = sb.toString(); //System.out.println("java: " + 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_TREE_CLASS.replace('.', '/'), null, LONG_RANGE_MULTI_SET_TYPE.getInternalName(), null); classWriter.visitSource(javaSource, null); Method m = Method.getMethod("void <init> ()"); GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, m, null, null, classWriter); constructor.loadThis(); constructor.loadArgs(); constructor.invokeConstructor(LONG_RANGE_MULTI_SET_TYPE, m); constructor.returnValue(); constructor.endMethod(); GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, LOOKUP_METHOD, null, null, classWriter); //Label labelTop = new Label(); //Label labelEnd = new Label(); //gen.visitLabel(labelTop); int uptoLocal = gen.newLocal(Type.INT_TYPE); //System.out.println("uptoLocal=" + uptoLocal); // nocommit is this not needed!? //gen.visitLocalVariable("upto", "I", null, labelTop, labelEnd, uptoLocal); gen.push(0); gen.storeLocal(uptoLocal, Type.INT_TYPE); buildAsm(gen, root, uptoLocal); // Return upto: gen.loadLocal(uptoLocal, Type.INT_TYPE); gen.returnValue(); //gen.visitLabel(labelEnd); gen.endMethod(); classWriter.visitEnd(); byte[] bytes = classWriter.toByteArray(); // javap -c /x/tmp/my.class /* try { FileOutputStream fos = new FileOutputStream(new File("/x/tmp/my.class")); fos.write(bytes); fos.close(); } catch (Exception e) { throw new RuntimeException(e); } */ // nocommit allow changing the class loader Class<? extends LongRangeMultiSet> treeClass = new Loader(LongRangeMultiSet.class.getClassLoader()) .define(COMPILED_TREE_CLASS, classWriter.toByteArray()); try { return treeClass.getConstructor().newInstance(); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new RuntimeException(e); } } else if (useArrayImpl) { return new ArrayLongRangeMultiSet(root); } else { return new SimpleLongRangeMultiSet(root); } }
From source file:com.changingbits.Builder.java
License:Apache License
public LongRangeCounter getCounter(boolean useAsm) { finish(false);/*from w w w. ja v a2s . co m*/ if (useAsm) { StringBuilder sb = new StringBuilder(); sb.append('\n'); int count = 0; for (LongRange range : ranges) { sb.append("// range "); sb.append(count++); sb.append(": "); sb.append(range); sb.append('\n'); } sb.append('\n'); buildJavaCounterSource(root, 0, sb, false); String javaSource = sb.toString(); //System.out.println("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_CLASS.replace('.', '/'), null, BASE_LONG_RANGE_COUNTER_TYPE.getInternalName(), null); classWriter.visitSource(javaSource, null); Method m = Method.getMethod("void <init> (com.changingbits.Node, int, int)"); GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, m, null, null, classWriter); constructor.loadThis(); constructor.loadArgs(); constructor.invokeConstructor(Type.getType(BaseLongRangeCounter.class), m); constructor.returnValue(); constructor.endMethod(); GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, ADD_METHOD, null, null, classWriter); buildCounterAsm(gen, root, false); 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/counter.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_CLASS, classWriter.toByteArray()); try { return cl.getConstructor(Node.class, int.class, int.class).newInstance(root, elementaryIntervals.size(), ranges.length); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new RuntimeException(e); } } else { return new SimpleLongRangeCounter(root, elementaryIntervals, ranges.length); } }
From source file:com.changingbits.Builder.java
License:Apache License
public LongRangeCounter getCounter2() { finish(false);/*from w ww .j a v a 2s. 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.facebook.buck.jvm.java.abi.ClassVisitorDriverFromElement.java
License:Apache License
/** Gets the class file version corresponding to the given source version constant. */ private static int sourceVersionToClassFileVersion(SourceVersion version) { switch (version) { case RELEASE_0: return Opcodes.V1_1; // JVMS8 4.1: 1.0 and 1.1 both support version 45.3 (Opcodes.V1_1) case RELEASE_1: return Opcodes.V1_1; case RELEASE_2: return Opcodes.V1_2; case RELEASE_3: return Opcodes.V1_3; case RELEASE_4: return Opcodes.V1_4; case RELEASE_5: return Opcodes.V1_5; case RELEASE_6: return Opcodes.V1_6; case RELEASE_7: return Opcodes.V1_7; case RELEASE_8: return Opcodes.V1_8; default:// w w w . j ava 2 s.co m throw new IllegalArgumentException(String.format("Unexpected source version: %s", version)); } }
From source file:com.facebook.buck.jvm.java.abi.SourceVersionUtils.java
License:Apache License
/** Gets the class file version corresponding to the given source version constant. */ public static int sourceVersionToClassFileVersion(SourceVersion version) { switch (version) { case RELEASE_0: return Opcodes.V1_1; // JVMS8 4.1: 1.0 and 1.1 both support version 45.3 (Opcodes.V1_1) case RELEASE_1: return Opcodes.V1_1; case RELEASE_2: return Opcodes.V1_2; case RELEASE_3: return Opcodes.V1_3; case RELEASE_4: return Opcodes.V1_4; case RELEASE_5: return Opcodes.V1_5; case RELEASE_6: return Opcodes.V1_6; case RELEASE_7: return Opcodes.V1_7; case RELEASE_8: return Opcodes.V1_8; default:/*from ww w . j a va2s. c o m*/ throw new IllegalArgumentException(String.format("Unexpected source version: %s", version)); } }
From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java
License:Open Source License
/** * Creates a generator for the bytecode for the adapter for the specified superclass and interfaces. * @param superClass the superclass the adapter will extend. * @param interfaces the interfaces the adapter will implement. * @param commonLoader the class loader that can see all of superClass, interfaces, and Nashorn classes. * @param classOverride true to generate the bytecode for the adapter that has class-level overrides, false to * generate the bytecode for the adapter that has instance-level overrides. * @throws AdaptationException if the adapter can not be generated for some reason. *///from ww w. j a v a2 s . c o m JavaAdapterBytecodeGenerator(final Class<?> superClass, final List<Class<?>> interfaces, final ClassLoader commonLoader, final boolean classOverride) throws AdaptationException { assert superClass != null && !superClass.isInterface(); assert interfaces != null; this.superClass = superClass; this.interfaces = interfaces; this.classOverride = classOverride; this.commonLoader = commonLoader; cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS) { @Override protected String getCommonSuperClass(final String type1, final String type2) { // We need to override ClassWriter.getCommonSuperClass to use this factory's commonLoader as a class // loader to find the common superclass of two types when needed. return JavaAdapterBytecodeGenerator.this.getCommonSuperClass(type1, type2); } }; superClassName = Type.getInternalName(superClass); generatedClassName = getGeneratedClassName(superClass, interfaces); cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER, generatedClassName, null, superClassName, getInternalTypeNames(interfaces)); generateGlobalFields(); gatherMethods(superClass); gatherMethods(interfaces); samName = abstractMethodNames.size() == 1 ? abstractMethodNames.iterator().next() : null; generateHandleFields(); generateConverterFields(); if (classOverride) { generateClassInit(); } generateConstructors(); generateMethods(); generateSuperMethods(); if (hasExplicitFinalizer) { generateFinalizerMethods(); } // } cw.visitEnd(); }