classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] RFC: Reflection refactoring


From: Jeroen Frijters
Subject: [cp-patches] RFC: Reflection refactoring
Date: Tue, 22 Mar 2005 14:01:31 +0100

Hi,

I would like to add an indirection layer to reflection (attached is
java.lang.reflect.Field, but I also want to do this for Method and
Constructor). By doing this, we can start caching these objects in Class
and have a Classpath common reflection caching mechanism.

To give an idea of how that would look, here's a rough example:

public class Class
{
  private SoftReference reflectionCache;

  static class ReflectionCache
  {
    Constructor defaultConstructor;
    VMMethod[] methods;
    VMField[] fields;
  }

  private ReflectionCache getReflectionCache()
  {
    ReflectionCache rc;
    if(reflectionCache == null)
    {
      rc = new ReflectionCache();
      reflectionCache = new SoftReference(rc);
      return rc;
    }
    rc = reflectionCache.get();
    if(rc == null)
    {
      rc = new ReflectionCache();
      reflectionCache = new SoftReference(rc);
    }
    return rc;
  }

  public Field getDeclaredField(String name) throws NoSuchFieldException
  {
    memberAccessCheck(Member.DECLARED);
    VMField[] field = internalGetDeclaredFields();
    for (int i = 0; i < fields.length; i++)
      {
        if (fields[i].getName().equals(name))
          return fields[i].newField();
      }
    throw new NoSuchFieldException();
  }

  private VMField[] internalGetDeclaredFields()
  {
    ReflectionCache rc = getReflectionCache();
    if(rc.fields == null)
    {
      rc.fields = VMClass.getDeclaredFields(this);
    }
    return rc.fields;
  }
}

Regards,
Jeroen

Attachment: Field.java
Description: Field.java

Attachment: VMField.java
Description: VMField.java


reply via email to

[Prev in Thread] Current Thread [Next in Thread]