Skip to content

Commit 6798c4b

Browse files
Alternate strategy for creation of compact constructor argument bindings (#3975)
Tasks from [Records] Follow up tasks to #3896
1 parent b6e0254 commit 6798c4b

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/apt/model/ExecutableElementImpl.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
import javax.lang.model.type.TypeMirror;
2323
import org.eclipse.jdt.core.compiler.CharOperation;
2424
import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
25-
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
2625
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
2726
import org.eclipse.jdt.internal.compiler.ast.Argument;
28-
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
2927
import org.eclipse.jdt.internal.compiler.lookup.*;
3028

3129
public class ExecutableElementImpl extends ElementImpl implements
@@ -126,18 +124,11 @@ public List<? extends VariableElement> getParameters() {
126124
VariableElement param = new VariableElementImpl(this._env, argument.binding);
127125
params.add(param);
128126
}
129-
} else if (methodDeclaration.isCompactConstructor()) {
130-
RecordComponentBinding[] components = binding.declaringClass.components();
131-
for (int i = 0; i < length; i++) {
132-
List<AnnotationBinding> relevantAnnotations = new ArrayList<>();
133-
ASTNode.getRelevantAnnotations(components[i].sourceRecordComponent().annotations, TagBits.AnnotationForParameter, relevantAnnotations);
134-
LocalVariableBinding pi = new LocalVariableBinding(binding.parameterNames[i], binding.parameters[i], ClassFileConstants.AccDefault, true) {
135-
@Override
136-
public AnnotationBinding[] getAnnotations() {
137-
return relevantAnnotations.toArray(new AnnotationBinding[0]);
138-
}
139-
};
140-
VariableElement param = new VariableElementImpl(this._env, pi);
127+
} else if (methodDeclaration.isCompactConstructor()) { // can't fold if-else into one as normal argument bindings are not added to scope yet
128+
for (LocalVariableBinding local : methodDeclaration.scope.locals) {
129+
if (local == null || !local.isParameter())
130+
continue;
131+
VariableElement param = new VariableElementImpl(this._env, local);
141132
params.add(param);
142133
}
143134
}

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

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import static org.eclipse.jdt.internal.compiler.lookup.MethodBinding.PARAM_NULLITY;
3737
import static org.eclipse.jdt.internal.compiler.lookup.MethodBinding.PARAM_OWNING;
3838

39-
import java.util.ArrayList;
4039
import java.util.List;
4140
import org.eclipse.jdt.core.compiler.CategorizedProblem;
4241
import org.eclipse.jdt.core.compiler.CharOperation;
@@ -173,44 +172,25 @@ public void bindArguments() {
173172
}
174173
return;
175174
}
176-
}
177-
178-
boolean used = this.binding == null || this.binding.isAbstract() || this.binding.isNative();
179-
AnnotationBinding[][] paramAnnotations = null;
180-
AbstractVariableDeclaration [] methodArguments = this.arguments != null ? this.arguments : this.isCompactConstructor() ? this.scope.referenceType().recordComponents : NO_ARGUMENTS;
181-
for (int i = 0, length = methodArguments.length; i < length; i++) {
182-
AbstractVariableDeclaration methodArgument = methodArguments[i];
183-
AnnotationBinding [] param_i_Annotations;
184-
if (methodArgument instanceof Argument argument) {
175+
boolean used = this.binding.isAbstract() || this.binding.isNative();
176+
AnnotationBinding[][] paramAnnotations = null;
177+
for (int i = 0, length = this.arguments.length; i < length; i++) {
178+
Argument argument = this.arguments[i];
185179
this.binding.parameters[i] = argument.bind(this.scope, this.binding.parameters[i], used);
186-
param_i_Annotations = argument.annotations != null ? argument.binding.getAnnotations() : null;
187-
} else {
188-
final LocalVariableBinding implicitArgument = new LocalVariableBinding(methodArgument.name, methodArgument.type.resolvedType, methodArgument.modifiers, true);
189-
this.scope.addLocalVariable(implicitArgument);
190-
List<AnnotationBinding> relevantAnnotationBindings = new ArrayList<>();
191-
ASTNode.getRelevantAnnotations(methodArgument.annotations, TagBits.AnnotationForParameter, relevantAnnotationBindings);
192-
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-
}
197-
}
198-
199-
if (param_i_Annotations != null && param_i_Annotations.length > 0) {
200-
if (paramAnnotations == null) {
201-
paramAnnotations = new AnnotationBinding[length][];
202-
for (int j=0; j<i; j++) {
203-
paramAnnotations[j] = Binding.NO_ANNOTATIONS;
180+
if (argument.annotations != null) {
181+
if (paramAnnotations == null) {
182+
paramAnnotations = new AnnotationBinding[length][];
183+
for (int j=0; j<i; j++) {
184+
paramAnnotations[j] = Binding.NO_ANNOTATIONS;
185+
}
204186
}
187+
paramAnnotations[i] = argument.binding.getAnnotations();
188+
} else if (paramAnnotations != null) {
189+
paramAnnotations[i] = Binding.NO_ANNOTATIONS;
205190
}
206-
paramAnnotations[i] = param_i_Annotations;
207-
} else if (paramAnnotations != null) {
208-
paramAnnotations[i] = Binding.NO_ANNOTATIONS;
209191
}
210-
}
211-
if (paramAnnotations != null) {
212-
this.binding.tagBits |= TagBits.HasParameterAnnotations;
213-
this.binding.setParameterAnnotations(paramAnnotations);
192+
if (paramAnnotations != null)
193+
this.binding.setParameterAnnotations(paramAnnotations);
214194
}
215195
}
216196

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,13 +2248,37 @@ private MethodBinding resolveTypesWithSuspendedTempErrorHandlingPolicy(MethodBin
22482248
RecordComponentBinding[] rcbs = components();
22492249
int length = rcbs.length;
22502250
method.parameters = new TypeBinding[length];
2251+
AnnotationBinding[][] methodsParameterAnnotations = null;
22512252
for (int i = 0; i < length; i++ ) {
22522253
method.parameters[i] = rcbs[i].type;
22532254
TypeBinding leafType = rcbs[i].type == null ? null : rcbs[i].type.leafComponentType();
22542255
if (leafType instanceof ReferenceBinding && (((ReferenceBinding) leafType).modifiers & ExtraCompilerModifiers.AccGenericSignature) != 0)
22552256
method.modifiers |= ExtraCompilerModifiers.AccGenericSignature;
22562257
if (rcbs[i].type.hasTypeAnnotations())
22572258
methodDecl.bits |= ASTNode.HasTypeAnnotations;
2259+
// bind the implicit argument already.
2260+
final LocalVariableBinding implicitArgument = new LocalVariableBinding(rcbs[i].name, rcbs[i].type, rcbs[i].modifiers, true);
2261+
methodDecl.scope.addLocalVariable(implicitArgument);
2262+
List<AnnotationBinding> propagatedAnnotations = new ArrayList<>();
2263+
ASTNode.getRelevantAnnotations(rcbs[i].sourceRecordComponent().annotations, TagBits.AnnotationForParameter, propagatedAnnotations);
2264+
AnnotationBinding[] annotationsForParameter = propagatedAnnotations.toArray(new AnnotationBinding[0]);
2265+
if (annotationsForParameter != null && annotationsForParameter.length > 0) {
2266+
implicitArgument.setAnnotations(annotationsForParameter, this.scope, true);
2267+
implicitArgument.extendedTagBits |= ExtendedTagBits.AllAnnotationsResolved;
2268+
if (methodsParameterAnnotations == null) {
2269+
methodsParameterAnnotations = new AnnotationBinding[length][];
2270+
for (int j = 0; j < i; j++) {
2271+
methodsParameterAnnotations[j] = Binding.NO_ANNOTATIONS;
2272+
}
2273+
}
2274+
methodsParameterAnnotations[i] = annotationsForParameter;
2275+
} else if (methodsParameterAnnotations != null) {
2276+
methodsParameterAnnotations[i] = Binding.NO_ANNOTATIONS;
2277+
}
2278+
}
2279+
if (methodsParameterAnnotations != null) {
2280+
methodDecl.binding.tagBits |= TagBits.HasParameterAnnotations;
2281+
methodDecl.binding.setParameterAnnotations(methodsParameterAnnotations);
22582282
}
22592283
}
22602284

0 commit comments

Comments
 (0)