本文整理匯總了Java中org.objectweb.asm.ClassWriter類的典型用法代碼示例。如果您正苦於以下問題:Java ClassWriter類的具體用法?Java ClassWriter怎麽用?Java ClassWriter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ClassWriter類屬於org.objectweb.asm包,在下文中一共展示了ClassWriter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: isClassOk
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
/**
* Check to see if a class is well-formed.
*
* @param logger the logger to write to if a problem is found
* @param logTag a tag to print to the log if a problem is found
* @param classNode the class to check
* @return true if the class is ok, false otherwise
*/
public static boolean isClassOk(final Logger logger, final String logTag, final ClassNode classNode) {
final StringWriter sw = new StringWriter();
final ClassWriter verifyWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
classNode.accept(verifyWriter);
final ClassReader ver = new ClassReader(verifyWriter.toByteArray());
try {
DrillCheckClassAdapter.verify(ver, false, new PrintWriter(sw));
} catch(final Exception e) {
logger.info("Caught exception verifying class:");
logClass(logger, logTag, classNode);
throw e;
}
final String output = sw.toString();
if (!output.isEmpty()) {
logger.info("Invalid class:\n" + output);
return false;
}
return true;
}
示例2: generateClosureClass
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
private void generateClosureClass(TContext closure_ctx) throws CompileException {
ClassWriter cw = ((TContextClosure) closure_ctx).getClassWriter();
Debug.assertion(cw != null, "cw should be valid");
// write class file
try {
LOG.debug("Writing Class File(" + closure_ctx.getName() + ")");
//byte[] code = cpLoader.writeClassFile(cw, closure_ctx.getName());
cpLoader.writeClassFile(cw, closure_ctx.getName());
} catch (Exception e) {
// e.printStackTrace();
CompileException excp = new CompileException("Exception occurred in writing class file");
excp.setTargetException(e);
throw excp;
}
}
示例3: registerClassMemberVar
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
private void registerClassMemberVar(TContextClass class_ctx, Container container) throws CompileException {
ClassWriter cw = class_ctx.getClassWriter();
Debug.assertion(cw != null, "ClassWriter should not be null");
AbsType field_type = container.getType();
Debug.assertion(field_type != null, "Container Type should not be null");
//// Compiled Instruction
LOG.debug(
"Adding Field(" + container.name + ":" + field_type.getMthdDscStr() + ") in (" + class_ctx.getName() + ")");
int access = 0;
access |= Opcodes.ACC_PUBLIC;
// access |= Opcodes.ACC_PROTECTED;
if (container.isSingleton()) {
access |= Opcodes.ACC_STATIC;
}
cw.visitField(access, container.name, field_type.getMthdDscStr(), null, null).visitEnd();
//// End
}
示例4: createMainMethod
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
private static void createMainMethod(ClassWriter cw) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
for (SysProp type : SysProp.values()) {
if (type != SysProp.Z_ERROR) {
dumpProperty(mv, type.sysProp);
}
}
mv.visitInsn(RETURN);
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l3, 0);
mv.visitMaxs(3, 1);
mv.visitEnd();
}
示例5: generateProxyClass
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
private void generateProxyClass(ClassWriter visitor, StructSchema<?> viewSchema, StructBindings<?> bindings, Collection<String> interfacesToImplement,
Collection<ModelType<?>> viewTypes, Type generatedType, Type superclassType, Class<? extends GeneratedViewState> backingStateType) {
Class<?> viewClass = viewSchema.getType().getConcreteClass();
StructSchema<?> delegateSchema = bindings.getDelegateSchema();
declareClass(visitor, interfacesToImplement, generatedType, superclassType);
declareStateField(visitor);
declareTypeConverterField(visitor);
declareManagedTypeField(visitor);
declareCanCallSettersField(visitor);
writeStaticConstructor(visitor, generatedType, viewClass);
writeConstructor(visitor, generatedType, superclassType, delegateSchema, Type.getType(backingStateType));
writeToString(visitor, generatedType, viewClass, delegateSchema);
writeGeneratedViewMethods(visitor, generatedType);
if (backingStateType == ModelElementState.class) {
writeManagedInstanceMethods(visitor, generatedType);
}
writeGroovyMethods(visitor, viewClass);
writeViewMethods(visitor, generatedType, viewTypes, bindings);
writeHashCodeMethod(visitor, generatedType);
writeEqualsMethod(visitor, generatedType);
visitor.visitEnd();
}
示例6: generate
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
/**
* Generates an implementation of the given managed type.
*
* <p>The generated type will:</p>
*
* <ul>
* <li>extend the given implementation class</li>
* <li>implement the given public interface</li>
* <li>override each public constructor of the given implementation class</li>
* </ul>
*/
public Class<?> generate(Class<?> implClass, Class<?> publicContractType) {
ClassWriter visitor = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
String generatedTypeName = publicContractType.getName() + "_Impl";
Type generatedType = Type.getType("L" + generatedTypeName.replaceAll("\\.", "/") + ";");
Type superclassType = Type.getType(implClass);
Type publicType = Type.getType(publicContractType);
generateClass(visitor, generatedType, superclassType, publicType);
generateConstructors(visitor, implClass, superclassType);
visitor.visitEnd();
return defineClass(visitor, publicContractType.getClassLoader(), generatedTypeName);
}
示例7: generateConstructors
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
private <T> void generateConstructors(ClassWriter visitor, Class<? extends T> implClass, Type superclassType) {
for (Constructor<?> constructor : implClass.getConstructors()) {
Type[] paramTypes = new Type[constructor.getParameterTypes().length];
for (int i = 0; i < paramTypes.length; i++) {
paramTypes[i] = Type.getType(constructor.getParameterTypes()[i]);
}
String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, paramTypes);
MethodVisitor constructorVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME, methodDescriptor, CONCRETE_SIGNATURE, NO_EXCEPTIONS);
constructorVisitor.visitCode();
putThisOnStack(constructorVisitor);
for (int i = 0; i < paramTypes.length; i++) {
constructorVisitor.visitVarInsn(paramTypes[i].getOpcode(Opcodes.ILOAD), i + 1);
}
constructorVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), CONSTRUCTOR_NAME, methodDescriptor, false);
finishVisitingMethod(constructorVisitor);
}
}
示例8: transform
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
public byte[] transform(String name, byte[] basicClass) {
int i = 0;
while (i < transformers.size()) {
if (transformers.get(i).is(name)) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
transformers.get(i).transform(classNode);
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
basicClass = writer.toByteArray();
System.out.println("[" + MineCamera.MODID + "] Patched " + transformers.get(i).className + " ...");
transformers.get(i).done();
i++;
// transformers.remove(i);
} else
i++;
}
return basicClass;
}
示例9: transform
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
List<ITransformer> transformers = getTransformers(transformedName);
if (!transformers.isEmpty()) {
ClassNode cn = getClassNode(basicClass);
if (cn == null)
return basicClass;
// Run all transformers on the Class
transformers.forEach(transformer -> transformer.transform(cn));
// Return transformed class bytecode
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
cn.accept(cw);
return cw.toByteArray();
}
return basicClass;
}
示例10: serializeClass
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
public byte[] serializeClass(ClassInstance cls, boolean mapped) {
ClassNode cn = cls.getMergedAsmNode();
if (cn == null) throw new IllegalArgumentException("cls without asm node: "+cls);
ClassWriter writer = new ClassWriter(0);
synchronized (Util.asmNodeSync) {
if (mapped) {
AsmClassRemapper.process(cn, remapper, writer);
} else {
cn.accept(writer);
}
}
return writer.toByteArray();
}
示例11: 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);
}
}
}
示例12: 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);
}
}
示例13: 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();
}
示例14: rewriteClass
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
/**
* Writes a modified *.class file to the output JAR file.
*/
private void rewriteClass(
JarEntry entry,
InputStream inputStream,
JarOutputStream outputStream) throws IOException {
ClassReader classReader = new ClassReader(inputStream);
ClassNode classNode = new ClassNode(Opcodes.ASM5);
classReader.accept(classNode, EMPTY_FLAGS);
modifyClass(classNode);
ClassWriter classWriter = new ClassWriter(0);
classNode.accept(classWriter);
outputStream.putNextEntry(new ZipEntry(entry.getName()));
outputStream.write(classWriter.toByteArray());
}
示例15: AsmFlagsPanel
import org.objectweb.asm.ClassWriter; //導入依賴的package包/類
public AsmFlagsPanel() {
//@formatter:off
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel p1 = new JPanel();
p1.setBorder(BorderFactory.createTitledBorder("Input Flags"));
p1.setLayout(new GridLayout(0, 2));
p1.add(inE = new ActionCheckBox("Expand Frames", Access.hasAccess(getOptions().classFlagsInput, ClassReader.EXPAND_FRAMES), (b) -> update()));
p1.add(inD = new ActionCheckBox("Skip Debug", Access.hasAccess(getOptions().classFlagsInput, ClassReader.SKIP_DEBUG), (b) -> update()));
p1.add(inF = new ActionCheckBox("Skip Frames", Access.hasAccess(getOptions().classFlagsInput, ClassReader.SKIP_FRAMES), (b) -> update()));
p1.add(inC = new ActionCheckBox("Skip Code", Access.hasAccess(getOptions().classFlagsInput, ClassReader.SKIP_CODE), (b) -> update()));
JPanel p2 = new JPanel();
p2.setBorder(BorderFactory.createTitledBorder("Output Flags"));
p2.setLayout(new GridLayout(0, 2));
p2.add(outF = new ActionCheckBox("Compute Frames", Access.hasAccess(getOptions().classFlagsOutput, ClassWriter.COMPUTE_FRAMES), (b) -> update()));
p2.add(outM = new ActionCheckBox("Compute Maxs", Access.hasAccess(getOptions().classFlagsOutput, ClassWriter.COMPUTE_MAXS), (b) -> update()));
add(p1);
add(p2);
//@formatter:on
}