diff options
author | Andrew Clement <aclement@vmware.com> | 2025-03-25 12:41:50 -0700 |
---|---|---|
committer | Andrew Clement <aclement@vmware.com> | 2025-03-25 12:41:50 -0700 |
commit | b9e22803ad2772a704547aa46f45eeb1cd66c3ef (patch) | |
tree | f6ae3442a8dcba543db7fa2103c908b1ef082075 | |
parent | afc327cb06940d4d134ab9e03d93ae2ce3d8ebc9 (diff) | |
download | aspectj-b9e22803ad2772a704547aa46f45eeb1cd66c3ef.tar.gz aspectj-b9e22803ad2772a704547aa46f45eeb1cd66c3ef.zip |
Fixes #326: ClassCastException: class ParameterizedTypeBinding cannot be cast to class SourceTypeBinding
Modified EclipseResolvedMember such that if it finds it is dealing with a ParameterizedTypeBinding
it resolves it to the base type binding before attempting the cast. Testcode added.
Fixes #326
10 files changed, 123 insertions, 1 deletions
diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseResolvedMember.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseResolvedMember.java index 31a71c3b8..a69d689e1 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseResolvedMember.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseResolvedMember.java @@ -29,6 +29,7 @@ import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope; import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers; import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding; import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding; +import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding; import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding; import org.aspectj.weaver.AnnotationAJ; @@ -404,7 +405,14 @@ public class EclipseResolvedMember extends ResolvedMemberImpl { if (realBinding instanceof MethodBinding) { MethodBinding mb = (MethodBinding) realBinding; if (mb != null) { - SourceTypeBinding stb = (SourceTypeBinding) mb.declaringClass; + SourceTypeBinding stb = null; + ReferenceBinding declaringClass = mb.declaringClass; + if (declaringClass != null) { + declaringClass = declaringClass.actualType(); + } + if (declaringClass instanceof SourceTypeBinding) { + stb = (SourceTypeBinding)declaringClass; + } if (stb != null) { ClassScope cScope = stb.scope; if (cScope != null) { diff --git a/tests/bugs1923/gh326/pkg/BusinessDao.java b/tests/bugs1923/gh326/pkg/BusinessDao.java new file mode 100644 index 000000000..8ab256912 --- /dev/null +++ b/tests/bugs1923/gh326/pkg/BusinessDao.java @@ -0,0 +1,9 @@ +package pkg; + +public class BusinessDao<D> { + + public D doSomething() throws SourceException { + return (D) new BusinessDto(); + } + +} diff --git a/tests/bugs1923/gh326/pkg/BusinessDto.java b/tests/bugs1923/gh326/pkg/BusinessDto.java new file mode 100644 index 000000000..42d792b27 --- /dev/null +++ b/tests/bugs1923/gh326/pkg/BusinessDto.java @@ -0,0 +1,8 @@ +package pkg; + +public class BusinessDto { + + public BusinessDto() throws SourceException { +throw new SourceException(); +} +} diff --git a/tests/bugs1923/gh326/pkg/BusinessService.java b/tests/bugs1923/gh326/pkg/BusinessService.java new file mode 100644 index 000000000..10d98400f --- /dev/null +++ b/tests/bugs1923/gh326/pkg/BusinessService.java @@ -0,0 +1,18 @@ +package pkg; + +public class BusinessService { + + @HandleSourceException(message="42") + public BusinessDto doSomething() throws TargetException { + return new BusinessDao<BusinessDto>().doSomething(); + } + + +public static void main(String []argv) throws TargetException { +try { + new BusinessService().doSomething(); +} catch (TargetException te) { + System.out.println(te.getMessage()); +} +} +} diff --git a/tests/bugs1923/gh326/pkg/HandleSourceException.java b/tests/bugs1923/gh326/pkg/HandleSourceException.java new file mode 100644 index 000000000..a5645cfac --- /dev/null +++ b/tests/bugs1923/gh326/pkg/HandleSourceException.java @@ -0,0 +1,17 @@ +package pkg; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Retention(RUNTIME) +@Target(METHOD) +public @interface HandleSourceException { + + String message() default ""; + + boolean useLogger() default true; + +} diff --git a/tests/bugs1923/gh326/pkg/SourceException.java b/tests/bugs1923/gh326/pkg/SourceException.java new file mode 100644 index 000000000..2ada2d6fe --- /dev/null +++ b/tests/bugs1923/gh326/pkg/SourceException.java @@ -0,0 +1,7 @@ +package pkg; + +public class SourceException extends Exception { + + private static final long serialVersionUID = 2789789285541660969L; + +} diff --git a/tests/bugs1923/gh326/pkg/SourceExceptionHandlerAspect.aj b/tests/bugs1923/gh326/pkg/SourceExceptionHandlerAspect.aj new file mode 100644 index 000000000..6bd48f3b7 --- /dev/null +++ b/tests/bugs1923/gh326/pkg/SourceExceptionHandlerAspect.aj @@ -0,0 +1,29 @@ +package pkg; + +import java.lang.annotation.Annotation; + +import org.aspectj.lang.reflect.MethodSignature; + +public aspect SourceExceptionHandlerAspect { + + pointcut handleSourceExceptionPointcut(): @annotation(HandleSourceException); + + declare soft : SourceException : handleSourceExceptionPointcut(); + + Object around() throws TargetException: handleSourceExceptionPointcut() { + try { + return proceed(); + } catch (Throwable exception) { + String message = ""; + Annotation[] annotations = ((MethodSignature) thisJoinPoint.getSignature()).getMethod().getAnnotationsByType(HandleSourceException.class); + if (annotations.length == 1) { + message = ((HandleSourceException) annotations[0]).message(); + } + if (message.isBlank()) { + message = exception.getMessage(); + } + throw new TargetException(message, exception); + } + } + +} diff --git a/tests/bugs1923/gh326/pkg/TargetException.java b/tests/bugs1923/gh326/pkg/TargetException.java new file mode 100644 index 000000000..aa3f5c868 --- /dev/null +++ b/tests/bugs1923/gh326/pkg/TargetException.java @@ -0,0 +1,11 @@ +package pkg; + +public class TargetException extends Exception { + + private static final long serialVersionUID = -1282142579066274623L; + + public TargetException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/tests/src/test/java/org/aspectj/systemtest/ajc1923/Bugs1923Tests.java b/tests/src/test/java/org/aspectj/systemtest/ajc1923/Bugs1923Tests.java index dbcb6cf5f..8a3547472 100644 --- a/tests/src/test/java/org/aspectj/systemtest/ajc1923/Bugs1923Tests.java +++ b/tests/src/test/java/org/aspectj/systemtest/ajc1923/Bugs1923Tests.java @@ -38,6 +38,10 @@ public class Bugs1923Tests extends XMLBasedAjcTestCase { public void testGh327_IntertypeMethods() { runTest("problem with intertype method declaration code generation"); } + + public void testGh326_ClassCastExceptionHandling() { + runTest("classcast on exception handling aspect"); + } @Override protected java.net.URL getSpecFile() { diff --git a/tests/src/test/resources/org/aspectj/systemtest/ajc1923/ajc1923.xml b/tests/src/test/resources/org/aspectj/systemtest/ajc1923/ajc1923.xml index 4315ac1ad..6908c1e12 100644 --- a/tests/src/test/resources/org/aspectj/systemtest/ajc1923/ajc1923.xml +++ b/tests/src/test/resources/org/aspectj/systemtest/ajc1923/ajc1923.xml @@ -71,5 +71,16 @@ <compile files="F.aj" options="-17"> </compile> </ajc-test> + + <ajc-test dir="bugs1923/gh326" vm="17" title="classcast on exception handling aspect"> + <compile files="pkg/BusinessDao.java pkg/BusinessService.java pkg/SourceException.java pkg/TargetException.java pkg/BusinessDto.java pkg/HandleSourceException.java pkg/SourceExceptionHandlerAspect.aj" options="-17"> + </compile> + <run class="pkg.BusinessService"> + <stdout> + <line text="42"/> + </stdout> + </run> + </ajc-test> + </suite> |