File tree Expand file tree Collapse file tree 10 files changed +123
-1
lines changed
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup
java/org/aspectj/systemtest/ajc1923
resources/org/aspectj/systemtest/ajc1923 Expand file tree Collapse file tree 10 files changed +123
-1
lines changed Original file line number Diff line number Diff line change 2929import org .aspectj .org .eclipse .jdt .internal .compiler .lookup .ExtraCompilerModifiers ;
3030import org .aspectj .org .eclipse .jdt .internal .compiler .lookup .FieldBinding ;
3131import org .aspectj .org .eclipse .jdt .internal .compiler .lookup .MethodBinding ;
32+ import org .aspectj .org .eclipse .jdt .internal .compiler .lookup .ReferenceBinding ;
3233import org .aspectj .org .eclipse .jdt .internal .compiler .lookup .SourceTypeBinding ;
3334import org .aspectj .org .eclipse .jdt .internal .compiler .lookup .TypeBinding ;
3435import 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 ) {
Original file line number Diff line number Diff line change 1+ package pkg ;
2+
3+ public class BusinessDao <D > {
4+
5+ public D doSomething () throws SourceException {
6+ return (D ) new BusinessDto ();
7+ }
8+
9+ }
Original file line number Diff line number Diff line change 1+ package pkg ;
2+
3+ public class BusinessDto {
4+
5+ public BusinessDto () throws SourceException {
6+ throw new SourceException ();
7+ }
8+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package pkg ;
2+
3+ public class SourceException extends Exception {
4+
5+ private static final long serialVersionUID = 2789789285541660969L ;
6+
7+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 () {
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments