1

The problem is that I need to create in runtime a class like this:

public class Foo {
  @Bar int value0;
  @Bar int value1;
  @Bar int value2;
....
}

with number of fields being decided at runtime.

I was looking at Javassist, and there you can create a new class, and add fields to it, but I haven't found a way to annotate those fields.

2
  • I'd be interested to know why you can't use an array. Commented May 22, 2010 at 2:34
  • I am using library that requires it. Library was intended to be used for fixed objects, but I need more flexibility with generating them at runtime. Commented May 22, 2010 at 10:10

1 Answer 1

6

You can use a bytecode manipulation library like ASM:

import java.lang.reflect.Field;
import java.util.Arrays;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;
public class a {
  public static void main(String[] args) throws Exception {
    Class<?> klass = new ClassLoader(a.class.getClassLoader()) {
      public Class<?> defineClass() {
        ClassWriter cw = new ClassWriter(0);
        cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER,
          "Foo", null, "java/lang/Object", null);
        for (int i = 0; i < 3; i++) {
          FieldVisitor fv = cw.visitField(0, "value" + i, "I", null, null);
          fv.visitAnnotation("LBar;", true).visitEnd();
        }
        cw.visitEnd();
        byte[] bytes = cw.toByteArray();
        return defineClass("Foo", bytes, 0, bytes.length);
      }
    }.defineClass();

    for (Field f : klass.getDeclaredFields()) {
      System.out.println(f + " " + Arrays.toString(f.getAnnotations()));
    }
  }
}

Output:

int Foo.value0 [@Bar()]
int Foo.value1 [@Bar()]
int Foo.value2 [@Bar()]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.