1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| package com.jeantessier.classreader; |
34 |
| |
35 |
| import java.io.*; |
36 |
| import java.util.*; |
37 |
| |
38 |
| import junit.framework.*; |
39 |
| |
40 |
| public class TestSignatureAttribute extends TestCase { |
41 |
| public static final String TEST_GENERIC_CLASS_CLASS = "testgenericclass"; |
42 |
| public static final String TEST_GENERIC_METHODS_CLASS = "testgenericmethods"; |
43 |
| public static final String TEST_GENERIC_CLASS_FILENAME = "classes" + File.separator + TEST_GENERIC_CLASS_CLASS + ".class"; |
44 |
| public static final String TEST_GENERIC_METHODS_FILENAME = "classes" + File.separator + TEST_GENERIC_METHODS_CLASS + ".class"; |
45 |
| |
46 |
| private ClassfileLoader loader; |
47 |
| |
48 |
7
| protected void setUp() throws Exception {
|
49 |
7
| super.setUp();
|
50 |
| |
51 |
7
| loader = new AggregatingClassfileLoader();
|
52 |
| |
53 |
7
| loader.load(Collections.singleton(TEST_GENERIC_CLASS_FILENAME));
|
54 |
7
| loader.load(Collections.singleton(TEST_GENERIC_METHODS_FILENAME));
|
55 |
| } |
56 |
| |
57 |
1
| public void testClassDoesNotHaveSignatureAttribute() {
|
58 |
1
| Classfile nonGenericClass = loader.getClassfile(TEST_GENERIC_METHODS_CLASS);
|
59 |
| |
60 |
1
| Signature_attribute signatureAttribute = findSingleSignatureAttribute(nonGenericClass.getAttributes());
|
61 |
1
| assertNull("Found Signature attribute: " + signatureAttribute, signatureAttribute);
|
62 |
1
| assertFalse("Classfile with Signature attribute is not generic", nonGenericClass.isGeneric());
|
63 |
| } |
64 |
| |
65 |
1
| public void testClassHasSignatureAttribute() {
|
66 |
1
| Classfile genericClass = loader.getClassfile(TEST_GENERIC_CLASS_CLASS);
|
67 |
| |
68 |
1
| Signature_attribute signatureAttribute = findSingleSignatureAttribute(genericClass.getAttributes());
|
69 |
1
| assertNotNull("Signature attribute missing", signatureAttribute);
|
70 |
1
| assertEquals("Signature", "<T:Ljava/lang/Object;>Ljava/lang/Object;", signatureAttribute.getSignature());
|
71 |
1
| assertTrue("Classfile with Signature attribute is not generic", genericClass.isGeneric());
|
72 |
| } |
73 |
| |
74 |
1
| public void testConstructorHasSignatureAttribute() {
|
75 |
1
| Classfile genericClass = loader.getClassfile(TEST_GENERIC_CLASS_CLASS);
|
76 |
1
| Method_info method = genericClass.getMethod(TEST_GENERIC_CLASS_CLASS + "(java.lang.Object)");
|
77 |
| |
78 |
1
| Signature_attribute signatureAttribute = findSingleSignatureAttribute(method.getAttributes());
|
79 |
1
| assertNotNull("Signature attribute missing", signatureAttribute);
|
80 |
1
| assertEquals("Signature", "(TT;)V", signatureAttribute.getSignature());
|
81 |
1
| assertTrue("Method with Signature attribute is not generic", method.isGeneric());
|
82 |
| } |
83 |
| |
84 |
1
| public void testGenericConstructorHasSignatureAttribute() {
|
85 |
1
| Classfile genericClass = loader.getClassfile(TEST_GENERIC_METHODS_CLASS);
|
86 |
1
| Method_info method = genericClass.getMethod(TEST_GENERIC_METHODS_CLASS + "(java.lang.Object)");
|
87 |
| |
88 |
1
| Signature_attribute signatureAttribute = findSingleSignatureAttribute(method.getAttributes());
|
89 |
1
| assertNotNull("Signature attribute missing", signatureAttribute);
|
90 |
1
| assertEquals("Signature", "<T:Ljava/lang/Object;>(TT;)V", signatureAttribute.getSignature());
|
91 |
1
| assertTrue("Method with Signature attribute is not generic", method.isGeneric());
|
92 |
| } |
93 |
| |
94 |
1
| public void testMethodHasSignatureAttribute() {
|
95 |
1
| Classfile genericClass = loader.getClassfile(TEST_GENERIC_CLASS_CLASS);
|
96 |
1
| Method_info method = genericClass.getMethod("testmethod(java.lang.Object)");
|
97 |
| |
98 |
1
| Signature_attribute signatureAttribute = findSingleSignatureAttribute(method.getAttributes());
|
99 |
1
| assertNotNull("Signature attribute missing", signatureAttribute);
|
100 |
1
| assertEquals("Signature", "(TT;)TT;", signatureAttribute.getSignature());
|
101 |
1
| assertTrue("Method with Signature attribute is not generic", method.isGeneric());
|
102 |
| } |
103 |
| |
104 |
1
| public void testGenericMethodHasSignatureAttribute() {
|
105 |
1
| Classfile genericClass = loader.getClassfile(TEST_GENERIC_METHODS_CLASS);
|
106 |
1
| Method_info method = genericClass.getMethod("testmethod(java.lang.Object)");
|
107 |
| |
108 |
1
| Signature_attribute signatureAttribute = findSingleSignatureAttribute(method.getAttributes());
|
109 |
1
| assertNotNull("Signature attribute missing", signatureAttribute);
|
110 |
1
| assertEquals("Signature", "<T:Ljava/lang/Object;>(TT;)TT;", signatureAttribute.getSignature());
|
111 |
1
| assertTrue("Method with Signature attribute is not generic", method.isGeneric());
|
112 |
| } |
113 |
| |
114 |
1
| public void testFieldHasSignatureAttribute() {
|
115 |
1
| Classfile genericClass = loader.getClassfile(TEST_GENERIC_CLASS_CLASS);
|
116 |
1
| Field_info field = genericClass.getField("testfield");
|
117 |
| |
118 |
1
| Signature_attribute signatureAttribute = findSingleSignatureAttribute(field.getAttributes());
|
119 |
1
| assertNotNull("Signature attribute missing", signatureAttribute);
|
120 |
1
| assertEquals("Signature", "TT;", signatureAttribute.getSignature());
|
121 |
1
| assertTrue("Field with Signature attribute is not generic", field.isGeneric());
|
122 |
| } |
123 |
| |
124 |
7
| private Signature_attribute findSingleSignatureAttribute(Collection<? extends Attribute_info> attributes) {
|
125 |
7
| Signature_attribute result = null;
|
126 |
| |
127 |
7
| for (Attribute_info attribute : attributes) {
|
128 |
14
| if (attribute instanceof Signature_attribute) {
|
129 |
6
| if (result == null) {
|
130 |
6
| result = (Signature_attribute) attribute;
|
131 |
| } else { |
132 |
0
| fail("Multiple Signature attributes");
|
133 |
| } |
134 |
| } |
135 |
| } |
136 |
| |
137 |
7
| return result;
|
138 |
| } |
139 |
| } |