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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
// Keep somewhat in sync with
// langtools/test/tools/javac/annotations/typeAnnotations/referenceinfos/ReferenceInfoUtil.java
// Adapted to handled the same type qualifier appearing multiple times.
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.Code_attribute;
import com.sun.tools.classfile.ConstantPool.InvalidIndex;
import com.sun.tools.classfile.ConstantPool.UnexpectedEntry;
import com.sun.tools.classfile.Field;
import com.sun.tools.classfile.Method;
import com.sun.tools.classfile.RuntimeTypeAnnotations_attribute;
import com.sun.tools.classfile.TypeAnnotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.checkerframework.javacutil.Pair;
import org.checkerframework.javacutil.PluginUtil;
public class ReferenceInfoUtil {
public static final int IGNORE_VALUE = -321;
public static List<TypeAnnotation> extendedAnnotationsOf(ClassFile cf) {
List<TypeAnnotation> annos = new ArrayList<>();
findAnnotations(cf, annos);
return annos;
}
/////////////////// Extract type annotations //////////////////
private static void findAnnotations(ClassFile cf, List<TypeAnnotation> annos) {
findAnnotations(cf, Attribute.RuntimeVisibleTypeAnnotations, annos);
findAnnotations(cf, Attribute.RuntimeInvisibleTypeAnnotations, annos);
for (Field f : cf.fields) {
findAnnotations(cf, f, annos);
}
for (Method m : cf.methods) {
findAnnotations(cf, m, annos);
}
}
private static void findAnnotations(ClassFile cf, Method m, List<TypeAnnotation> annos) {
findAnnotations(cf, m, Attribute.RuntimeVisibleTypeAnnotations, annos);
findAnnotations(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, annos);
}
private static void findAnnotations(ClassFile cf, Field m, List<TypeAnnotation> annos) {
findAnnotations(cf, m, Attribute.RuntimeVisibleTypeAnnotations, annos);
findAnnotations(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, annos);
}
/**
* Test the result of Attributes.getIndex according to expectations encoded in the method's
* name.
*/
private static void findAnnotations(ClassFile cf, String name, List<TypeAnnotation> annos) {
int index = cf.attributes.getIndex(cf.constant_pool, name);
if (index != -1) {
Attribute attr = cf.attributes.get(index);
assert attr instanceof RuntimeTypeAnnotations_attribute;
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute) attr;
annos.addAll(Arrays.asList(tAttr.annotations));
}
}
/**
* Test the result of Attributes.getIndex according to expectations encoded in the method's
* name.
*/
private static void findAnnotations(
ClassFile cf, Method m, String name, List<TypeAnnotation> annos) {
int index = m.attributes.getIndex(cf.constant_pool, name);
if (index != -1) {
Attribute attr = m.attributes.get(index);
assert attr instanceof RuntimeTypeAnnotations_attribute;
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute) attr;
annos.addAll(Arrays.asList(tAttr.annotations));
}
int cindex = m.attributes.getIndex(cf.constant_pool, Attribute.Code);
if (cindex != -1) {
Attribute cattr = m.attributes.get(cindex);
assert cattr instanceof Code_attribute;
Code_attribute cAttr = (Code_attribute) cattr;
index = cAttr.attributes.getIndex(cf.constant_pool, name);
if (index != -1) {
Attribute attr = cAttr.attributes.get(index);
assert attr instanceof RuntimeTypeAnnotations_attribute;
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute) attr;
annos.addAll(Arrays.asList(tAttr.annotations));
}
}
}
/**
* Test the result of Attributes.getIndex according to expectations encoded in the method's
* name.
*/
private static void findAnnotations(
ClassFile cf, Field m, String name, List<TypeAnnotation> annos) {
int index = m.attributes.getIndex(cf.constant_pool, name);
if (index != -1) {
Attribute attr = m.attributes.get(index);
assert attr instanceof RuntimeTypeAnnotations_attribute;
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute) attr;
annos.addAll(Arrays.asList(tAttr.annotations));
}
}
/////////////////////// Equality testing /////////////////////
private static boolean areEquals(int a, int b) {
return a == b || a == IGNORE_VALUE || b == IGNORE_VALUE;
}
private static boolean areEquals(int[] a, int[] a2) {
if (a == a2) {
return true;
}
if (a == null || a2 == null) {
return false;
}
int length = a.length;
if (a2.length != length) {
return false;
}
for (int i = 0; i < length; i++) {
if (areEquals(a[i], a2[i])) {
return false;
}
}
return true;
}
public static boolean areEquals(TypeAnnotation.Position p1, TypeAnnotation.Position p2) {
if (p1 == p2) {
return true;
}
if (p1 == null || p2 == null) {
return false;
}
boolean result =
((p1.type == p2.type)
&& (p1.location.equals(p2.location))
&& areEquals(p1.offset, p2.offset)
&& areEquals(p1.lvarOffset, p2.lvarOffset)
&& areEquals(p1.lvarLength, p2.lvarLength)
&& areEquals(p1.lvarIndex, p2.lvarIndex)
&& areEquals(p1.bound_index, p2.bound_index)
&& areEquals(p1.parameter_index, p2.parameter_index)
&& areEquals(p1.type_index, p2.type_index)
&& areEquals(p1.exception_index, p2.exception_index));
return result;
}
public static String positionCompareStr(
TypeAnnotation.Position p1, TypeAnnotation.Position p2) {
return PluginUtil.joinLines(
"type = " + p1.type + ", " + p2.type,
"offset = " + p1.offset + ", " + p2.offset,
"lvarOffset = " + p1.lvarOffset + ", " + p2.lvarOffset,
"lvarLength = " + p1.lvarLength + ", " + p2.lvarLength,
"lvarIndex = " + p1.lvarIndex + ", " + p2.lvarIndex,
"bound_index = " + p1.bound_index + ", " + p2.bound_index,
"parameter_index = " + p1.parameter_index + ", " + p2.parameter_index,
"type_index = " + p1.type_index + ", " + p2.type_index,
"exception_index = " + p1.exception_index + ", " + p2.exception_index,
"");
}
private static TypeAnnotation findAnnotation(
String name,
TypeAnnotation.Position expected,
List<TypeAnnotation> annotations,
ClassFile cf)
throws InvalidIndex, UnexpectedEntry {
String properName = "L" + name + ";";
for (TypeAnnotation anno : annotations) {
String actualName = cf.constant_pool.getUTF8Value(anno.annotation.type_index);
if (properName.equals(actualName)) {
System.out.println("For Anno: " + actualName);
}
if (properName.equals(actualName) && areEquals(expected, anno.position)) {
return anno;
}
}
return null;
}
public static boolean compare(
List<Pair<String, TypeAnnotation.Position>> expectedAnnos,
List<TypeAnnotation> actualAnnos,
ClassFile cf)
throws InvalidIndex, UnexpectedEntry {
if (actualAnnos.size() != expectedAnnos.size()) {
throw new ComparisonException(
"Wrong number of annotations", expectedAnnos, actualAnnos);
}
for (Pair<String, TypeAnnotation.Position> e : expectedAnnos) {
String aName = e.first;
TypeAnnotation.Position expected = e.second;
TypeAnnotation actual = findAnnotation(aName, expected, actualAnnos, cf);
if (actual == null) {
throw new ComparisonException(
"Expected annotation not found: " + aName + " position: " + expected,
expectedAnnos,
actualAnnos);
}
}
return true;
}
}
class ComparisonException extends RuntimeException {
private static final long serialVersionUID = -3930499712333815821L;
public final List<Pair<String, TypeAnnotation.Position>> expected;
public final List<TypeAnnotation> found;
public ComparisonException(
String message,
List<Pair<String, TypeAnnotation.Position>> expected,
List<TypeAnnotation> found) {
super(message);
this.expected = expected;
this.found = found;
}
public String toString() {
return PluginUtil.joinLines(
super.toString(),
"\tExpected: "
+ expected.size()
+ " annotations; but found: "
+ found.size()
+ " annotations",
" Expected: " + expected,
" Found: " + found);
}
}
|