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
|
// 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.Annotation;
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPool.InvalidIndex;
import com.sun.tools.classfile.ConstantPool.UnexpectedEntry;
import com.sun.tools.classfile.Method;
import com.sun.tools.classfile.RuntimeAnnotations_attribute;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import org.checkerframework.javacutil.PluginUtil;
public class ReferenceInfoUtil {
public static final int IGNORE_VALUE = -321;
public static List<Annotation> extendedAnnotationsOf(ClassFile cf) {
List<Annotation> annos = new ArrayList<>();
findAnnotations(cf, annos);
return annos;
}
/////////////////// Extract annotations //////////////////
private static void findAnnotations(ClassFile cf, List<Annotation> annos) {
for (Method m : cf.methods) {
findAnnotations(cf, m, Attribute.RuntimeVisibleAnnotations, annos);
}
}
/**
* 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<Annotation> annos) {
int index = m.attributes.getIndex(cf.constant_pool, name);
if (index != -1) {
Attribute attr = m.attributes.get(index);
assert attr instanceof RuntimeAnnotations_attribute;
RuntimeAnnotations_attribute tAttr = (RuntimeAnnotations_attribute) attr;
for (Annotation an : tAttr.annotations) {
if (!containsName(annos, an, cf)) {
annos.add(an);
}
}
}
}
private static Annotation findAnnotation(
String name, List<Annotation> annotations, ClassFile cf)
throws InvalidIndex, UnexpectedEntry {
String properName = "L" + name + ";";
for (Annotation anno : annotations) {
String actualName = cf.constant_pool.getUTF8Value(anno.type_index);
if (properName.equals(actualName)) {
return anno;
}
}
return null;
}
public static boolean compare(
List<String> expectedAnnos, List<Annotation> actualAnnos, ClassFile cf)
throws InvalidIndex, UnexpectedEntry {
if (actualAnnos.size() != expectedAnnos.size()) {
throw new ComparisonException(
"Wrong number of annotations", expectedAnnos, actualAnnos, cf);
}
for (String annoName : expectedAnnos) {
Annotation anno = findAnnotation(annoName, actualAnnos, cf);
if (anno == null) {
throw new ComparisonException(
"Expected annotation not found: " + annoName,
expectedAnnos,
actualAnnos,
cf);
}
}
return true;
}
private static boolean containsName(List<Annotation> annos, Annotation anno, ClassFile cf) {
try {
for (Annotation an : annos) {
if (cf.constant_pool
.getUTF8Value(an.type_index)
.equals(cf.constant_pool.getUTF8Value(anno.type_index))) {
return true;
}
}
} catch (Exception e) {
throw new RuntimeException();
}
return false;
}
}
class ComparisonException extends RuntimeException {
private static final long serialVersionUID = -3930499712333815821L;
public final List<String> expected;
public final List<Annotation> found;
public final ClassFile cf;
public ComparisonException(
String message, List<String> expected, List<Annotation> found, ClassFile cf) {
super(message);
this.expected = expected;
this.found = found;
this.cf = cf;
}
public String toString() {
StringJoiner foundString = new StringJoiner(",");
for (Annotation anno : found) {
try {
foundString.add(cf.constant_pool.getUTF8Value(anno.type_index));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return PluginUtil.joinLines(
super.toString(),
"\tExpected: "
+ expected.size()
+ " annotations; but found: "
+ found.size()
+ " annotations",
" Expected: " + expected,
" Found: " + foundString);
}
}
|