本文整理汇总了Java中org.objectweb.asm.ClassWriter.visit方法的典型用法代码示例。如果您正苦于以下问题:Java ClassWriter.visit方法的具体用法?Java ClassWriter.visit怎么用?Java ClassWriter.visit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.objectweb.asm.ClassWriter
的用法示例。
在下文中一共展示了ClassWriter.visit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateClass
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] generateClass() {
ClassWriter cw = new ClassWriter(0);
cw.visit(52, ACC_SUPER | ACC_PUBLIC, INNER_CLASS_NAME_INTERNAL, null, "java/lang/Object", null);
cw.visitSource("UnbalancedMonitorsTest.java", null);
cw.visitInnerClass(INNER_CLASS_NAME_INTERNAL, CLASS_NAME_INTERNAL, "UnbalancedMonitors", ACC_STATIC);
visitConstructor(cw);
visitWrongOrder(cw);
visitBlockStructured(cw, true, false);
visitBlockStructured(cw, true, true);
visitBlockStructured(cw, false, false);
visitBlockStructured(cw, false, true);
cw.visitEnd();
return cw.toByteArray();
}
示例2: transform
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
protected byte[] transform(String className, byte[] bytes) {
// First scan for annotation, and short circuit transformation if not present
ClassReader classReader = new Java9ClassReader(bytes);
AnnotationDetector detector = new AnnotationDetector();
classReader.accept(detector, ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE);
if (!detector.found) {
return bytes;
}
if (findLoadedClass(detector.interfaceName) == null) {
// TODO:ADAM - need to do this earlier
ClassWriter emptyWriter = new ClassWriter(0);
emptyWriter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_INTERFACE, detector.interfaceName.replace('.', '/'), null, Type.getType(Object.class).getInternalName(), null);
emptyWriter.visitEnd();
byte[] emptyBytecode = emptyWriter.toByteArray();
defineClass(detector.interfaceName, emptyBytecode, 0, emptyBytecode.length);
}
ClassWriter classWriter = new ClassWriter(0);
classReader.accept(new TransformingAdapter(classWriter, detector.interfaceName), 0);
bytes = classWriter.toByteArray();
return bytes;
}
示例3: run
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public void run() {
if (!hasWorkingJavac()) {
ClassWriter w = new ClassWriter(0);
w.visit(Opcodes.V1_8, Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC, "com/sun/tools/javac/code/Scope$WriteableScope", null, "com/sun/tools/javac/code/Scope", null);
byte[] classData = w.toByteArray();
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
Class scopeClass = Class.forName("com.sun.tools.javac.code.Scope");
unsafe.defineClass("com.sun.tools.javac.code.Scope$WriteableScope", classData, 0, classData.length, scopeClass.getClassLoader(), scopeClass.getProtectionDomain());
} catch (Throwable t) {
//ignore...
Logger.getLogger(NoJavacHelper.class.getName()).log(Level.FINE, null, t);
}
}
}
示例4: proceed
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public void proceed() {
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
writer.visit(Opcodes.V1_6,
Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_SUPER,
RSymbols.R_STYLEABLES_CLASS_NAME,
null,
"java/lang/Object",
null);
for (String name : symbols.getStyleables().keySet()) {
writer.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, name, "[I", null, null);
}
writeClinit(writer);
writer.visitEnd();
byte[] bytes = writer.toByteArray();
try {
if (!dir.isDirectory() && !dir.mkdirs()) {
throw new RuntimeException("Cannot mkdir " + dir);
}
Files.write(dir.toPath().resolve(RSymbols.R_STYLEABLES_CLASS_NAME + ".class"), bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
示例5: generate
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public byte[] generate(){
String className = "eu.mikroskeem.test.shuriken.common.classtools.gen.InvalidGeneratedClass";
String classNameInternal = className.replaceAll("\\.", "/");
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
cw.visit(
V1_8,
ACC_PUBLIC + ACC_SUPER,
classNameInternal,
null,
Type.getInternalName(Object.class),
null);
FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "test", Type.getDescriptor(String.class), null, "hey");
fv.visitEnd();
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}
示例6: compile
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public byte[] compile(String className, Class<?> clazz) throws IOException, CompilerException {
ClassWriter cw = new ClassWriter(0);
Type clazzType = Type.getType(clazz);
String internalName = className.replace('.', '/');
String classDesc = "L" + internalName + ";";
String signature = BytecodeGenerator.OBJECT.getDescriptor() + "L" +
BytecodeGenerator.OBJECT_TEMPLATE.getInternalName() + "<" + clazzType.getDescriptor() + ">;";
cw.visit(52, ACC_PUBLIC + ACC_SUPER, internalName, signature,
BytecodeGenerator.OBJECT.getInternalName(),
new String[]{BytecodeGenerator.OBJECT_TEMPLATE.getInternalName()});
cw.visitSource(templateName, null);
createConstructor(cw, classDesc);
createObjectRender(cw, internalName, classDesc, clazzType);
DataManager getter = new ClassDataManager(clazz, clazzType);
generator = new BytecodeGenerator(this, cw, getter, internalName, classDesc);
generator.insertMethodStart("render");
parser.parse();
generator.insertMethodEnd();
cw.visitEnd();
return cw.toByteArray();
}
示例7: generateClass
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static byte[] generateClass(Class<? extends TileEntity> baseClass, String className, String contentId)
{
ClassWriter cw = new ClassWriter(0);
cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, className, null, Type.getInternalName(baseClass), null);
// Constructor
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(contentId);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(baseClass), "<init>", "(Ljava/lang/String;)V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
return cw.toByteArray();
}
示例8: generateSyntheticClass
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public byte[] generateSyntheticClass(String name) {
ClassWriter visitor = new ClassWriter(0);
visitor.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT, name.replace('.', '/'), null, OBJECT_TYPE.getInternalName(), null);
visitor.visitEnd();
return visitor.toByteArray();
}
示例9: genClass
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Test
public void genClass()
{
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_6, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE,
"cn/edu/ruc/iir/paraflow/loader/consumer/utils/Comparable",
null,
"java/lang/Object",
null);
classWriter.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
"LESS", "I", null, -1)
.visitEnd();
classWriter.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
"EQUAL", "I", null, 0)
.visitEnd();
classWriter.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
"GREATER", "I", null, 1)
.visitEnd();
classWriter.visitMethod(ACC_PUBLIC + ACC_ABSTRACT,
"compareTo", "(Ljava/lang/Object;)I", null, null)
.visitEnd();
classWriter.visitEnd();
byte[] bytes = classWriter.toByteArray();
MyClassLoader myClassLoader = new MyClassLoader();
Class clazz = myClassLoader.defineClass("cn.edu.ruc.iir.paraflow.loader.consumer.utils.Comparable", bytes);
try {
System.out.println(clazz.getField("LESS").getName());
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
示例10: generateConfigClass
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
* Creates AppPerformanceConfig.class file with a static boolean field AppPerformanceConfig#enabled in it.
*
* @param value Initializes the AppPerformanceConfig#enabled with value.
* @return byte array of the class.
*/
public static byte[] generateConfigClass(boolean value) {
ClassWriter cw = new ClassWriter(0);
cw.visit(V1_6, ACC_FINAL + ACC_SUPER,
"com/rakuten/tech/mobile/perf/runtime/internal/AppPerformanceConfig", null,
"java/lang/Object", null);
cw.visitField(ACC_PUBLIC + ACC_STATIC, "enabled", "Z", null, value);
cw.visitEnd();
return cw.toByteArray();
}
示例11: testClassLoading
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Test
public void testClassLoading() throws Exception {
SimpleClassLoader simpleClassLoader = new SimpleClassLoader(ClassLoader.getSystemClassLoader());
String className = getClass().getPackage().getName()+".GeneratedTestClass";
String classNameInternal = className.replaceAll("\\.", "/");
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, classNameInternal, null, Type.getInternalName(Object.class), null);
{
FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "test", Type.getDescriptor(String.class), null, "hey");
fv.visitEnd();
}
{
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
cw.visitEnd();
}
cw.visitEnd();
/* Test class loading */
Class<?> generated = ClassLoaderTools.defineClass(simpleClassLoader, className, cw.toByteArray());
Assertions.assertNotNull(generated, "Class didn't load!");
Assertions.assertTrue(Reflect.getClass(className, simpleClassLoader).isPresent(),
"Class isn't present in classloader!");
Assertions.assertFalse(Reflect.getClass(className).isPresent()
, "Class is present in system class loader!");
/* Test field */
ClassWrapper<?> c = Reflect.construct(Reflect.wrapClass(generated));
Optional<FieldWrapper<String>> testFieldOptional = c.getField("test", String.class);
Assertions.assertTrue(testFieldOptional.isPresent(), "Test field is not present!");
Assertions.assertEquals("hey", testFieldOptional.get().read(), "Test field content didn't match!");
}
示例12: generate
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static Pair<String, byte[]> generate(){
String className = "eu/mikroskeem/test/shuriken/classloader/classes/TestClass1";
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
cw.visit(52, ACC_PUBLIC + ACC_SUPER, className, null, Type.getInternalName(Object.class), null);
ClassTools.generateSimpleSuperConstructor(cw, Object.class);
cw.visitEnd();
return new Pair<>(className+".class.br", cw.toByteArray());
}
示例13: generateMethodArgsUpdater
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] generateMethodArgsUpdater(Class<?> classToProxy, Method method, int methodId) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
String classToProxyDescriptor = Type.getDescriptor(classToProxy);
String classToProxyInternalName = Type.getInternalName(classToProxy);
String suffix = SUFFIX_START + method.getName() + methodId;
String selfClassInternalName = classToProxyInternalName + suffix;
String selfClassDescriptor = BytecodeGenUtils.makeSuffixClassDescriptor(classToProxyDescriptor, suffix);
String argsClassInternalName = classToProxyInternalName + MethodArgumentsGenerator.SUFFIX_START + method.getName() + methodId;
String constDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
Stream.concat(Stream.of(List.class), Stream.of(method.getParameterTypes())).map(Type::getType)
.toArray(Type[]::new));
cw.visit(52, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, selfClassInternalName, null, "java/lang/Object",
new String[] { "io/primeval/reflex/arguments/ArgumentsUpdater" });
Parameter[] parameters = method.getParameters();
generateFields(method, cw, parameters);
generateConstructor(method, cw, selfClassInternalName, selfClassDescriptor, constDesc, parameters);
generateHashCodeMethod(cw, selfClassInternalName, selfClassDescriptor, parameters);
generateEqualsMethod(cw, selfClassInternalName, selfClassDescriptor, parameters);
generateToStringMethod(cw, selfClassInternalName, selfClassDescriptor, parameters);
generateUpdateMethod(cw, selfClassInternalName, selfClassDescriptor, argsClassInternalName, constDesc, parameters);
generateParametersGetter(cw, selfClassInternalName, selfClassDescriptor);
generateArgumentSetters(cw, selfClassInternalName, selfClassDescriptor, parameters);
generateArgumentGetters(cw, selfClassInternalName, selfClassDescriptor, parameters);
cw.visitEnd();
return cw.toByteArray();
}
示例14: visit
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public Object visit(ASTStreamSignature node, Object data) throws CompileException {
LangUnitNode stream_hdr = node.getChildren(0);
Debug.assertion(stream_hdr.isNodeId(JJTSTREAMHEADER),
"first child should be Stream Type, but(" + stream_hdr.getNodeName() + ")");
switch (stream_hdr.getStreamForm()) {
case LangUnitNode.STREAMFORM_CLASS: {
TContextClass class_context = (TContextClass) this.getTopContext();
Debug.assertion(class_context != null, "top context should not be null");
Debug.assertion(class_context.isForm(AbsType.FORM_CLASS), "top context should be class");
String class_name = class_context.getName();
String super_class_name = null;
AbsClassType super_class = class_context.getSuperClass();
if (super_class != null) {
super_class_name = ((AbsType) super_class).getName();
Debug.assertion(super_class_name != null, "Super Class Name should not be invalid");
}
String[] interfaces = check_interface_impl_and_get(class_context);
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
class_context.setClassWriter(cw);
LOG.info("Creating Public Class...({}) super({})", class_name, super_class_name);
//// Writing Byte Code
cw.visit(Compiler.java_version, Opcodes.ACC_PUBLIC, class_name, null, super_class_name, interfaces);
if (class_context.isGenDfltConstructor()) {
// if it does not have any constructor, add a dummy constructor
// default constructor
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
// super class default constructor call
if (super_class != null) {
LOG.info("ALOAD 0");
LOG.info("INVOKESPECIAL " + super_class_name + " <init> ()V");
mv.visitVarInsn(Opcodes.ALOAD, 0); // this
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, super_class_name, "<init>", "()V", false);
}
if (Debug.enable_compile_debug_print) {
// LOG.info("GETSTATIC java/lang/System.out");
// LOG.info("LDC "+class_name+" was instantiated");
// LOG.info("INVOKEVIRTUAL java/io/PrintStream.println");
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn(class_name + " was instantiated");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
}
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
//// end Byte Code
}
}
break;
default:
// do nothing for other stream form ...
}
return null;
}
示例15: getPatchFileContents
import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] getPatchFileContents(
ImmutableList<String> patchFileContents, ImmutableList<Integer> patchIndexContents) {
if (patchFileContents.size() != patchIndexContents.size()) {
throw new GradleException("patchFileContents's size is "
+ patchFileContents.size() + ", but patchIndexContents's size is "
+ patchIndexContents.size() + ", please check the changed classes.");
}
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER,
IncrementalVisitor.APP_PATCHES_LOADER_IMPL, null,
IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, null);
{
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL,
IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL,
"<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(Opcodes.ACC_PUBLIC,
"getPatchedClasses", "()[Ljava/lang/String;", null, null);
mv.visitCode();
mv.visitIntInsn(Opcodes.SIPUSH, patchFileContents.size());
mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
for (int index = 0; index < patchFileContents.size(); index++) {
mv.visitInsn(Opcodes.DUP);
mv.visitIntInsn(Opcodes.SIPUSH, index);
mv.visitLdcInsn(patchFileContents.get(index));
mv.visitInsn(Opcodes.AASTORE);
}
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(4, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(Opcodes.ACC_PUBLIC,
"getPatchedClassIndexes", "()[I", null, null);
mv.visitCode();
mv.visitIntInsn(Opcodes.SIPUSH, patchIndexContents.size());
mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
for (int index = 0; index < patchIndexContents.size(); index++) {
mv.visitInsn(Opcodes.DUP);
mv.visitIntInsn(Opcodes.SIPUSH, index);
mv.visitLdcInsn(patchIndexContents.get(index));
mv.visitInsn(Opcodes.IASTORE);
}
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(4, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}