本文整理汇总了Java中jdk.internal.org.objectweb.asm.ClassWriter.visit方法的典型用法代码示例。如果您正苦于以下问题:Java ClassWriter.visit方法的具体用法?Java ClassWriter.visit怎么用?Java ClassWriter.visit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jdk.internal.org.objectweb.asm.ClassWriter
的用法示例。
在下文中一共展示了ClassWriter.visit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateClass
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
* Generates a class file with the given class name
*/
byte[] generateClass(String className) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
+ ClassWriter.COMPUTE_FRAMES);
cw.visit(V1_9,
ACC_PUBLIC + ACC_SUPER,
className.replace(".", "/"),
null,
"java/lang/Object",
null);
// <init>
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}
示例2: getClassFile
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
static byte[] getClassFile(String suffix) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
cw.visit(52, ACC_PUBLIC | ACC_SUPER, CLASS_NAME + suffix, null, "java/lang/Object", null);
{
mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, METHOD_NAME, TYPE.toMethodDescriptorString(), null, null);
mv.visitCode();
Handle bsm = new Handle(H_INVOKESTATIC,
CallSiteDepContextTest.class.getName().replace(".", "/"),
"bootstrap",
bsmMH.type().toMethodDescriptorString());
mv.visitInvokeDynamicInsn("methodName", TYPE.toMethodDescriptorString(), bsm);
mv.visitInsn(IRETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
示例3: badHostClass
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
static public void badHostClass(Class<?> hostClass) {
// choose a class name in the same package as the host class
String className;
if (hostClass != null) {
String prefix = packageName(hostClass);
if (prefix.length() > 0)
prefix = prefix.replace('.', '/') + "/";
className = prefix + "Anon";
} else {
className = "Anon";
}
// create the class
String superName = "java/lang/Object";
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
+ ClassWriter.COMPUTE_FRAMES);
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
className, null, superName, null);
byte[] classBytes = cw.toByteArray();
int cpPoolSize = constantPoolSize(classBytes);
Class<?> anonClass
= unsafe.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);
}
示例4: verifyASM
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
static void verifyASM() throws Exception {
ClassWriter cw = new ClassWriter(0);
cw.visit(V1_8, ACC_PUBLIC, "X", null, "java/lang/Object", null);
MethodVisitor mv = cw.visitMethod(ACC_STATIC, "foo",
"()V", null, null);
mv.visitMaxs(2, 1);
mv.visitMethodInsn(INVOKESTATIC,
"java/util/function/Function.class",
"identity", "()Ljava/util/function/Function;", true);
mv.visitInsn(RETURN);
cw.visitEnd();
byte[] carray = cw.toByteArray();
// for debugging
// write((new File("X.class")).toPath(), carray, CREATE, TRUNCATE_EXISTING);
// verify using javap/classfile reader
ClassFile cf = ClassFile.read(new ByteArrayInputStream(carray));
int mcount = checkMethod(cf, "foo");
if (mcount < 1) {
throw new RuntimeException("unexpected method count, expected 1" +
"but got " + mcount);
}
}
示例5: writeClassFile
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static void writeClassFile() throws Exception {
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(99, ACC_PUBLIC + ACC_SUPER, "ClassFile", null, "java/lang/Object", null);
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
cw.visitEnd();
try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
fos.write(cw.toByteArray());
}
}
示例6: createClass
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static Class<?> createClass() {
try {
ClassWriter cw = new ClassWriter(0);
cw.visit(Opcodes.V1_8,
Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
UNNAMED,
null,
OBJECT,
null);
cw.visitSource(UNNAMED, null);
cw.visitEnd();
byte[] bytes = cw.toByteArray();
ClassLoader loader = new ClassLoader(null) {
@Override
protected Class<?> findClass(String cn) throws ClassNotFoundException {
if (cn.equals(UNNAMED))
return super.defineClass(UNNAMED, bytes, 0, bytes.length);
throw new ClassNotFoundException(cn);
}
};
return loader.loadClass(UNNAMED);
} catch (Exception e) {
throw new InternalError(e);
}
}
示例7: loadClassData
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private byte[] loadClassData(String name) throws Exception {
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
switch (name) {
case INTERFACE_NAME:
cw.visit(V1_8, ACC_ABSTRACT | ACC_INTERFACE | ACC_PUBLIC, INTERFACE_NAME, null, "java/lang/Object", null);
{
mv = cw.visitMethod(ACC_PRIVATE, "privInstance", "()I", null, null);
mv.visitCode();
mv.visitLdcInsn(EXPECTED);
mv.visitInsn(IRETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
break;
case CLASS_NAME:
cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME, null, "java/lang/Object", new String[]{INTERFACE_NAME});
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
break;
default:
break;
}
cw.visitEnd();
return cw.toByteArray();
}
示例8: dumpT1
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] dumpT1() {
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T1", null, "java/lang/Object", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
mv.visitCode();
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("p1/T1.m()");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);
mv.visitIntInsn(BIPUSH, 3);
mv.visitInsn(IRETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
示例9: generateTestClass
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static byte[] generateTestClass() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(TestOSRWithNonEmptyStack.CLASS_FILE_VERSION, ACC_PUBLIC,
TestOSRWithNonEmptyStack.CLASS_NAME, null, "java/lang/Object",
null);
TestOSRWithNonEmptyStack.generateConstructor(cw);
TestOSRWithNonEmptyStack.generateTestMethod(cw);
cw.visitEnd();
return cw.toByteArray();
}
示例10: generateInvokerTemplate
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/** Produces byte code for a class that is used as an injected invoker. */
private static byte[] generateInvokerTemplate() {
ClassWriter cw = new ClassWriter(0);
// private static class InjectedInvoker {
// @Hidden
// static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
// return vamh.invokeExact(args);
// }
// }
cw.visit(52, ACC_PRIVATE | ACC_SUPER, "InjectedInvoker", null, "java/lang/Object", null);
MethodVisitor mv = cw.visitMethod(ACC_STATIC, "invoke_V",
"(Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
null, null);
// Suppress invoker method in stack traces.
AnnotationVisitor av0 = mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true);
av0.visitEnd();
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact",
"([Ljava/lang/Object;)Ljava/lang/Object;", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}
示例11: generateStringMaker
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static byte[] generateStringMaker() {
// interface StringMaker extends Maker {
// String make();
// }
ClassWriter cw = new ClassWriter(0);
cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"});
cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
"()Ljava/lang/String;", null, null);
cw.visitEnd();
return cw.toByteArray();
}
示例12: defineIndyBootstrapMethodClass
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
void defineIndyBootstrapMethodClass(ClassWriter cw) {
cw.visit(52, ACC_SUPER | ACC_PUBLIC,
BOOTSTRAP_METHOD_CLASS_NAME, null, "java/lang/Object", null);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC,
BOOTSTRAP_METHOD_NAME, BOOTSTRAP_METHOD_DESC, null, null);
mv.visitCode();
defineIndyBootstrapMethodBody(mv);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
示例13: dump_HasFinal
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static void dump_HasFinal () throws Exception {
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "HasFinal", null, "java/lang/Object", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "m", "(Ljava/lang/String;)V", null, null);
mv.visitCode();
mv.visitInsn(RETURN);
mv.visitMaxs(0, 2);
mv.visitEnd();
}
cw.visitEnd();
try (FileOutputStream fos = new FileOutputStream(new File("HasFinal.class"))) {
fos.write(cw.toByteArray());
}
}
示例14: dumpP1B
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] dumpP1B() {
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "P1/B", null, "P1/A", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "P1/A", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(0, "m", "()I", null, null);
mv.visitCode();
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("B.m");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
mv.visitIntInsn(BIPUSH, 2);
mv.visitInsn(IRETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
示例15: dumpClassFile
import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] dumpClassFile(int cfv, int testDimension264, String arrayDim) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
MethodVisitor mv;
classCName = "classCName_" + cfv + "_" + testDimension264;
cw.visit(cfv, ACC_PUBLIC + ACC_SUPER, classCName, null, "java/lang/Object", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{ // classCName main method
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
mv.visitCode();
mv.visitIntInsn(BIPUSH, 1);
mv.visitTypeInsn(ANEWARRAY, arrayDim); // Test ANEWARRAY bytecode with various dimensions
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}