Skip to content

Commit 18e97aa

Browse files
Ravi ReddyRob McKenna
authored andcommitted
8322883: [BACKOUT] 8225377: type annotations are not visible to javac plugins across compilation boundaries
Reviewed-by: coffeys, robm
1 parent 60b6a66 commit 18e97aa

File tree

3 files changed

+0
-444
lines changed

3 files changed

+0
-444
lines changed

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

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

4341
import javax.lang.model.element.Modifier;
4442
import javax.lang.model.element.NestingKind;
@@ -2257,316 +2255,12 @@ public void run() {
22572255
currentClassFile = classFile;
22582256
List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
22592257
sym.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
2260-
addTypeAnnotationsToSymbol(sym, newList);
22612258
} finally {
22622259
currentClassFile = previousClassFile;
22632260
}
22642261
}
22652262
}
22662263

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

25712265
/************************************************************************
25722266
* Reading Symbols

0 commit comments

Comments
 (0)