public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] three annotations-related fixes/enhancements
@ 2015-08-13 19:30 Jamison Hope
  2015-08-13 20:45 ` Per Bothner
  0 siblings, 1 reply; 2+ messages in thread
From: Jamison Hope @ 2015-08-13 19:30 UTC (permalink / raw)
  To: Kawa mailing list

[-- Attachment #1: Type: text/plain, Size: 1350 bytes --]

The attached patch fixes 2 bugs and provides 1 new feature:

1. When an annotation entry is read in via the readAnnotationEntry()
method of RuntimeAnnotationsAttr, it sets the entry's
annotationTypeIndex field, but not its annotationType field.  So then
later when the annotation is searched for later, we get a
NullPointerException in RuntimeAnnotationsAttr.getAnnotation() at this
line:

if (ann.getAnnotationType().getReflectClass() == clas) {

because getAnnotationType() returns null.

The first fix is to initialize the annotationType field when the entry
is read in.


2. When an annotation value is read in via the readAnnotationValue()
method of RuntimeAnnotationsAttr, if the value type is an enum constant,
it is stored as a String[] holding the names of the enum class and
field.  Later on, getAnnotation() returns a Proxy object that will
call AnnotationEntry.Value.getValue().  This is supposed to return the
appropriate enum constant, but instead returns the String[], leading to
a ClassCastException (I think it was).

The second fix is to fix Value.getValue() so that if it's of type 'e'
(enum), then convert the String[] into the actual enum constant.


3. The third change is an enhancement to give ClassType a
getAnnotation() method analogous to the ones in Field and Method.


--
Jamison Hope
The PTR Group
www.theptrgroup.com



[-- Attachment #2: gnu-bytecode-annotations.patch --]
[-- Type: application/octet-stream, Size: 3441 bytes --]

Index: gnu/bytecode/ChangeLog
===================================================================
--- gnu/bytecode/ChangeLog	(revision 8585)
+++ gnu/bytecode/ChangeLog	(working copy)
@@ -1,3 +1,11 @@
+2015-08-13  Jamison Hope  <jrh@theptrgroup.com>
+
+	* ClassType.java (getAnnotation): New method.
+	* RuntimeAnnotationsAttr.java (readAnnotationEntry): Set
+	annotationType when entry is read to avoid NullPointerException in
+	getAnnotation() later.
+	* AnnotationEntry.java (Value.getValue): Convert enum values.
+
 2015-06-04  Per Bothner  <per@bothner.com>
 
 	* PrimType.java (compare): Handle signed vs unsigned types.
Index: gnu/bytecode/ClassType.java
===================================================================
--- gnu/bytecode/ClassType.java	(revision 8585)
+++ gnu/bytecode/ClassType.java	(working copy)
@@ -1128,6 +1128,18 @@
     return result;
   }
 
+    public <T extends java.lang.annotation.Annotation>
+    T getAnnotation(Class<T> clas) {
+        T ann = RuntimeAnnotationsAttr.getAnnotation(this, clas);
+        if (ann != null)
+            return ann;
+        if ((flags & EXISTING_CLASS) != 0 && getReflectClass() != null) {
+            Class<?> c = getReflectClass();
+            return c.getAnnotation(clas);
+        }
+        return null;
+    }
+
   /** Do various fixups after generating code but before we can write it out.
    * This includes assigning constant pool indexes where needed,
    * finalizing labels, etc. */
Index: gnu/bytecode/RuntimeAnnotationsAttr.java
===================================================================
--- gnu/bytecode/RuntimeAnnotationsAttr.java	(revision 8585)
+++ gnu/bytecode/RuntimeAnnotationsAttr.java	(working copy)
@@ -129,6 +129,7 @@
         int tindex = dstr.readUnsignedShort();
         CpoolEntry cpentry = constants.getForced(tindex, ConstantPool.UTF8);
         aentry.annotationTypeIndex = tindex;
+        aentry.annotationType = (ClassType) Type.signatureToType(((CpoolUtf8)cpentry).getString());
         int count = dstr.readUnsignedShort();
         for (int i = 0;  i < count;  i++) {
             int nindex = dstr.readUnsignedShort();
Index: gnu/bytecode/AnnotationEntry.java
===================================================================
--- gnu/bytecode/AnnotationEntry.java	(revision 8585)
+++ gnu/bytecode/AnnotationEntry.java	(working copy)
@@ -310,6 +310,31 @@
             }
           return valuex;
         }
+      else if (kind == 'e') {
+          if (valuex == null) {
+              if (value instanceof Enum) {
+                  valuex = value;
+              } else {
+                  ClassType type;
+                  String name;
+
+                  if (value instanceof Field) {
+                      Field f = (Field) value;
+                      type = f.getDeclaringClass();
+                      name = f.getName();
+                  } else {
+                      String[] sarr = (String[]) value;
+                      type = (ClassType) Type.signatureToType(sarr[0]);
+                      name = sarr[1];
+                  }
+                  Class<?> clas = type.getReflectClass();
+                  Class<? extends Enum> eclas = clas.asSubclass(Enum.class);
+                  Enum val = Enum.valueOf(eclas, name);
+                  valuex = val;
+              }
+          }
+          return valuex;
+      }
       // FIXME other conversions needed?
       return value;
     }

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] three annotations-related fixes/enhancements
  2015-08-13 19:30 [PATCH] three annotations-related fixes/enhancements Jamison Hope
@ 2015-08-13 20:45 ` Per Bothner
  0 siblings, 0 replies; 2+ messages in thread
From: Per Bothner @ 2015-08-13 20:45 UTC (permalink / raw)
  To: kawa

On 08/13/2015 12:30 PM, Jamison Hope wrote:
> The attached patch fixes 2 bugs and provides 1 new feature:

Thanks!  I checked this in.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-08-13 20:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-13 19:30 [PATCH] three annotations-related fixes/enhancements Jamison Hope
2015-08-13 20:45 ` Per Bothner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).