Skip to content

Commit

Permalink
checkAnonymousClassStaticMembers in JvmGenericTypeValidator
Browse files Browse the repository at this point in the history
note that the issues are reported on the name not on modifier
  • Loading branch information
LorenzoBettini committed Jan 12, 2024
1 parent 53147b7 commit 6b92c38
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ValidationBug435020Test extends AbstractXtendTestCase {
'''
val c = parser.parse(source)
c.assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.ANONYMOUS_CLASS_STATIC_FIELD,
source.indexOf("static"), "static".length,
source.indexOf("x"), 1,
"A static field of an anonymous class must be final"
)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ class ValidationBug435020Test extends AbstractXtendTestCase {
'''
val c = parser.parse(source)
c.assertError(XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.ANONYMOUS_CLASS_STATIC_METHOD,
source.indexOf("static"), "static".length,
source.indexOf("bar"), "bar".length,
"A method of an anonymous class cannot be static"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void test_02() {
final String source = _builder.toString();
final XtendFile c = this.parser.parse(source);
this.helper.assertError(c, XtendPackage.Literals.XTEND_FIELD, IssueCodes.ANONYMOUS_CLASS_STATIC_FIELD,
source.indexOf("static"), "static".length(),
source.indexOf("x"), 1,
"A static field of an anonymous class must be final");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
Expand Down Expand Up @@ -166,7 +166,7 @@ public void test_04() {
final String source = _builder.toString();
final XtendFile c = this.parser.parse(source);
this.helper.assertError(c, XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.ANONYMOUS_CLASS_STATIC_METHOD,
source.indexOf("static"), "static".length(),
source.indexOf("bar"), "bar".length(),
"A method of an anonymous class cannot be static");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,27 +581,6 @@ protected void doCheckWhitespaceIn(RichString richString) {
richStringProcessor.process(richString, helper, helper);
}

@Check
public void checkStaticMembers(AnonymousClass anonymousClass) {
for (XtendMember member : anonymousClass.getMembers()) {
if (member.isStatic()) {
if (member instanceof XtendExecutable) {
error("A method of an anonymous class cannot be static.", member, XTEND_MEMBER__MODIFIERS,
INSIGNIFICANT_INDEX, ANONYMOUS_CLASS_STATIC_METHOD);
} else if (member instanceof XtendField) {
JvmField field = (JvmField) jvmModelAssociations.getPrimaryJvmElement(member);
if (!member.isFinal()) {
error("A static field of an anonymous class must be final.", member, XTEND_MEMBER__MODIFIERS,
INSIGNIFICANT_INDEX, ANONYMOUS_CLASS_STATIC_FIELD);
} else if (!field.isConstant()) {
error("A static field of an anonymous class must be initialized with a constant expression.",
member, XTEND_FIELD__INITIAL_VALUE, INSIGNIFICANT_INDEX, ANONYMOUS_CLASS_STATIC_FIELD);
}
}
}
}
}

@Check
public void checkDuplicateAndOverriddenFunctions(XtendTypeDeclaration xtendType) {
final JvmDeclaredType inferredType = associations.getInferredType(xtendType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ protected void checkJvmGenericType(JvmGenericType type) {
handleExceptionDuringValidation(() -> checkDuplicateAndOverriddenFunctions(sourceType, type));
var members = type.getMembers();
handleExceptionDuringValidation(() -> checkJvmExecutables(members));
if (isAnonymous(type))
handleExceptionDuringValidation(() -> checkAnonymousClassStaticMembers(type));
members.forEach(member -> {
EcoreUtil2.eAllOfType(member, JvmGenericType.class).
forEach(nestedType ->
Expand Down Expand Up @@ -246,6 +248,30 @@ protected EObject getSuperTypeSourceElement(JvmTypeReference extendedType) {
return associated;
}

protected void checkAnonymousClassStaticMembers(JvmGenericType type) {
type.getMembers().stream().filter(this::isAssociatedToSource).forEach(member -> {
var source = associations.getPrimarySourceElement(member);
if (member instanceof JvmOperation) {
JvmOperation operation = (JvmOperation) member;
if (operation.isStatic())
error("A method of an anonymous class cannot be static.", source, getFeatureForIssue(source),
INSIGNIFICANT_INDEX, ANONYMOUS_CLASS_STATIC_METHOD);
} else if (member instanceof JvmField) {
JvmField field = (JvmField) member;
if (field.isStatic()) {
if (!field.isFinal()) {
error("A static field of an anonymous class must be final.", source, getFeatureForIssue(source),
INSIGNIFICANT_INDEX, ANONYMOUS_CLASS_STATIC_FIELD);
} else if (!field.isConstant()) {
var initExpression = Iterables.getLast(filter(source.eContents(), XExpression.class));
error("A static field of an anonymous class must be initialized with a constant expression.",
source, initExpression.eContainingFeature(), INSIGNIFICANT_INDEX, ANONYMOUS_CLASS_STATIC_FIELD);
}
}
}
});
}

protected void checkDefaultSuperConstructor(EObject sourceType, JvmGenericType type) {
Iterable<JvmConstructor> constructors = filter(type.getMembers(), JvmConstructor.class);
if(type.getExtendedClass() != null) {
Expand Down

0 comments on commit 6b92c38

Please sign in to comment.