zhfeng commented on issue #7841:
URL: https://github.com/apache/camel-quarkus/issues/7841#issuecomment-4200694890

   The AI suggest the other way to detect the class by using ASM, like
   ```java
   class ExceptionClassDetector extends MethodVisitor {
         private final Set<String> pendingClasses = new LinkedHashSet<>();
         private final Set<String> confirmedClasses;                            
                                                           
      
         ExceptionClassDetector(Set<String> confirmedClasses) {                 
                                                           
             super(Opcodes.ASM9);
             this.confirmedClasses = confirmedClasses;                          
                                                           
         }
   
         @Override
         public void visitLdcInsn(Object value) {
             if (value instanceof Type type && type.getSort() == Type.OBJECT) {
                 String className = type.getClassName();                        
                                                           
                 if (isThrowable(className)) {
                     pendingClasses.add(className);                             
                                                           
                 }
             }                                                                  
                                                           
         }
   
         @Override
         public void visitMethodInsn(int opcode, String owner, String name,
                 String descriptor, boolean isInterface) {                      
                                                           
             if (isExceptionRegistrationMethod(name, descriptor)) {
                 confirmedClasses.addAll(pendingClasses);                       
                                                           
             }
             pendingClasses.clear();                                            
                                                           
         }
   
         private boolean isExceptionRegistrationMethod(String name, String 
descriptor) {                                                   
             return ("onException".equals(name) || "doCatch".equals(name) || 
"exception".equals(name))
                     && descriptor.contains("[Ljava/lang/Class;");              
                                                           
         }
     }                                     
   ```
   
   What this correctly handles:                                                 
                                                         
     - onException(MyException.class) — confirmed
     - onException(IOException.class, MyException.class) — both confirmed       
                                                           
     - log.info(SomeException.class.getName()) — cleared at getName(), not 
registered
     - someMap.put(SomeException.class, handler) — cleared at put(), not 
registered                                                        
                                                                                
                                                           
     Edge cases it can't handle (need manual @RegisterForReflection):           
                                                           
     - onException(getExceptionClass()) — dynamic, no LDC at all                
                                                           
     - Exception class stored to a variable with an unrelated method call in 
between before being passed to onException    
   
   @JiriOndrusek what do you think? 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to