Skip to content

Commit b6e0254

Browse files
Various cleanups for Records 2.0 (#3972)
* Incorporate several self-review comments from #3925
1 parent bd12606 commit b6e0254

File tree

11 files changed

+28
-52
lines changed

11 files changed

+28
-52
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5043,7 +5043,7 @@ private int generateMethodParameters(final MethodBinding binding) {
50435043
}
50445044

50455045
boolean needSynthetics = isCanonicalConstructor ? false : // WYSIWYG
5046-
isConstructor && !isCanonicalConstructor && declaringClass.isNestedType();
5046+
isConstructor && declaringClass.isNestedType();
50475047
if (needSynthetics) {
50485048
// Take into account the synthetic argument names
50495049
// This tracks JLS8, paragraph 8.8.9

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,15 @@ public void bindArguments() {
185185
this.binding.parameters[i] = argument.bind(this.scope, this.binding.parameters[i], used);
186186
param_i_Annotations = argument.annotations != null ? argument.binding.getAnnotations() : null;
187187
} else {
188-
this.scope.addLocalVariable(new LocalVariableBinding(methodArgument.name, methodArgument.type.resolvedType, methodArgument.modifiers, true)); // LVB is not annotated - FIXME
188+
final LocalVariableBinding implicitArgument = new LocalVariableBinding(methodArgument.name, methodArgument.type.resolvedType, methodArgument.modifiers, true);
189+
this.scope.addLocalVariable(implicitArgument);
189190
List<AnnotationBinding> relevantAnnotationBindings = new ArrayList<>();
190191
ASTNode.getRelevantAnnotations(methodArgument.annotations, TagBits.AnnotationForParameter, relevantAnnotationBindings);
191192
param_i_Annotations = relevantAnnotationBindings.toArray(new AnnotationBinding[0]);
193+
if (param_i_Annotations != null && param_i_Annotations.length > 0) {
194+
implicitArgument.setAnnotations(param_i_Annotations, this.scope, true);
195+
implicitArgument.extendedTagBits |= ExtendedTagBits.AllAnnotationsResolved;
196+
}
192197
}
193198

194199
if (param_i_Annotations != null && param_i_Annotations.length > 0) {

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,8 @@ public TypeBinding createBinding(MethodScope scope, TypeBinding typeBinding) {
7979
}
8080
if ((this.binding.extendedTagBits & ExtendedTagBits.AnnotationResolved) == 0) {
8181
Annotation[] annots = this.annotations;
82-
long sourceLevel = scope.compilerOptions().sourceLevel;
83-
if (sourceLevel >= ClassFileConstants.JDK14 && annots == null) {
84-
annots = ASTNode.copyRecordComponentAnnotations(scope,
85-
this.binding, annots);
86-
}
8782
if (annots != null)
8883
resolveAnnotations(scope, annots, this.binding, true);
89-
9084
Annotation.isTypeUseCompatible(this.type, scope, annots);
9185
scope.validateNullAnnotation(this.binding.tagBits, this.type, annots);
9286
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public void resolve(ClassScope scope) {
235235
// @param tags
236236
int paramTagsSize = this.paramReferences == null ? 0 : this.paramReferences.length;
237237
for (int i = 0; i < paramTagsSize; i++) {
238-
if(scope.referenceContext.nRecordComponents > 0) {
238+
if(scope.referenceContext.recordComponents.length > 0) {
239239
break;
240240
}
241241
JavadocSingleNameReference param = this.paramReferences[i];

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public class TypeDeclaration extends Statement implements ProblemSeverities, Ref
103103

104104
// 16 Records support
105105
public RecordComponent[] recordComponents = NO_RECORD_COMPONENTS;
106-
public int nRecordComponents;
107106
public static Set<String> disallowedComponentNames;
108107

109108
// 17 Sealed Type support
@@ -1172,7 +1171,7 @@ public StringBuilder printHeader(int indent, StringBuilder output) {
11721171
output.append(this.name);
11731172
if (this.isRecord()) {
11741173
output.append('(');
1175-
for (int i = 0; i < this.nRecordComponents; i++) {
1174+
for (int i = 0, length = this.recordComponents.length; i < length; i++) {
11761175
if (i > 0) output.append(", "); //$NON-NLS-1$
11771176
output.append(this.recordComponents[i].type.getTypeName()[0]);
11781177
output.append(' ');

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,7 @@ public SyntheticMethodBinding addSyntheticCanonicalConstructor() {
723723
SyntheticMethodBinding canonicalConstructor = new SyntheticMethodBinding(this, this.components);
724724
SyntheticMethodBinding[] accessors = new SyntheticMethodBinding[2];
725725
this.synthetics[SourceTypeBinding.METHOD_EMUL].put(TypeConstants.INIT, accessors);
726-
accessors[0] = canonicalConstructor;
727-
resolveTypesFor(canonicalConstructor);
728-
return canonicalConstructor;
726+
return accessors[0] = canonicalConstructor;
729727
}
730728
/* Add a new synthetic component accessor for the record class */
731729
public SyntheticMethodBinding addSyntheticRecordComponentAccessor(RecordComponentBinding rcb) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,6 @@ public SyntheticMethodBinding(SourceTypeBinding declaringClass, RecordComponentB
463463
this.extendedTagBits |= ExtendedTagBits.IsCanonicalConstructor;
464464
this.parameters = rcb.length == 0 ? Binding.NO_PARAMETERS : new TypeBinding[rcb.length];
465465
this.parameterNames = rcb.length == 0 ? Binding.NO_PARAMETER_NAMES : new char[rcb.length][];
466-
this.modifiers |= ExtraCompilerModifiers.AccUnresolved;
467466
AnnotationBinding[][] paramAnnotations = null;
468467
for (int i = 0, length = rcb.length; i < length; i++) {
469468
this.parameters[i] = rcb[i].type;

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
import java.util.LinkedList;
4747
import java.util.List;
4848
import java.util.Properties;
49-
import java.util.function.Consumer;
50-
import java.util.function.Predicate;
5149
import java.util.stream.Stream;
5250
import org.eclipse.jdt.core.compiler.CharOperation;
5351
import org.eclipse.jdt.core.compiler.InvalidInputException;
@@ -169,7 +167,6 @@ protected enum SwitchRuleKind {
169167
protected static final int HALT = 0; // halt and throw up hands.
170168
protected static final int RESTART = 1; // stacks adjusted, alternate goal from check point.
171169
protected static final int RESUME = 2; // stacks untouched, just continue from where left off.
172-
private static final short TYPE_CLASS = 1;
173170

174171
public Scanner scanner;
175172
public TerminalToken currentToken = TokenNameNotAToken;
@@ -10283,7 +10280,6 @@ protected void consumeRecordComponentHeaderRightParen() {
1028310280
0,
1028410281
length);
1028510282
typeDecl.recordComponents = recComps;
10286-
typeDecl.nRecordComponents = recComps.length;
1028710283
} else {
1028810284
typeDecl.recordComponents = ASTNode.NO_RECORD_COMPONENTS;
1028910285
}
@@ -12186,16 +12182,12 @@ public void parse(MethodDeclaration md, CompilationUnitDeclaration unit) {
1218612182
}
1218712183
}
1218812184
public ASTNode[] parseClassBodyDeclarations(char[] source, int offset, int length, CompilationUnitDeclaration unit) {
12189-
/* automaton initialization */
12190-
initialize();
12191-
goForClassBodyDeclarations();
12192-
return parseBodyDeclarations(source, offset, length, unit, TYPE_CLASS);
12193-
}
12194-
12195-
private ASTNode[] parseBodyDeclarations(char[] source, int offset, int length, CompilationUnitDeclaration unit, short classRecordType) {
1219612185
boolean oldDiet = this.diet;
1219712186
int oldInt = this.dietInt;
1219812187
boolean oldTolerateDefaultClassMethods = this.tolerateDefaultClassMethods;
12188+
/* automaton initialization */
12189+
initialize();
12190+
goForClassBodyDeclarations();
1219912191
/* scanner initialization */
1220012192
this.scanner.setSource(source);
1220112193
this.scanner.resetTo(offset, offset + length - 1);
@@ -12235,24 +12227,19 @@ private ASTNode[] parseBodyDeclarations(char[] source, int offset, int length, C
1223512227
if (!this.options.performMethodsFullRecovery && !this.options.performStatementsRecovery) {
1223612228
return null;
1223712229
}
12238-
// collect all body declaration inside the compilation unit except the default constructor and implicit methods and fields for records
12230+
// collect all body declaration inside the compilation unit except the default constructor
1223912231
final List bodyDeclarations = new ArrayList();
12240-
unit.ignoreFurtherInvestigation = false;
12241-
Predicate<MethodDeclaration> methodPred = classRecordType == TYPE_CLASS ?
12242-
mD -> !mD.isDefaultConstructor() : mD -> true;
12243-
Consumer<FieldDeclaration> fieldAction = classRecordType == TYPE_CLASS ?
12244-
fD -> bodyDeclarations.add(fD) : fD -> { bodyDeclarations.add(fD);} ;
1224512232
ASTVisitor visitor = new ASTVisitor() {
1224612233
@Override
1224712234
public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
12248-
if (methodPred.test(methodDeclaration)) {
12235+
if (!methodDeclaration.isDefaultConstructor()) {
1224912236
bodyDeclarations.add(methodDeclaration);
1225012237
}
1225112238
return false;
1225212239
}
1225312240
@Override
1225412241
public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {
12255-
fieldAction.accept(fieldDeclaration);
12242+
bodyDeclarations.add(fieldDeclaration);
1225612243
return false;
1225712244
}
1225812245
@Override
@@ -12261,6 +12248,7 @@ public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {
1226112248
return false;
1226212249
}
1226312250
};
12251+
unit.ignoreFurtherInvestigation = false;
1226412252
unit.traverse(visitor, unit.scope);
1226512253
unit.ignoreFurtherInvestigation = true;
1226612254
result = (ASTNode[]) bodyDeclarations.toArray(new ASTNode[bodyDeclarations.size()]);

org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionOnJavadocTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void filterPossibleTags(Scope scope) {
144144
switch (scope.kind) {
145145
case Scope.CLASS_SCOPE:
146146
TypeDeclaration typeDecl = ((ClassScope)scope).referenceContext;
147-
boolean isRecordWithComponent = typeDecl.isRecord() && typeDecl.nRecordComponents >0 ;
147+
boolean isRecordWithComponent = typeDecl.isRecord() && typeDecl.recordComponents.length > 0;
148148
if (((ClassScope)scope).referenceContext.binding.isGenericType() || isRecordWithComponent) {
149149
filteredTags[size++] = possibleTag;
150150
}

org.eclipse.jdt.core/model/org/eclipse/jdt/internal/compiler/parser/SourceTypeConverter.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ private TypeDeclaration convert(SourceType typeHandle, CompilationResult compila
516516
// Hence, use the one that does
517517
type.modifiers |= ExtraCompilerModifiers.AccRecord;
518518
IField[] recordComponents = typeHandle.getRecordComponents();
519-
type.recordComponents = new RecordComponent[type.nRecordComponents = recordComponents.length];
519+
type.recordComponents = new RecordComponent[recordComponents.length];
520520
for(int i = 0; i < recordComponents.length; i++) {
521521
type.recordComponents[i] = convertRecordComponents((SourceField)recordComponents[i], type, compilationResult);
522522
}
@@ -602,21 +602,10 @@ private TypeDeclaration convert(SourceType typeHandle, CompilationResult compila
602602
initializers = typeInfo.getInitializers();
603603
initializerCount = initializers.length;
604604
}
605-
SourceField[] sourceFields = null;
605+
IField[] sourceFields = null;
606606
int sourceFieldCount = 0;
607607
if ((this.flags & FIELD) != 0) {
608-
SourceField[] allFields = typeInfo.getFieldHandles();
609-
if (type.isRecord()) {
610-
int staticFieldCount = 0;
611-
for (int i = 0, length = allFields.length; i < length; i++) {
612-
SourceFieldElementInfo elementInfo = (SourceFieldElementInfo) allFields[i].getElementInfo();
613-
if ((elementInfo.getModifiers() & ClassFileConstants.AccStatic) != 0)
614-
allFields[staticFieldCount++] = allFields[i];
615-
}
616-
System.arraycopy(allFields, 0, sourceFields = new SourceField[staticFieldCount], 0, staticFieldCount);
617-
} else {
618-
sourceFields = allFields;
619-
}
608+
sourceFields = typeHandle.getFields();
620609
sourceFieldCount = sourceFields.length;
621610
}
622611
int length = initializerCount + sourceFieldCount;
@@ -627,7 +616,7 @@ private TypeDeclaration convert(SourceType typeHandle, CompilationResult compila
627616
}
628617
int index = 0;
629618
for (int i = initializerCount; i < length; i++) {
630-
type.fields[i] = convert(sourceFields[index++], type, compilationResult);
619+
type.fields[i] = convert((SourceField) sourceFields[index++], type, compilationResult);
631620
}
632621
}
633622

0 commit comments

Comments
 (0)