426N/A/*
553N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
426N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
426N/A *
426N/A * This code is free software; you can redistribute it and/or modify it
426N/A * under the terms of the GNU General Public License version 2 only, as
426N/A * published by the Free Software Foundation.
426N/A *
426N/A * This code is distributed in the hope that it will be useful, but WITHOUT
426N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
426N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
426N/A * version 2 for more details (a copy is included in the LICENSE file that
426N/A * accompanied this code).
426N/A *
426N/A * You should have received a copy of the GNU General Public License version
426N/A * 2 along with this work; if not, write to the Free Software Foundation,
426N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
426N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
426N/A */
426N/A
426N/Aimport java.io.*;
426N/Aimport java.net.*;
426N/Aimport java.util.*;
426N/Aimport com.sun.tools.classfile.*;
426N/Aimport com.sun.tools.classfile.Type.ArrayType;
426N/Aimport com.sun.tools.classfile.Type.ClassSigType;
426N/Aimport com.sun.tools.classfile.Type.ClassType;
426N/Aimport com.sun.tools.classfile.Type.MethodType;
426N/Aimport com.sun.tools.classfile.Type.SimpleType;
426N/Aimport com.sun.tools.classfile.Type.TypeParamType;
426N/Aimport com.sun.tools.classfile.Type.WildcardType;
426N/A
426N/A/*
426N/A * @test
426N/A * @bug 6888367
426N/A * @summary classfile library parses signature attributes incorrectly
426N/A */
426N/A
426N/A/*
426N/A * This test is a pretty detailed test both of javac signature generation and classfile
426N/A * signature parsing. The first part of the test tests all the examples given in the
426N/A * second part of the test. Each example comes with one or two annotations, @Desc, @Sig,
426N/A * for the descriptor and signature of the annotated declaration. Annotations are
426N/A * provided whenever the annotated item is expected to have a corresponding value.
426N/A * Each annotation has two argument values. The first arg is the expected value of the
426N/A * descriptor/signature as found in the class file. This value is mostly for documentation
426N/A * purposes in reading the test. The second value is the rendering of the descriptor or
426N/A * signature using a custom Type visitor that explicitly includes an indication of the
426N/A * Type classes being used to represent the descriptor/signature. Thus we test
426N/A * that the descriptor/signature is being parsed into the expected type tree structure.
426N/A */
426N/Apublic class T6888367 {
426N/A
426N/A public static void main(String... args) throws Exception {
426N/A new T6888367().run();
426N/A }
426N/A
426N/A public void run() throws Exception {
426N/A ClassFile cf = getClassFile("Test");
426N/A
426N/A testFields(cf);
426N/A testMethods(cf);
426N/A testInnerClasses(cf); // recursive
426N/A
426N/A if (errors > 0)
426N/A throw new Exception(errors + " errors found");
426N/A }
426N/A
426N/A void testFields(ClassFile cf) throws Exception {
426N/A String cn = cf.getName();
426N/A ConstantPool cp = cf.constant_pool;
426N/A for (Field f: cf.fields) {
426N/A test("field " + cn + "." + f.getName(cp), f.descriptor, f.attributes, cp);
426N/A }
426N/A }
426N/A
426N/A void testMethods(ClassFile cf) throws Exception {
426N/A String cn = cf.getName();
426N/A ConstantPool cp = cf.constant_pool;
426N/A for (Method m: cf.methods) {
426N/A test("method " + cn + "." + m.getName(cp), m.descriptor, m.attributes, cp);
426N/A }
426N/A }
426N/A
426N/A void testInnerClasses(ClassFile cf) throws Exception {
426N/A ConstantPool cp = cf.constant_pool;
426N/A InnerClasses_attribute ic =
426N/A (InnerClasses_attribute) cf.attributes.get(Attribute.InnerClasses);
426N/A for (InnerClasses_attribute.Info info: ic.classes) {
426N/A String outerClassName = cp.getClassInfo(info.outer_class_info_index).getName();
426N/A if (!outerClassName.equals(cf.getName())) {
426N/A continue;
426N/A }
426N/A String innerClassName = cp.getClassInfo(info.inner_class_info_index).getName();
426N/A ClassFile icf = getClassFile(innerClassName);
426N/A test("class " + innerClassName, null, icf.attributes, icf.constant_pool);
426N/A testInnerClasses(icf);
426N/A }
426N/A }
426N/A
426N/A void test(String name, Descriptor desc, Attributes attrs, ConstantPool cp)
426N/A throws Exception {
426N/A AnnotValues d = getDescValue(attrs, cp);
426N/A AnnotValues s = getSigValue(attrs, cp);
426N/A if (d == null && s == null) // not a test field or method if no @Desc or @Sig given
426N/A return;
426N/A
426N/A System.err.println(name);
426N/A
426N/A if (desc != null) {
426N/A System.err.println(" descriptor: " + desc.getValue(cp));
426N/A checkEqual(d.raw, desc.getValue(cp));
426N/A Type dt = new Signature(desc.index).getType(cp);
426N/A checkEqual(d.type, tp.print(dt));
426N/A }
426N/A
426N/A Signature_attribute sa = (Signature_attribute) attrs.get(Attribute.Signature);
426N/A if (sa != null)
426N/A System.err.println(" signature: " + sa.getSignature(cp));
426N/A
426N/A if (s != null || sa != null) {
426N/A if (s != null && sa != null) {
426N/A checkEqual(s.raw, sa.getSignature(cp));
426N/A Type st = new Signature(sa.signature_index).getType(cp);
426N/A checkEqual(s.type, tp.print(st));
426N/A } else if (s != null)
426N/A error("@Sig annotation found but not Signature attribute");
426N/A else
426N/A error("Signature attribute found but no @Sig annotation");
426N/A }
426N/A
426N/A System.err.println();
426N/A }
426N/A
426N/A
426N/A ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
426N/A URL url = getClass().getResource(name + ".class");
426N/A InputStream in = url.openStream();
426N/A try {
426N/A return ClassFile.read(in);
426N/A } finally {
426N/A in.close();
426N/A }
426N/A }
426N/A
426N/A AnnotValues getDescValue(Attributes attrs, ConstantPool cp) throws Exception {
426N/A return getAnnotValues(Desc.class.getName(), attrs, cp);
426N/A }
426N/A
426N/A AnnotValues getSigValue(Attributes attrs, ConstantPool cp) throws Exception {
426N/A return getAnnotValues(Sig.class.getName(), attrs, cp);
426N/A }
426N/A
426N/A static class AnnotValues {
426N/A AnnotValues(String raw, String type) {
426N/A this.raw = raw;
426N/A this.type = type;
426N/A }
426N/A final String raw;
426N/A final String type;
426N/A }
426N/A
426N/A AnnotValues getAnnotValues(String annotName, Attributes attrs, ConstantPool cp)
426N/A throws Exception {
426N/A RuntimeInvisibleAnnotations_attribute annots =
426N/A (RuntimeInvisibleAnnotations_attribute)attrs.get(Attribute.RuntimeInvisibleAnnotations);
426N/A if (annots != null) {
426N/A for (Annotation a: annots.annotations) {
426N/A if (cp.getUTF8Value(a.type_index).equals("L" + annotName + ";")) {
426N/A Annotation.Primitive_element_value pv0 =
426N/A (Annotation.Primitive_element_value) a.element_value_pairs[0].value;
426N/A Annotation.Primitive_element_value pv1 =
426N/A (Annotation.Primitive_element_value) a.element_value_pairs[1].value;
426N/A return new AnnotValues(
426N/A cp.getUTF8Value(pv0.const_value_index),
426N/A cp.getUTF8Value(pv1.const_value_index));
426N/A }
426N/A }
426N/A }
426N/A return null;
426N/A
426N/A }
426N/A
426N/A void checkEqual(String expect, String found) {
426N/A if (!(expect == null ? found == null : expect.equals(found))) {
426N/A System.err.println("expected: " + expect);
426N/A System.err.println(" found: " + found);
426N/A error("unexpected values found");
426N/A }
426N/A }
426N/A
426N/A void error(String msg) {
426N/A System.err.println("error: " + msg);
426N/A errors++;
426N/A }
426N/A
426N/A int errors;
426N/A
426N/A TypePrinter tp = new TypePrinter();
426N/A
426N/A class TypePrinter implements Type.Visitor<String,Void> {
426N/A String print(Type t) {
426N/A return t == null ? null : t.accept(this, null);
426N/A }
426N/A String print(String pre, List<? extends Type> ts, String post) {
426N/A if (ts == null)
426N/A return null;
426N/A StringBuilder sb = new StringBuilder();
426N/A sb.append(pre);
426N/A String sep = "";
426N/A for (Type t: ts) {
426N/A sb.append(sep);
426N/A sb.append(print(t));
426N/A sep = ",";
426N/A }
426N/A sb.append(post);
426N/A return sb.toString();
426N/A }
426N/A
426N/A public String visitSimpleType(SimpleType type, Void p) {
426N/A return "S{" + type.name + "}";
426N/A }
426N/A
426N/A public String visitArrayType(ArrayType type, Void p) {
426N/A return "A{" + print(type.elemType) + "}";
426N/A }
426N/A
426N/A public String visitMethodType(MethodType type, Void p) {
426N/A StringBuilder sb = new StringBuilder();
426N/A sb.append("M{");
426N/A if (type.typeParamTypes != null)
426N/A sb.append(print("<", type.typeParamTypes, ">"));
426N/A sb.append(print(type.returnType));
426N/A sb.append(print("(", type.paramTypes, ")"));
426N/A if (type.throwsTypes != null)
426N/A sb.append(print("", type.throwsTypes, ""));
426N/A sb.append("}");
426N/A return sb.toString();
426N/A }
426N/A
426N/A public String visitClassSigType(ClassSigType type, Void p) {
426N/A StringBuilder sb = new StringBuilder();
426N/A sb.append("CS{");
426N/A if (type.typeParamTypes != null)
426N/A sb.append(print("<", type.typeParamTypes, ">"));
426N/A sb.append(print(type.superclassType));
426N/A if (type.superinterfaceTypes != null)
426N/A sb.append(print("i(", type.superinterfaceTypes, ")"));
426N/A sb.append("}");
426N/A return sb.toString();
426N/A }
426N/A
426N/A public String visitClassType(ClassType type, Void p) {
426N/A StringBuilder sb = new StringBuilder();
426N/A sb.append("C{");
426N/A if (type.outerType != null) {
426N/A sb.append(print(type.outerType));
426N/A sb.append(".");
426N/A }
426N/A sb.append(type.name);
426N/A if (type.typeArgs != null)
426N/A sb.append(print("<", type.typeArgs, ">"));
426N/A sb.append("}");
426N/A return sb.toString();
426N/A }
426N/A
426N/A public String visitTypeParamType(TypeParamType type, Void p) {
426N/A StringBuilder sb = new StringBuilder();
426N/A sb.append("TA{");
426N/A sb.append(type.name);
426N/A if (type.classBound != null) {
426N/A sb.append(":c");
426N/A sb.append(print(type.classBound));
426N/A }
426N/A if (type.interfaceBounds != null)
426N/A sb.append(print(":i", type.interfaceBounds, ""));
426N/A sb.append("}");
426N/A return sb.toString();
426N/A }
426N/A
426N/A public String visitWildcardType(WildcardType type, Void p) {
426N/A switch (type.kind) {
426N/A case UNBOUNDED:
426N/A return "W{?}";
426N/A case EXTENDS:
426N/A return "W{e," + print(type.boundType) + "}";
426N/A case SUPER:
426N/A return "W{s," + print(type.boundType) + "}";
426N/A default:
426N/A throw new AssertionError();
426N/A }
426N/A }
426N/A
426N/A };
426N/A}
426N/A
426N/A
426N/A@interface Desc {
426N/A String d();
426N/A String t();
426N/A}
426N/A
426N/A@interface Sig {
426N/A String s();
426N/A String t();
426N/A}
426N/A
426N/Aclass Clss { }
426N/Ainterface Intf { }
426N/Aclass GenClss<T> { }
426N/A
426N/Aclass Test {
426N/A // fields
426N/A
426N/A @Desc(d="Z", t="S{boolean}")
426N/A boolean z;
426N/A
426N/A @Desc(d="B", t="S{byte}")
426N/A byte b;
426N/A
426N/A @Desc(d="C", t="S{char}")
426N/A char c;
426N/A
426N/A @Desc(d="D", t="S{double}")
426N/A double d;
426N/A
426N/A @Desc(d="F", t="S{float}")
426N/A float f;
426N/A
426N/A @Desc(d="I", t="S{int}")
426N/A int i;
426N/A
426N/A @Desc(d="J", t="S{long}")
426N/A long l;
426N/A
426N/A @Desc(d="S", t="S{short}")
426N/A short s;
426N/A
426N/A @Desc(d="LClss;", t="C{Clss}")
426N/A Clss clss;
426N/A
426N/A @Desc(d="LIntf;", t="C{Intf}")
426N/A Intf intf;
426N/A
426N/A @Desc(d="[I", t="A{S{int}}")
426N/A int[] ai;
426N/A
426N/A @Desc(d="[LClss;", t="A{C{Clss}}")
426N/A Clss[] aClss;
426N/A
426N/A @Desc(d="LGenClss;", t="C{GenClss}")
426N/A @Sig(s="LGenClss<LClss;>;", t="C{GenClss<C{Clss}>}")
426N/A GenClss<Clss> genClass;
426N/A
426N/A // methods, return types
426N/A
426N/A @Desc(d="()V", t="M{S{void}()}")
426N/A void mv0() { }
426N/A
426N/A @Desc(d="()I", t="M{S{int}()}")
426N/A int mi0() { return 0; }
426N/A
426N/A @Desc(d="()LClss;", t="M{C{Clss}()}")
426N/A Clss mclss0() { return null; }
426N/A
426N/A @Desc(d="()[I", t="M{A{S{int}}()}")
426N/A int[] mai0() { return null; }
426N/A
426N/A @Desc(d="()[LClss;", t="M{A{C{Clss}}()}")
426N/A Clss[] maClss0() { return null; }
426N/A
426N/A @Desc(d="()LGenClss;", t="M{C{GenClss}()}")
426N/A @Sig(s="()LGenClss<LClss;>;", t="M{C{GenClss<C{Clss}>}()}")
426N/A GenClss<Clss> mgenClss0() { return null; }
426N/A
426N/A @Desc(d="()LGenClss;", t="M{C{GenClss}()}")
426N/A @Sig(s="()LGenClss<*>;", t="M{C{GenClss<W{?}>}()}")
426N/A GenClss<?> mgenClssW0() { return null; }
426N/A
426N/A @Desc(d="()LGenClss;", t="M{C{GenClss}()}")
426N/A @Sig(s="()LGenClss<+LClss;>;", t="M{C{GenClss<W{e,C{Clss}}>}()}")
426N/A GenClss<? extends Clss> mgenClssWExtClss0() { return null; }
426N/A
426N/A @Desc(d="()LGenClss;", t="M{C{GenClss}()}")
426N/A @Sig(s="()LGenClss<-LClss;>;", t="M{C{GenClss<W{s,C{Clss}}>}()}")
426N/A GenClss<? super Clss> mgenClssWSupClss0() { return null; }
426N/A
426N/A @Desc(d="()Ljava/lang/Object;", t="M{C{java/lang/Object}()}")
426N/A @Sig(s="<T:Ljava/lang/Object;>()TT;", t="M{<TA{T:cC{java/lang/Object}}>S{T}()}")
426N/A <T> T mt0() { return null; }
426N/A
426N/A @Desc(d="()LGenClss;", t="M{C{GenClss}()}")
426N/A @Sig(s="<T:Ljava/lang/Object;>()LGenClss<+TT;>;",
426N/A t="M{<TA{T:cC{java/lang/Object}}>C{GenClss<W{e,S{T}}>}()}")
426N/A <T> GenClss<? extends T> mgenClssWExtT0() { return null; }
426N/A
426N/A @Desc(d="()LGenClss;", t="M{C{GenClss}()}")
426N/A @Sig(s="<T:Ljava/lang/Object;>()LGenClss<-TT;>;", t="M{<TA{T:cC{java/lang/Object}}>C{GenClss<W{s,S{T}}>}()}")
426N/A <T> GenClss<? super T> mgenClssWSupT0() { return null; }
426N/A
426N/A // methods, arg types
426N/A
426N/A @Desc(d="(I)V", t="M{S{void}(S{int})}")
426N/A void mi1(int arg) { }
426N/A
426N/A @Desc(d="(LClss;)V", t="M{S{void}(C{Clss})}")
426N/A void mclss1(Clss arg) { }
426N/A
426N/A @Desc(d="([I)V", t="M{S{void}(A{S{int}})}")
426N/A void mai1(int[] arg) { }
426N/A
426N/A @Desc(d="([LClss;)V", t="M{S{void}(A{C{Clss}})}")
426N/A void maClss1(Clss[] arg) { }
426N/A
426N/A @Desc(d="(LGenClss;)V", t="M{S{void}(C{GenClss})}")
426N/A @Sig(s="(LGenClss<LClss;>;)V", t="M{S{void}(C{GenClss<C{Clss}>})}")
426N/A void mgenClss1(GenClss<Clss> arg) { }
426N/A
426N/A @Desc(d="(LGenClss;)V", t="M{S{void}(C{GenClss})}")
426N/A @Sig(s="(LGenClss<*>;)V", t="M{S{void}(C{GenClss<W{?}>})}")
426N/A void mgenClssW1(GenClss<?> arg) { }
426N/A
426N/A @Desc(d="(LGenClss;)V", t="M{S{void}(C{GenClss})}")
426N/A @Sig(s="(LGenClss<+LClss;>;)V", t="M{S{void}(C{GenClss<W{e,C{Clss}}>})}")
426N/A void mgenClssWExtClss1(GenClss<? extends Clss> arg) { }
426N/A
426N/A @Desc(d="(LGenClss;)V", t="M{S{void}(C{GenClss})}")
426N/A @Sig(s="(LGenClss<-LClss;>;)V", t="M{S{void}(C{GenClss<W{s,C{Clss}}>})}")
426N/A void mgenClssWSupClss1(GenClss<? super Clss> arg) { }
426N/A
426N/A @Desc(d="(Ljava/lang/Object;)V", t="M{S{void}(C{java/lang/Object})}")
426N/A @Sig(s="<T:Ljava/lang/Object;>(TT;)V",
426N/A t="M{<TA{T:cC{java/lang/Object}}>S{void}(S{T})}")
426N/A <T> void mt1(T arg) { }
426N/A
426N/A @Desc(d="(LGenClss;)V", t="M{S{void}(C{GenClss})}")
426N/A @Sig(s="<T:Ljava/lang/Object;>(LGenClss<+TT;>;)V",
426N/A t="M{<TA{T:cC{java/lang/Object}}>S{void}(C{GenClss<W{e,S{T}}>})}")
426N/A <T> void mgenClssWExtT1(GenClss<? extends T> arg) { }
426N/A
426N/A @Desc(d="(LGenClss;)V", t="M{S{void}(C{GenClss})}")
426N/A @Sig(s="<T:Ljava/lang/Object;>(LGenClss<-TT;>;)V",
426N/A t="M{<TA{T:cC{java/lang/Object}}>S{void}(C{GenClss<W{s,S{T}}>})}")
426N/A <T> void mgenClssWSupT1(GenClss<? super T> arg) { }
426N/A
426N/A // methods, throws
426N/A
426N/A @Desc(d="()V", t="M{S{void}()}")
426N/A void m_E() throws Exception { }
426N/A
426N/A @Desc(d="()V", t="M{S{void}()}")
426N/A @Sig(s="<T:Ljava/lang/Throwable;>()V^TT;",
426N/A t="M{<TA{T:cC{java/lang/Throwable}}>S{void}()S{T}}")
426N/A <T extends Throwable> void m_T() throws T { }
426N/A
426N/A // inner classes
426N/A
426N/A static class X {
426N/A // no sig
426N/A class P { }
426N/A
426N/A @Sig(s="<TQ:Ljava/lang/Object;>LTest$X$P;",
426N/A t="CS{<TA{TQ:cC{java/lang/Object}}>C{Test$X$P}}")
426N/A class Q<TQ> extends P { }
426N/A
426N/A @Sig(s="<TR:Ljava/lang/Object;>LTest$X$Q<TTR;>;",
426N/A t="CS{<TA{TR:cC{java/lang/Object}}>C{Test$X$Q<S{TR}>}}")
426N/A class R<TR> extends Q<TR> { }
426N/A }
426N/A
426N/A @Sig(s="<TY:Ljava/lang/Object;>Ljava/lang/Object;",
426N/A t="CS{<TA{TY:cC{java/lang/Object}}>C{java/lang/Object}}")
426N/A static class Y<TY> {
426N/A // no sig
426N/A class P { }
426N/A
426N/A @Sig(s="<TQ:Ljava/lang/Object;>LTest$Y<TTY;>.P;",
426N/A t="CS{<TA{TQ:cC{java/lang/Object}}>C{C{Test$Y<S{TY}>}.P}}")
426N/A class Q<TQ> extends P { }
426N/A
426N/A @Sig(s="<TR:Ljava/lang/Object;>LTest$Y<TTY;>.Q<TTR;>;",
426N/A t="CS{<TA{TR:cC{java/lang/Object}}>C{C{Test$Y<S{TY}>}.Q<S{TR}>}}")
426N/A class R<TR> extends Q<TR> {
426N/A // no sig
426N/A class R1 { }
426N/A
426N/A @Sig(s="<TR2:Ljava/lang/Object;>LTest$Y<TTY;>.R<TTR;>.R1;",
426N/A t="CS{<TA{TR2:cC{java/lang/Object}}>C{C{C{Test$Y<S{TY}>}.R<S{TR}>}.R1}}")
426N/A class R2<TR2> extends R1 { }
426N/A }
426N/A
426N/A @Sig(s="LTest$Y<TTY;>.Q<TTY;>;", t="C{C{Test$Y<S{TY}>}.Q<S{TY}>}")
426N/A class S extends Q<TY> {
426N/A // no sig
426N/A class S1 { }
426N/A
426N/A @Sig(s="<TS2:Ljava/lang/Object;>LTest$Y<TTY;>.S.S1;",
426N/A t="CS{<TA{TS2:cC{java/lang/Object}}>C{C{C{Test$Y<S{TY}>}.S}.S1}}")
426N/A class S2<TS2> extends S1 { }
426N/A
426N/A @Sig(s="LTest$Y<TTY;>.S.S2<TTY;>;",
426N/A t="C{C{C{Test$Y<S{TY}>}.S}.S2<S{TY}>}")
426N/A class S3 extends S2<TY> { }
426N/A }
426N/A }
426N/A}
426N/A
426N/A