6983646: javap should identify why a DefaultAttribute is being used
authorvromero
Sat, 29 Jun 2013 20:12:24 +0100
changeset 18667 7c871925691f
parent 18666 59ab10272fb5
child 18668 1f99c1b43ab2
6983646: javap should identify why a DefaultAttribute is being used Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/classfile/Attribute.java
langtools/src/share/classes/com/sun/tools/classfile/DefaultAttribute.java
langtools/src/share/classes/com/sun/tools/javap/AttributeWriter.java
--- a/langtools/src/share/classes/com/sun/tools/classfile/Attribute.java	Fri Jun 28 19:47:54 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/classfile/Attribute.java	Sat Jun 29 20:12:24 2013 +0100
@@ -77,10 +77,12 @@
 
         public Attribute createAttribute(ClassReader cr, int name_index, byte[] data)
                 throws IOException {
-            if (standardAttributes == null)
+            if (standardAttributes == null) {
                 init();
+            }
 
             ConstantPool cp = cr.getConstantPool();
+            String reasonForDefaultAttr;
             try {
                 String name = cp.getUTF8Value(name_index);
                 Class<? extends Attribute> attrClass = standardAttributes.get(name);
@@ -90,14 +92,18 @@
                         Constructor<? extends Attribute> constr = attrClass.getDeclaredConstructor(constrArgTypes);
                         return constr.newInstance(new Object[] { cr, name_index, data.length });
                     } catch (Throwable t) {
+                        reasonForDefaultAttr = t.toString();
                         // fall through and use DefaultAttribute
                         // t.printStackTrace();
                     }
+                } else {
+                    reasonForDefaultAttr = "unknown attribute";
                 }
             } catch (ConstantPoolException e) {
+                reasonForDefaultAttr = e.toString();
                 // fall through and use DefaultAttribute
             }
-            return new DefaultAttribute(cr, name_index, data);
+            return new DefaultAttribute(cr, name_index, data, reasonForDefaultAttr);
         }
 
         protected void init() {
--- a/langtools/src/share/classes/com/sun/tools/classfile/DefaultAttribute.java	Fri Jun 28 19:47:54 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/classfile/DefaultAttribute.java	Sat Jun 29 20:12:24 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,13 +33,24 @@
  */
 public class DefaultAttribute extends Attribute {
     DefaultAttribute(ClassReader cr, int name_index, byte[] data) {
+        this(cr, name_index, data, null);
+    }
+
+    DefaultAttribute(ClassReader cr, int name_index, byte[] data, String reason) {
         super(name_index, data.length);
         info = data;
+        this.reason = reason;
     }
 
     public DefaultAttribute(ConstantPool constant_pool, int name_index, byte[] info) {
+        this(constant_pool, name_index, info, null);
+    }
+
+    public DefaultAttribute(ConstantPool constant_pool, int name_index,
+            byte[] info, String reason) {
         super(name_index, info.length);
         this.info = info;
+        this.reason = reason;
     }
 
     public <R, P> R accept(Visitor<R, P> visitor, P p) {
@@ -47,4 +58,7 @@
     }
 
     public final byte[] info;
+    /** Why did we need to generate a DefaultAttribute
+     */
+    public final String reason;
 }
--- a/langtools/src/share/classes/com/sun/tools/javap/AttributeWriter.java	Fri Jun 28 19:47:54 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javap/AttributeWriter.java	Sat Jun 29 20:12:24 2013 +0100
@@ -114,6 +114,9 @@
     }
 
     public Void visitDefault(DefaultAttribute attr, Void ignore) {
+        if (attr.reason != null) {
+            report(attr.reason);
+        }
         byte[] data = attr.info;
         int i = 0;
         int j = 0;