From 438eb9301014ffb63bbcbae4df684a2907ac91d5 Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Fri, 6 Jan 2023 01:20:55 +0100 Subject: No more compiler errors for implicitly static inner aspects of interfaces Fixes #162. Contains regression test Bugs1919Tests.testInterfaceInnerAspectImplicitlyStatic. Signed-off-by: Alexander Kriegisch --- .../internal/compiler/ast/AspectDeclaration.java | 7 ++++- .../ast/ValidateAtAspectJAnnotationsVisitor.java | 8 +++++- .../github_162/InterfaceWithInnerClass.java | 32 ++++++++++++++++++++++ .../aspectj/systemtest/ajc1919/Bugs1919Tests.java | 4 +++ .../org/aspectj/systemtest/ajc1919/ajc1919.xml | 18 ++++++++++++ 5 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 tests/bugs1919/github_162/InterfaceWithInnerClass.java diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AspectDeclaration.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AspectDeclaration.java index cb8b82dc3..a1e7d5cdf 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AspectDeclaration.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AspectDeclaration.java @@ -155,7 +155,12 @@ public class AspectDeclaration extends TypeDeclaration { } if (this.enclosingType != null) { - if (!Modifier.isStatic(modifiers)) { + if ( + !Modifier.isStatic(modifiers) && + // Inner classes/aspects of interfaces are implicitly static, + // see https://github.com/eclipse/org.aspectj/issues/162 + (this.enclosingType.modifiers & ClassFileConstants.AccInterface) == 0 + ) { scope.problemReporter().signalError(sourceStart, sourceEnd, "inner aspects must be static"); ignoreFurtherInvestigation = true; return; diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/ValidateAtAspectJAnnotationsVisitor.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/ValidateAtAspectJAnnotationsVisitor.java index 8a63b153f..d27b2b62a 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/ValidateAtAspectJAnnotationsVisitor.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/ValidateAtAspectJAnnotationsVisitor.java @@ -36,6 +36,7 @@ import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.aspectj.org.eclipse.jdt.internal.compiler.ast.StringLiteral; import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference; +import org.aspectj.org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.aspectj.org.eclipse.jdt.internal.compiler.impl.Constant; import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.Binding; import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BlockScope; @@ -230,7 +231,12 @@ public class ValidateAtAspectJAnnotationsVisitor extends ASTVisitor { private void validateAspectDeclaration(TypeDeclaration typeDecl) { if (typeStack.size() > 1) { // it's a nested aspect - if (!Modifier.isStatic(typeDecl.modifiers)) { + if ( + !Modifier.isStatic(typeDecl.modifiers) && + // Inner classes/aspects of interfaces are implicitly static, + // see https://github.com/eclipse/org.aspectj/issues/162 + (typeDecl.enclosingType.modifiers & ClassFileConstants.AccInterface) == 0 + ) { typeDecl.scope.problemReporter().signalError(typeDecl.sourceStart, typeDecl.sourceEnd, "inner aspects must be static"); return; diff --git a/tests/bugs1919/github_162/InterfaceWithInnerClass.java b/tests/bugs1919/github_162/InterfaceWithInnerClass.java new file mode 100644 index 000000000..f26975cc0 --- /dev/null +++ b/tests/bugs1919/github_162/InterfaceWithInnerClass.java @@ -0,0 +1,32 @@ +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.JoinPoint; + +/** + * https://github.com/eclipse/org.aspectj/issues/162 + */ +public interface InterfaceWithInnerClass { + public class ImplicitlyStatic { + public int getNumber() { + return 11; + } + + public static void main(String[] args) { + System.out.println(new ImplicitlyStatic().getNumber()); + } + } + + /*static*/ aspect MyAspect { + before() : execution(* main(..)) { + System.out.println(thisJoinPoint); + } + } + + @Aspect + /*static*/ class MyAnnotationAspect { + @Before("execution(* getNumber(..))") + public void myAdvice(JoinPoint thisJoinPoint){ + System.out.println(thisJoinPoint); + } + } +} diff --git a/tests/src/test/java/org/aspectj/systemtest/ajc1919/Bugs1919Tests.java b/tests/src/test/java/org/aspectj/systemtest/ajc1919/Bugs1919Tests.java index 7a13eaeea..ae3c54398 100644 --- a/tests/src/test/java/org/aspectj/systemtest/ajc1919/Bugs1919Tests.java +++ b/tests/src/test/java/org/aspectj/systemtest/ajc1919/Bugs1919Tests.java @@ -27,6 +27,10 @@ public class Bugs1919Tests extends XMLBasedAjcTestCase { runTest("parenthesised expression with AspectJ keyword"); } + public void testInterfaceInnerAspectImplicitlyStatic() { + runTest("inner aspect of interface is implicitly static"); + } + public static Test suite() { return XMLBasedAjcTestCase.loadSuite(Bugs1919Tests.class); } diff --git a/tests/src/test/resources/org/aspectj/systemtest/ajc1919/ajc1919.xml b/tests/src/test/resources/org/aspectj/systemtest/ajc1919/ajc1919.xml index 8754acd33..f89455941 100644 --- a/tests/src/test/resources/org/aspectj/systemtest/ajc1919/ajc1919.xml +++ b/tests/src/test/resources/org/aspectj/systemtest/ajc1919/ajc1919.xml @@ -204,4 +204,22 @@ + + + + + + + + + + + + + + + -- cgit v1.2.3