Skip to content

Commit 4216546

Browse files
cushonjerboaa
authored andcommitted
8341779: [REDO BACKPORT] type annotations are not visible to javac plugins across compilation boundaries (JDK-8225377)
Reviewed-by: vromero
1 parent 04c4241 commit 4216546

File tree

2 files changed

+302
-0
lines changed

2 files changed

+302
-0
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import java.util.HashSet;
3737
import java.util.Map;
3838
import java.util.Set;
39+
import java.util.function.BiFunction;
3940
import java.util.function.IntFunction;
41+
import java.util.function.Predicate;
4042

4143
import javax.lang.model.element.Modifier;
4244
import javax.lang.model.element.NestingKind;
@@ -2255,12 +2257,311 @@ public void run() {
22552257
currentClassFile = classFile;
22562258
List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
22572259
sym.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
2260+
addTypeAnnotationsToSymbol(sym, newList);
22582261
} finally {
22592262
currentClassFile = previousClassFile;
22602263
}
22612264
}
22622265
}
22632266

2267+
/**
2268+
* Rewrites types in the given symbol to include type annotations.
2269+
*
2270+
* <p>The list of type annotations includes annotations for all types in the signature of the
2271+
* symbol. Associating the annotations with the correct type requires interpreting the JVMS
2272+
* 4.7.20-A target_type to locate the correct type to rewrite, and then interpreting the JVMS
2273+
* 4.7.20.2 type_path to associate the annotation with the correct contained type.
2274+
*/
2275+
private static void addTypeAnnotationsToSymbol(
2276+
Symbol s, List<Attribute.TypeCompound> attributes) {
2277+
new TypeAnnotationSymbolVisitor(attributes).visit(s, null);
2278+
}
2279+
2280+
private static class TypeAnnotationSymbolVisitor
2281+
extends Types.DefaultSymbolVisitor<Void, Void> {
2282+
2283+
private final List<Attribute.TypeCompound> attributes;
2284+
2285+
private TypeAnnotationSymbolVisitor(List<Attribute.TypeCompound> attributes) {
2286+
this.attributes = attributes;
2287+
}
2288+
2289+
@Override
2290+
public Void visitClassSymbol(Symbol.ClassSymbol s, Void unused) {
2291+
ClassType t = (ClassType) s.type;
2292+
int i = 0;
2293+
ListBuffer<Type> interfaces = new ListBuffer<>();
2294+
for (Type itf : t.interfaces_field) {
2295+
interfaces.add(addTypeAnnotations(itf, classExtends(i++)));
2296+
}
2297+
t.interfaces_field = interfaces.toList();
2298+
t.supertype_field = addTypeAnnotations(t.supertype_field, classExtends(65535));
2299+
if (t.typarams_field != null) {
2300+
t.typarams_field =
2301+
rewriteTypeParameters(
2302+
t.typarams_field, TargetType.CLASS_TYPE_PARAMETER_BOUND);
2303+
}
2304+
return null;
2305+
}
2306+
2307+
@Override
2308+
public Void visitMethodSymbol(Symbol.MethodSymbol s, Void unused) {
2309+
Type t = s.type;
2310+
if (t.hasTag(TypeTag.FORALL)) {
2311+
Type.ForAll fa = (Type.ForAll) t;
2312+
fa.tvars = rewriteTypeParameters(fa.tvars, TargetType.METHOD_TYPE_PARAMETER_BOUND);
2313+
t = fa.qtype;
2314+
}
2315+
MethodType mt = (MethodType) t;
2316+
ListBuffer<Type> argtypes = new ListBuffer<>();
2317+
int i = 0;
2318+
for (Symbol.VarSymbol param : s.params) {
2319+
param.type = addTypeAnnotations(param.type, methodFormalParameter(i++));
2320+
argtypes.add(param.type);
2321+
}
2322+
mt.argtypes = argtypes.toList();
2323+
ListBuffer<Type> thrown = new ListBuffer<>();
2324+
i = 0;
2325+
for (Type thrownType : mt.thrown) {
2326+
thrown.add(addTypeAnnotations(thrownType, thrownType(i++)));
2327+
}
2328+
mt.thrown = thrown.toList();
2329+
mt.restype = addTypeAnnotations(mt.restype, TargetType.METHOD_RETURN);
2330+
if (mt.recvtype != null) {
2331+
mt.recvtype = addTypeAnnotations(mt.recvtype, TargetType.METHOD_RECEIVER);
2332+
}
2333+
return null;
2334+
}
2335+
2336+
@Override
2337+
public Void visitVarSymbol(Symbol.VarSymbol s, Void unused) {
2338+
s.type = addTypeAnnotations(s.type, TargetType.FIELD);
2339+
return null;
2340+
}
2341+
2342+
@Override
2343+
public Void visitSymbol(Symbol s, Void unused) {
2344+
return null;
2345+
}
2346+
2347+
private List<Type> rewriteTypeParameters(List<Type> tvars, TargetType boundType) {
2348+
ListBuffer<Type> tvarbuf = new ListBuffer<>();
2349+
int typeVariableIndex = 0;
2350+
for (Type tvar : tvars) {
2351+
Type bound = tvar.getUpperBound();
2352+
if (bound.isCompound()) {
2353+
ClassType ct = (ClassType) bound;
2354+
int boundIndex = 0;
2355+
if (ct.supertype_field != null) {
2356+
ct.supertype_field =
2357+
addTypeAnnotations(
2358+
ct.supertype_field,
2359+
typeParameterBound(
2360+
boundType, typeVariableIndex, boundIndex++));
2361+
}
2362+
ListBuffer<Type> itfbuf = new ListBuffer<>();
2363+
for (Type itf : ct.interfaces_field) {
2364+
itfbuf.add(
2365+
addTypeAnnotations(
2366+
itf,
2367+
typeParameterBound(
2368+
boundType, typeVariableIndex, boundIndex++)));
2369+
}
2370+
ct.interfaces_field = itfbuf.toList();
2371+
} else {
2372+
bound =
2373+
addTypeAnnotations(
2374+
bound,
2375+
typeParameterBound(
2376+
boundType,
2377+
typeVariableIndex,
2378+
bound.isInterface() ? 1 : 0));
2379+
}
2380+
((TypeVar) tvar).setUpperBound(bound);
2381+
tvarbuf.add(tvar);
2382+
typeVariableIndex++;
2383+
}
2384+
return tvarbuf.toList();
2385+
}
2386+
2387+
private Type addTypeAnnotations(Type type, TargetType targetType) {
2388+
return addTypeAnnotations(type, pos -> pos.type == targetType);
2389+
}
2390+
2391+
private Type addTypeAnnotations(Type type, Predicate<TypeAnnotationPosition> filter) {
2392+
Assert.checkNonNull(type);
2393+
2394+
// Find type annotations that match the given target type
2395+
ListBuffer<Attribute.TypeCompound> filtered = new ListBuffer<>();
2396+
for (Attribute.TypeCompound attribute : this.attributes) {
2397+
if (filter.test(attribute.position)) {
2398+
filtered.add(attribute);
2399+
}
2400+
}
2401+
if (filtered.isEmpty()) {
2402+
return type;
2403+
}
2404+
2405+
// Group the matching annotations by their type path. Each group of annotations will be
2406+
// added to a type at that location.
2407+
Map<List<TypeAnnotationPosition.TypePathEntry>, ListBuffer<Attribute.TypeCompound>>
2408+
attributesByPath = new HashMap<>();
2409+
for (Attribute.TypeCompound attribute : filtered.toList()) {
2410+
attributesByPath
2411+
.computeIfAbsent(attribute.position.location, k -> new ListBuffer<>())
2412+
.add(attribute);
2413+
}
2414+
2415+
// Search the structure of the type to find the contained types at each type path
2416+
Map<Type, List<Attribute.TypeCompound>> attributesByType = new HashMap<>();
2417+
new TypeAnnotationLocator(attributesByPath, attributesByType).visit(type, List.nil());
2418+
2419+
// Rewrite the type and add the annotations
2420+
type = new TypeAnnotationTypeMapping(attributesByType).visit(type, null);
2421+
Assert.check(attributesByType.isEmpty(), "Failed to apply annotations to types");
2422+
2423+
return type;
2424+
}
2425+
2426+
private static Predicate<TypeAnnotationPosition> typeParameterBound(
2427+
TargetType targetType, int parameterIndex, int boundIndex) {
2428+
return pos ->
2429+
pos.type == targetType
2430+
&& pos.parameter_index == parameterIndex
2431+
&& pos.bound_index == boundIndex;
2432+
}
2433+
2434+
private static Predicate<TypeAnnotationPosition> methodFormalParameter(int index) {
2435+
return pos ->
2436+
pos.type == TargetType.METHOD_FORMAL_PARAMETER && pos.parameter_index == index;
2437+
}
2438+
2439+
private static Predicate<TypeAnnotationPosition> thrownType(int index) {
2440+
return pos -> pos.type == TargetType.THROWS && pos.type_index == index;
2441+
}
2442+
2443+
private static Predicate<TypeAnnotationPosition> classExtends(int index) {
2444+
return pos -> pos.type == TargetType.CLASS_EXTENDS && pos.type_index == index;
2445+
}
2446+
}
2447+
2448+
/**
2449+
* Visit all contained types, assembling a type path to represent the current location, and
2450+
* record the types at each type path that need to be annotated.
2451+
*/
2452+
private static class TypeAnnotationLocator
2453+
extends Types.DefaultTypeVisitor<Void, List<TypeAnnotationPosition.TypePathEntry>> {
2454+
private final Map<List<TypeAnnotationPosition.TypePathEntry>,
2455+
ListBuffer<Attribute.TypeCompound>> attributesByPath;
2456+
private final Map<Type, List<Attribute.TypeCompound>> attributesByType;
2457+
2458+
private TypeAnnotationLocator(
2459+
Map<List<TypeAnnotationPosition.TypePathEntry>, ListBuffer<Attribute.TypeCompound>>
2460+
attributesByPath,
2461+
Map<Type, List<Attribute.TypeCompound>> attributesByType) {
2462+
this.attributesByPath = attributesByPath;
2463+
this.attributesByType = attributesByType;
2464+
}
2465+
2466+
@Override
2467+
public Void visitClassType(ClassType t, List<TypeAnnotationPosition.TypePathEntry> path) {
2468+
// As described in JVMS 4.7.20.2, type annotations on nested types are located with
2469+
// 'left-to-right' steps starting on 'the outermost part of the type for which a type
2470+
// annotation is admissible'. So the current path represents the outermost containing
2471+
// type of the type being visited, and we add type path steps for every contained nested
2472+
// type.
2473+
List<ClassType> enclosing = List.nil();
2474+
for (Type curr = t;
2475+
curr != null && curr != Type.noType;
2476+
curr = curr.getEnclosingType()) {
2477+
enclosing = enclosing.prepend((ClassType) curr);
2478+
}
2479+
for (ClassType te : enclosing) {
2480+
if (te.typarams_field != null) {
2481+
int i = 0;
2482+
for (Type typaram : te.typarams_field) {
2483+
visit(typaram, path.append(new TypeAnnotationPosition.TypePathEntry(
2484+
TypeAnnotationPosition.TypePathEntryKind.TYPE_ARGUMENT, i++)));
2485+
}
2486+
}
2487+
visitType(te, path);
2488+
path = path.append(TypeAnnotationPosition.TypePathEntry.INNER_TYPE);
2489+
}
2490+
return null;
2491+
}
2492+
2493+
@Override
2494+
public Void visitWildcardType(
2495+
WildcardType t, List<TypeAnnotationPosition.TypePathEntry> path) {
2496+
visit(t.type, path.append(TypeAnnotationPosition.TypePathEntry.WILDCARD));
2497+
return super.visitWildcardType(t, path);
2498+
}
2499+
2500+
@Override
2501+
public Void visitArrayType(ArrayType t, List<TypeAnnotationPosition.TypePathEntry> path) {
2502+
visit(t.elemtype, path.append(TypeAnnotationPosition.TypePathEntry.ARRAY));
2503+
return super.visitArrayType(t, path);
2504+
}
2505+
2506+
@Override
2507+
public Void visitType(Type t, List<TypeAnnotationPosition.TypePathEntry> path) {
2508+
ListBuffer<Attribute.TypeCompound> attributes = attributesByPath.remove(path);
2509+
if (attributes != null) {
2510+
attributesByType.put(t, attributes.toList());
2511+
}
2512+
return null;
2513+
}
2514+
}
2515+
2516+
/** A type mapping that rewrites the type to include type annotations. */
2517+
private static class TypeAnnotationTypeMapping extends Type.StructuralTypeMapping<Void> {
2518+
2519+
private final Map<Type, List<Attribute.TypeCompound>> attributesByType;
2520+
2521+
private TypeAnnotationTypeMapping(
2522+
Map<Type, List<Attribute.TypeCompound>> attributesByType) {
2523+
this.attributesByType = attributesByType;
2524+
}
2525+
2526+
private <T extends Type> Type reannotate(T t, BiFunction<T, Void, Type> f) {
2527+
// We're relying on object identify of Type instances to record where the annotations
2528+
// need to be added, so we have to retrieve the annotations for each type before
2529+
// rewriting it, and then add them after its contained types have been rewritten.
2530+
List<Attribute.TypeCompound> attributes = attributesByType.remove(t);
2531+
Type mapped = f.apply(t, null);
2532+
if (attributes == null) {
2533+
return mapped;
2534+
}
2535+
// Runtime-visible and -invisible annotations are completed separately, so if the same
2536+
// type has annotations from both it will get annotated twice.
2537+
TypeMetadata.Annotations existing = mapped.getMetadata(TypeMetadata.Annotations.class);
2538+
if (existing != null) {
2539+
existing.annotationBuffer().addAll(attributes);
2540+
return mapped;
2541+
}
2542+
return mapped.annotatedType(attributes);
2543+
}
2544+
2545+
@Override
2546+
public Type visitClassType(ClassType t, Void unused) {
2547+
return reannotate(t, super::visitClassType);
2548+
}
2549+
2550+
@Override
2551+
public Type visitWildcardType(WildcardType t, Void unused) {
2552+
return reannotate(t, super::visitWildcardType);
2553+
}
2554+
2555+
@Override
2556+
public Type visitArrayType(ArrayType t, Void unused) {
2557+
return reannotate(t, super::visitArrayType);
2558+
}
2559+
2560+
@Override
2561+
public Type visitType(Type t, Void unused) {
2562+
return reannotate(t, (x, u) -> x);
2563+
}
2564+
}
22642565

22652566
/************************************************************************
22662567
* Reading Symbols

test/langtools/tools/javac/processing/model/type/BasicAnnoTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* jdk.compiler/com.sun.tools.javac.util
3434
* @build JavacTestingAbstractProcessor DPrinter BasicAnnoTests
3535
* @compile/process -XDaccessInternalAPI -processor BasicAnnoTests -proc:only BasicAnnoTests.java
36+
* @compile/process -XDaccessInternalAPI -processor BasicAnnoTests -proc:only BasicAnnoTests
3637
*/
3738

3839
import java.io.PrintWriter;

0 commit comments

Comments
 (0)