Java Code Examples for org.objectweb.asm.ClassVisitor#visitField()

The following examples show how to use org.objectweb.asm.ClassVisitor#visitField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassVisitorDriverFromElement.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitVariable(VariableElement e, ClassVisitor classVisitor) {
  if (e.getModifiers().contains(Modifier.PRIVATE)) {
    return null;
  }

  FieldVisitor fieldVisitor =
      classVisitor.visitField(
          accessFlagsUtils.getAccessFlags(e),
          e.getSimpleName().toString(),
          descriptorFactory.getDescriptor(e),
          signatureFactory.getSignature(e),
          e.getConstantValue());
  visitAnnotations(e, fieldVisitor::visitAnnotation);
  fieldVisitor.visitEnd();

  return null;
}
 
Example 2
Source File: FieldCreatorImpl.java    From gizmo with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ClassVisitor file) {
    FieldVisitor fieldVisitor = file.visitField(modifiers, fieldDescriptor.getName(), fieldDescriptor.getType(), signature, null);
    for(AnnotationCreatorImpl annotation : annotations) {
        AnnotationVisitor av = fieldVisitor.visitAnnotation(DescriptorUtils.extToInt(annotation.getAnnotationType()), annotation.getRetentionPolicy() == RetentionPolicy.RUNTIME);
        for(Map.Entry<String, Object> e : annotation.getValues().entrySet()) {
            AnnotationUtils.visitAnnotationValue(av, e.getKey(), e.getValue());
        }
        av.visitEnd();
    }
    fieldVisitor.visitEnd();
}
 
Example 3
Source File: ClassFile.java    From OSRS-Deobfuscator with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void accept(ClassVisitor visitor)
{
	String[] ints = interfaces.getInterfaces().stream().map(i -> i.getName()).toArray(String[]::new);

	visitor.visit(version, access, name.getName(), null, super_class.getName(), ints);
	visitor.visitSource(source, null);

	for (Annotation annotation : annotations.getAnnotations())
	{
		AnnotationVisitor av = visitor.visitAnnotation(annotation.getType().toString(), true);
		annotation.accept(av);
	}

	for (Field field : fields)
	{
		FieldVisitor fv = visitor.visitField(field.getAccessFlags(), field.getName(), field.getType().toString(), null, field.getValue());
		field.accept(fv);
	}

	for (Method method : methods)
	{
		String[] exceptions = method.getExceptions().getExceptions().stream().map(cl -> cl.getName()).toArray(String[]::new);
		if (exceptions.length == 0)
		{
			exceptions = null;
		}

		MethodVisitor mv = visitor.visitMethod(method.getAccessFlags(), method.getName(), method.getDescriptor().toString(), null, exceptions);
		method.accept(mv);
	}

	visitor.visitEnd();
}
 
Example 4
Source File: CommonHelpers.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
static void addIsServlet3(ClassVisitor cw) {
  FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, "$$isServlet3", "Z", null, null);
  fv.visitEnd();

  MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "$$isServlet3", "()Z", null, null);
  mv.visitCode();
  Label l0 = new Label();
  Label l1 = new Label();
  Label l2 = new Label();
  mv.visitTryCatchBlock(l0, l1, l2, "java/lang/NoSuchMethodException");
  mv.visitLabel(l0);
  mv.visitLineNumber(446, l0);
  mv.visitLdcInsn(Type.getType("Ljavax/servlet/ServletRequest;"));
  mv.visitLdcInsn("startAsync");
  mv.visitInsn(ICONST_0);
  mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
  mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getMethod",
      "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
  mv.visitInsn(POP);
  mv.visitLabel(l1);
  mv.visitLineNumber(447, l1);
  mv.visitInsn(ICONST_1);
  mv.visitInsn(IRETURN);
  mv.visitLabel(l2);
  mv.visitLineNumber(448, l2);
  mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/NoSuchMethodException" });
  mv.visitVarInsn(ASTORE, 0);
  Label l3 = new Label();
  mv.visitLabel(l3);
  mv.visitLineNumber(449, l3);
  mv.visitInsn(ICONST_0);
  mv.visitInsn(IRETURN);
  Label l4 = new Label();
  mv.visitLabel(l4);
  mv.visitLocalVariable("e", "Ljava/lang/NoSuchMethodException;", null, l3, l4, 0);
  mv.visitMaxs(3, 1);
  mv.visitEnd();
}
 
Example 5
Source File: GenNerdClass.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ClassWriter cw = new ClassWriter(0);
	TraceClassVisitor tracer =
			new TraceClassVisitor(cw, new PrintWriter(System.out));
	ClassVisitor cv = tracer;
	String name = "Nerd";
	String generics = null;
	String superName = "java/lang/Object";
	String[] interfaces = null;
	int access = ACC_PUBLIC + ACC_INTERFACE;
	int version = V1_5;
	cv.visit(version, access, name, generics, superName, interfaces);

	int fieldAccess = ACC_PUBLIC + ACC_FINAL + ACC_STATIC;
	String shortDescriptor = Type.SHORT_TYPE.getDescriptor();
	FieldVisitor hair = cv.visitField(fieldAccess, "hair", shortDescriptor,
									  null, new Integer(0));
	hair.visitEnd();

	MethodVisitor playVideoGame =
		cv.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "playVideoGame",
					   "()V", null, null);
	// no code to define, just finish it up
	playVideoGame.visitEnd();
	cv.visitEnd(); // prints if using tracer
	byte[] b = cw.toByteArray();
	// can define or write to file:
	// defineClass(name, b, 0, b.length)
	// from findClass() in subclass of ClassLoader

	FileOutputStream fos = new FileOutputStream("Nerd.class");
	fos.write(b);
	fos.close();
}
 
Example 6
Source File: GenNerdClass.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ClassWriter cw = new ClassWriter(0);
	TraceClassVisitor tracer =
			new TraceClassVisitor(cw, new PrintWriter(System.out));
	ClassVisitor cv = tracer;
	String name = "Nerd";
	String generics = null;
	String superName = "java/lang/Object";
	String[] interfaces = null;
	int access = ACC_PUBLIC + ACC_INTERFACE;
	int version = V1_5;
	cv.visit(version, access, name, generics, superName, interfaces);

	int fieldAccess = ACC_PUBLIC + ACC_FINAL + ACC_STATIC;
	String shortDescriptor = Type.SHORT_TYPE.getDescriptor();
	FieldVisitor hair = cv.visitField(fieldAccess, "hair", shortDescriptor,
									  null, new Integer(0));
	hair.visitEnd();

	MethodVisitor playVideoGame =
		cv.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "playVideoGame",
					   "()V", null, null);
	// no code to define, just finish it up
	playVideoGame.visitEnd();
	cv.visitEnd(); // prints if using tracer
	byte[] b = cw.toByteArray();
	// can define or write to file:
	// defineClass(name, b, 0, b.length)
	// from findClass() in subclass of ClassLoader

	FileOutputStream fos = new FileOutputStream("Nerd.class");
	fos.write(b);
	fos.close();
}
 
Example 7
Source File: ProxyChannelFactory.java    From JCTools with Apache License 2.0 5 votes vote down vote up
private static void implementInstanceFields(ClassVisitor classVisitor) {
    classVisitor.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL,
            "waitStrategy",
            Type.getDescriptor(WaitStrategy.class),
            null,
            null);
}
 
Example 8
Source File: ObfMapping.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
public FieldVisitor visitField(ClassVisitor visitor, int access, Object value) {
    return visitor.visitField(access, s_name, s_desc, null, value);
}
 
Example 9
Source File: MixinField.java    From android-perftracking with MIT License 4 votes vote down vote up
public void add(ClassVisitor cv) {
  _log.info("Adding field " + _fn.name);
  cv.visitField(_fn.access, _fn.name, _fn.desc, _fn.signature, _fn.value);
  //_fn.accept(cv);
}
 
Example 10
Source File: FieldDefinition.java    From yql-plus with Apache License 2.0 4 votes vote down vote up
public void generate(ClassVisitor cw) {
    FieldVisitor fv = cw.visitField(modifiers, name, type.getJVMType().getDescriptor(), null, null);
    generateAnnotations(fv);
    fv.visitEnd();
}
 
Example 11
Source File: ObfMapping.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
public FieldVisitor visitField(ClassVisitor visitor, int access, Object value) {
    return visitor.visitField(access, s_name, s_desc, null, value);
}