Skip to content

Commit fa6f194

Browse files
JEP 181 - Nest based access control - Code review driven cleanup (eclipse-jdt#4197)
1 parent 10c6669 commit fa6f194

File tree

6 files changed

+63
-98
lines changed

6 files changed

+63
-98
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ClassFile.java

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public class ClassFile implements TypeConstants, TypeIds {
104104
// that collection contains all the remaining bytes of the .class file
105105
public int headerOffset;
106106
public Map<TypeBinding, Boolean> innerClassesBindings;
107-
public Set<SourceTypeBinding> nestMembers;
108107
public List<Object> bootstrapMethods = null;
109108
public int methodCount;
110109
public int methodCountOffset;
@@ -124,7 +123,6 @@ public class ClassFile implements TypeConstants, TypeIds {
124123
public static final int INITIAL_CONTENTS_SIZE = 400;
125124
public static final int INITIAL_HEADER_SIZE = 1500;
126125
public static final int INNER_CLASSES_SIZE = 5;
127-
public static final int NESTED_MEMBER_SIZE = 5;
128126

129127
// TODO: Move these to an enum?
130128
public static final String ALTMETAFACTORY_STRING = new String(ConstantPool.ALTMETAFACTORY);
@@ -402,8 +400,7 @@ public int compare(ReferenceBinding o1, ReferenceBinding o2) {
402400
attributesNumber += generateTypeAnnotationAttributeForTypeDeclaration();
403401

404402
if (this.targetJDK >= ClassFileConstants.JDK11) {
405-
// add nestMember and nestHost attributes
406-
attributesNumber += generateNestAttributes();
403+
attributesNumber += generateNestAttributes(); // add nestMember and nestHost attributes
407404
}
408405
if (this.targetJDK >= ClassFileConstants.JDK16) {
409406
attributesNumber += generateRecordAttribute();
@@ -2683,8 +2680,8 @@ private int generateNestHostAttribute() {
26832680
if (nestHost == null)
26842681
return 0;
26852682
int localContentsOffset = this.contentsOffset;
2686-
if (localContentsOffset + 10 >= this.contents.length) {
2687-
resizeContents(10);
2683+
if (localContentsOffset + 8 >= this.contents.length) {
2684+
resizeContents(8);
26882685
}
26892686
int nestHostAttributeNameIndex =
26902687
this.constantPool.literalIndex(AttributeNamesConstants.NestHost);
@@ -2705,12 +2702,19 @@ private int generateNestHostAttribute() {
27052702
}
27062703
private int generateNestMembersAttribute() {
27072704

2708-
int localContentsOffset = this.contentsOffset;
2709-
List<String> nestedMembers = getNestMembers();
2705+
Set<SourceTypeBinding> nestMembers = this.referenceBinding.getNestMembers();
2706+
if (nestMembers == null )
2707+
return 0;
2708+
List<String> nestedMembers = nestMembers
2709+
.stream()
2710+
.map(s -> new String(s.constantPoolName()))
2711+
.sorted()
2712+
.collect(Collectors.toList());
27102713
int numberOfNestedMembers = nestedMembers != null ? nestedMembers.size() : 0;
2711-
if (numberOfNestedMembers == 0) // JVMS 11 4.7.29 says "at most one" NestMembers attribute - return if none.
2714+
if (numberOfNestedMembers == 0)
27122715
return 0;
27132716

2717+
int localContentsOffset = this.contentsOffset;
27142718
int exSize = 8 + 2 * numberOfNestedMembers;
27152719
if (exSize + localContentsOffset >= this.contents.length) {
27162720
resizeContents(exSize);
@@ -2737,7 +2741,7 @@ private int generateNestMembersAttribute() {
27372741
return 1;
27382742
}
27392743
private int generateNestAttributes() {
2740-
int nAttrs = generateNestMembersAttribute(); //either member or host will exist 4.7.29
2744+
int nAttrs = generateNestMembersAttribute(); // either member or host will exist 4.7.29
27412745
nAttrs += generateNestHostAttribute();
27422746
return nAttrs;
27432747
}
@@ -5950,25 +5954,6 @@ public void recordInnerClasses(TypeBinding binding, boolean onBottomForBug445231
59505954
enclosingType = enclosingType.enclosingType();
59515955
}
59525956
}
5953-
public void recordNestMember(SourceTypeBinding binding) {
5954-
SourceTypeBinding nestHost = binding != null ? binding.getNestHost() : null;
5955-
if (nestHost != null && !binding.equals(nestHost)) {// member
5956-
if (this.nestMembers == null) {
5957-
this.nestMembers = new HashSet<>(NESTED_MEMBER_SIZE);
5958-
}
5959-
this.nestMembers.add(binding);
5960-
}
5961-
}
5962-
public List<String> getNestMembers() {
5963-
if (this.nestMembers == null)
5964-
return null;
5965-
List<String> list = this.nestMembers
5966-
.stream()
5967-
.map(s -> new String(s.constantPoolName()))
5968-
.sorted()
5969-
.collect(Collectors.toList());
5970-
return list;
5971-
}
59725957

59735958
public int recordBootstrapMethod(FunctionalExpression expression) {
59745959
if (this.bootstrapMethods == null) {
@@ -6071,9 +6056,6 @@ public void reset(/*@Nullable*/SourceTypeBinding typeBinding, CompilerOptions op
60716056
if (this.innerClassesBindings != null) {
60726057
this.innerClassesBindings.clear();
60736058
}
6074-
if (this.nestMembers != null) {
6075-
this.nestMembers.clear();
6076-
}
60776059
if (this.bootstrapMethods != null) {
60786060
this.bootstrapMethods.clear();
60796061
}

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,7 @@ public void generateCode(ClassFile enclosingClassFile) {
609609
enclosingClassFile.recordInnerClasses(this.binding);
610610
classFile.recordInnerClasses(this.binding);
611611
}
612-
SourceTypeBinding nestHost = this.binding.getNestHost();
613-
if (nestHost != null && !TypeBinding.equalsEquals(nestHost, this.binding)) {
614-
ClassFile ocf = enclosingClassFile.outerMostEnclosingClassFile();
615-
if (ocf != null)
616-
ocf.recordNestMember(this.binding);
617-
}
612+
618613
TypeVariableBinding[] typeVariables = this.binding.typeVariables();
619614
for (TypeVariableBinding typeVariableBinding : typeVariables) {
620615
if ((typeVariableBinding.tagBits & TagBits.ContainsNestedTypeReferences) != 0) {
@@ -1469,7 +1464,7 @@ public void resolve() {
14691464
reporter.close();
14701465
}
14711466
}
1472-
updateNestHost();
1467+
updateNestRelations();
14731468
FieldDeclaration[] fieldsDecls = this.fields;
14741469
if (fieldsDecls != null) {
14751470
for (FieldDeclaration fieldDeclaration : fieldsDecls)
@@ -1795,19 +1790,34 @@ void updateMaxFieldCount() {
17951790
}
17961791
}
17971792

1798-
private SourceTypeBinding findNestHost() {
1799-
ClassScope classScope = this.scope.enclosingTopMostClassScope();
1800-
return classScope != null ? classScope.referenceContext.binding : null;
1801-
}
1793+
private final void updateNestRelations() {
18021794

1803-
void updateNestHost() {
1804-
if (this.binding == null)
1805-
return;
1806-
SourceTypeBinding nestHost = findNestHost();
1807-
if (nestHost != null && !this.binding.equals(nestHost)) {// member
1795+
ClassScope outerMostClassScope = this.scope;
1796+
Scope skope = this.scope;
1797+
1798+
while (skope != null && skope.kind != Scope.COMPILATION_UNIT_SCOPE) {
1799+
switch (skope.kind) {
1800+
case Scope.METHOD_SCOPE :
1801+
ReferenceContext context = ((MethodScope) skope).referenceContext;
1802+
if (context instanceof LambdaExpression lambdaExpression) {
1803+
if (lambdaExpression != lambdaExpression.original) // transient unreal universe.
1804+
return;
1805+
}
1806+
break;
1807+
case Scope.CLASS_SCOPE:
1808+
outerMostClassScope = (ClassScope) skope;
1809+
break;
1810+
}
1811+
skope = skope.parent;
1812+
}
1813+
1814+
SourceTypeBinding nestHost = outerMostClassScope != null ? outerMostClassScope.referenceContext.binding : null;
1815+
if (nestHost != null && !this.binding.equals(nestHost)) {
18081816
this.binding.setNestHost(nestHost);
1817+
nestHost.addNestMember(this.binding);
18091818
}
18101819
}
1820+
18111821
public boolean isPackageInfo() {
18121822
return CharOperation.equals(this.name, TypeConstants.PACKAGE_INFO_NAME);
18131823
}

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/classfmt/ClassFileReader.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -501,28 +501,6 @@ public ClassFileReader(byte[] classFileBytes, char[] fileName, boolean fullyInit
501501
this.moduleName = this.moduleDeclaration.name();
502502
}
503503
break;
504-
case 'N' :
505-
if (CharOperation.equals(attributeName, AttributeNamesConstants.NestHost)) {
506-
utf8Offset =
507-
this.constantPoolOffsets[u2At(this.constantPoolOffsets[u2At(readOffset + 6)] + 1)];
508-
@SuppressWarnings("unused")
509-
char[] nestHos = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
510-
} else if (CharOperation.equals(attributeName, AttributeNamesConstants.NestMembers)) {
511-
int offset = readOffset + 6;
512-
int nestMembersCount = u2At(offset);
513-
if (nestMembersCount != 0) {
514-
offset += 2;
515-
/** unused */
516-
char[][] nestMember = new char[nestMembersCount][];
517-
for (int j = 0; j < nestMembersCount; j++) {
518-
utf8Offset =
519-
this.constantPoolOffsets[u2At(this.constantPoolOffsets[u2At(offset)] + 1)];
520-
nestMember[j] = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
521-
offset += 2;
522-
}
523-
}
524-
}
525-
break;
526504
case 'P' :
527505
if (CharOperation.equals(attributeName, AttributeNamesConstants.PermittedSubclasses)) {
528506
int offset = readOffset + 6;

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,14 +1421,13 @@ public boolean isRecord() {
14211421
}
14221422

14231423
private static SourceTypeBinding getSourceTypeBinding(ReferenceBinding ref) {
1424-
if (ref instanceof SourceTypeBinding)
1425-
return (SourceTypeBinding) ref;
1426-
if (ref instanceof ParameterizedTypeBinding) {
1427-
ParameterizedTypeBinding ptb = (ParameterizedTypeBinding) ref;
1428-
return ptb.type instanceof SourceTypeBinding ? (SourceTypeBinding) ptb.type : null;
1429-
}
1424+
if (ref instanceof SourceTypeBinding stb)
1425+
return stb;
1426+
if (ref instanceof ParameterizedTypeBinding ptb)
1427+
return ptb.type instanceof SourceTypeBinding stb ? stb : null;
14301428
return null;
14311429
}
1430+
14321431
public boolean isNestmateOf(ReferenceBinding other) {
14331432
SourceTypeBinding s1 = getSourceTypeBinding(this);
14341433
SourceTypeBinding s2 = getSourceTypeBinding(other);

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,15 +1141,6 @@ public ClassScope enclosingInstanceScope() {
11411141
}
11421142
}
11431143

1144-
public final ClassScope enclosingTopMostClassScope() {
1145-
Scope scope = this;
1146-
while (scope != null) {
1147-
Scope t = scope.parent;
1148-
if (t instanceof CompilationUnitScope) break;
1149-
scope = t;
1150-
}
1151-
return scope instanceof ClassScope ? ((ClassScope) scope) : null;
1152-
}
11531144
public final MethodScope enclosingMethodScope() {
11541145
Scope scope = this;
11551146
while ((scope = scope.parent) != null) {
@@ -4908,14 +4899,14 @@ private ReferenceBinding[] getFilteredExceptions(MethodBinding method) {
49084899
}
49094900

49104901
public final ClassScope outerMostClassScope() {
4911-
ClassScope lastClassScope = null;
4902+
ClassScope outerMostClassScope = null;
49124903
Scope scope = this;
49134904
do {
4914-
if (scope instanceof ClassScope)
4915-
lastClassScope = (ClassScope) scope;
4905+
if (scope instanceof ClassScope classScope)
4906+
outerMostClassScope = classScope;
49164907
scope = scope.parent;
49174908
} while (scope != null);
4918-
return lastClassScope; // may answer null if no class around
4909+
return outerMostClassScope; // may answer null if no class around
49194910
}
49204911

49214912
public final MethodScope outerMostMethodScope() {

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,7 @@
5555
*******************************************************************************/
5656
package org.eclipse.jdt.internal.compiler.lookup;
5757

58-
import java.util.ArrayList;
59-
import java.util.Arrays;
60-
import java.util.Collection;
61-
import java.util.Comparator;
62-
import java.util.HashMap;
63-
import java.util.Iterator;
64-
import java.util.LinkedHashMap;
65-
import java.util.List;
66-
import java.util.Map;
58+
import java.util.*;
6759
import org.eclipse.jdt.core.compiler.CharOperation;
6860
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
6961
import org.eclipse.jdt.internal.compiler.ast.*;
@@ -112,6 +104,7 @@ public class SourceTypeBinding extends ReferenceBinding {
112104
public ExternalAnnotationProvider externalAnnotationProvider;
113105

114106
private SourceTypeBinding nestHost;
107+
private Set<SourceTypeBinding> nestMembers;
115108

116109
public boolean isImplicit = false;
117110
public boolean supertypeAnnotationsUpdated = false; // have any supertype annotations been updated during CompleteTypeBindingsSteps.INTEGRATE_ANNOTATIONS_IN_HIERARCHY?
@@ -2850,6 +2843,18 @@ public SourceTypeBinding getNestHost() {
28502843
return this.nestHost;
28512844
}
28522845

2846+
public Set<SourceTypeBinding> getNestMembers() {
2847+
return this.nestMembers;
2848+
}
2849+
2850+
public void addNestMember(SourceTypeBinding member) {
2851+
if (!member.equals(this)) {
2852+
if (this.nestMembers == null)
2853+
this.nestMembers = new HashSet<>(6);
2854+
this.nestMembers.add(member);
2855+
}
2856+
}
2857+
28532858
public void setNestHost(SourceTypeBinding nestHost) {
28542859
this.nestHost = nestHost;
28552860
}

0 commit comments

Comments
 (0)