public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
From: Jamison Hope <jrh@theptrgroup.com>
To: Kawa mailing list <kawa@sourceware.org>
Subject: [PATCH] three annotations-related fixes/enhancements
Date: Thu, 13 Aug 2015 19:30:00 -0000	[thread overview]
Message-ID: <26455D3A-3751-4667-A0AD-4E99AD145A88@theptrgroup.com> (raw)

[-- 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;
     }

             reply	other threads:[~2015-08-13 19:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-13 19:30 Jamison Hope [this message]
2015-08-13 20:45 ` Per Bothner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=26455D3A-3751-4667-A0AD-4E99AD145A88@theptrgroup.com \
    --to=jrh@theptrgroup.com \
    --cc=kawa@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).