当前位置: 首页>>代码示例>>Java>>正文


Java ClassWriter类代码示例

本文整理汇总了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;
}
 
开发者ID:skhalifa,项目名称:QDrill,代码行数:29,代码来源:AsmUtil.java

示例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;
  }
}
 
开发者ID:Samsung,项目名称:MeziLang,代码行数:18,代码来源:ASTCompileVisitor.java

示例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
}
 
开发者ID:Samsung,项目名称:MeziLang,代码行数:23,代码来源:ASTCompileVisitor.java

示例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();
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:18,代码来源:JavaInstallationProbe.java

示例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();
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:23,代码来源:ManagedProxyClassGenerator.java

示例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);
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:27,代码来源:ManagedCollectionProxyClassGenerator.java

示例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);
    }
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:18,代码来源:ManagedCollectionProxyClassGenerator.java

示例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;
}
 
开发者ID:PorPit,项目名称:MineCamera,代码行数:24,代码来源:CamTransformer.java

示例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;
}
 
开发者ID:ImpactDevelopment,项目名称:ClientAPI,代码行数:21,代码来源:ClientTransformer.java

示例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();
}
 
开发者ID:sfPlayer1,项目名称:Matcher,代码行数:17,代码来源:ClassFeatureExtractor.java

示例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);
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:NoJavacHelper.java

示例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);
    }
}
 
开发者ID:yrom,项目名称:shrinker,代码行数:26,代码来源:WriteStyleablesProcessor.java

示例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();
}
 
开发者ID:cubex2,项目名称:customstuff4,代码行数:17,代码来源:TileEntityRegistry.java

示例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());
}
 
开发者ID:codezjx,项目名称:MockableJarGenerator,代码行数:21,代码来源:MockableJarGenerator.java

示例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
}
 
开发者ID:Col-E,项目名称:Recaf,代码行数:20,代码来源:AsmFlagsPanel.java


注:本文中的org.objectweb.asm.ClassWriter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。