Skip to content

Commit b9e2280

Browse files
committed
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
1 parent afc327c commit b9e2280

File tree

10 files changed

+123
-1
lines changed

10 files changed

+123
-1
lines changed

org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseResolvedMember.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
3030
import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
3131
import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
32+
import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
3233
import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
3334
import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
3435
import org.aspectj.weaver.AnnotationAJ;
@@ -404,7 +405,14 @@ private TypeDeclaration getTypeDeclaration() {
404405
if (realBinding instanceof MethodBinding) {
405406
MethodBinding mb = (MethodBinding) realBinding;
406407
if (mb != null) {
407-
SourceTypeBinding stb = (SourceTypeBinding) mb.declaringClass;
408+
SourceTypeBinding stb = null;
409+
ReferenceBinding declaringClass = mb.declaringClass;
410+
if (declaringClass != null) {
411+
declaringClass = declaringClass.actualType();
412+
}
413+
if (declaringClass instanceof SourceTypeBinding) {
414+
stb = (SourceTypeBinding)declaringClass;
415+
}
408416
if (stb != null) {
409417
ClassScope cScope = stb.scope;
410418
if (cScope != null) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pkg;
2+
3+
public class BusinessDao<D> {
4+
5+
public D doSomething() throws SourceException {
6+
return (D) new BusinessDto();
7+
}
8+
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package pkg;
2+
3+
public class BusinessDto {
4+
5+
public BusinessDto() throws SourceException {
6+
throw new SourceException();
7+
}
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package pkg;
2+
3+
public class BusinessService {
4+
5+
@HandleSourceException(message="42")
6+
public BusinessDto doSomething() throws TargetException {
7+
return new BusinessDao<BusinessDto>().doSomething();
8+
}
9+
10+
11+
public static void main(String []argv) throws TargetException {
12+
try {
13+
new BusinessService().doSomething();
14+
} catch (TargetException te) {
15+
System.out.println(te.getMessage());
16+
}
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pkg;
2+
3+
import static java.lang.annotation.ElementType.METHOD;
4+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
5+
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RUNTIME)
10+
@Target(METHOD)
11+
public @interface HandleSourceException {
12+
13+
String message() default "";
14+
15+
boolean useLogger() default true;
16+
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pkg;
2+
3+
public class SourceException extends Exception {
4+
5+
private static final long serialVersionUID = 2789789285541660969L;
6+
7+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package pkg;
2+
3+
import java.lang.annotation.Annotation;
4+
5+
import org.aspectj.lang.reflect.MethodSignature;
6+
7+
public aspect SourceExceptionHandlerAspect {
8+
9+
pointcut handleSourceExceptionPointcut(): @annotation(HandleSourceException);
10+
11+
declare soft : SourceException : handleSourceExceptionPointcut();
12+
13+
Object around() throws TargetException: handleSourceExceptionPointcut() {
14+
try {
15+
return proceed();
16+
} catch (Throwable exception) {
17+
String message = "";
18+
Annotation[] annotations = ((MethodSignature) thisJoinPoint.getSignature()).getMethod().getAnnotationsByType(HandleSourceException.class);
19+
if (annotations.length == 1) {
20+
message = ((HandleSourceException) annotations[0]).message();
21+
}
22+
if (message.isBlank()) {
23+
message = exception.getMessage();
24+
}
25+
throw new TargetException(message, exception);
26+
}
27+
}
28+
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pkg;
2+
3+
public class TargetException extends Exception {
4+
5+
private static final long serialVersionUID = -1282142579066274623L;
6+
7+
public TargetException(String message, Throwable cause) {
8+
super(message, cause);
9+
}
10+
11+
}

tests/src/test/java/org/aspectj/systemtest/ajc1923/Bugs1923Tests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public void testGh327_IntertypeFields_Private() {
3838
public void testGh327_IntertypeMethods() {
3939
runTest("problem with intertype method declaration code generation");
4040
}
41+
42+
public void testGh326_ClassCastExceptionHandling() {
43+
runTest("classcast on exception handling aspect");
44+
}
4145

4246
@Override
4347
protected java.net.URL getSpecFile() {

tests/src/test/resources/org/aspectj/systemtest/ajc1923/ajc1923.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,16 @@
7171
<compile files="F.aj" options="-17">
7272
</compile>
7373
</ajc-test>
74+
75+
<ajc-test dir="bugs1923/gh326" vm="17" title="classcast on exception handling aspect">
76+
<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">
77+
</compile>
78+
<run class="pkg.BusinessService">
79+
<stdout>
80+
<line text="42"/>
81+
</stdout>
82+
</run>
83+
</ajc-test>
84+
7485

7586
</suite>

0 commit comments

Comments
 (0)