Access outer anonymous class from inner anonymous class

前端 未结 4 1545
孤城傲影
孤城傲影 2021-02-12 23:25

I\'m just curious. Is there a way to access parent in anonymous class that is inside another anonymous class?

I make this example create a JTable subclass

4条回答
  •  粉色の甜心
    2021-02-12 23:54

    I think the code below will do what is technically being asked. That said, I wouldn't recommend going this route when simpler options are available.

    I'm sure you will want your run method to do something more interesting than print "Hello World!" in an infinite loop, but this seemed ok for a proof-of-concept.

    package test;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    
    public class GetOuterAnonymousClass {
    
        public static void main(String args []){
    
            JTable table = new JTable(){
    
                @Override
                public void changeSelection(
                    final int row, final int column,
                    final boolean toggle, final boolean extend) {
                    Runnable runnable = new Runnable() {
                        private Object caller;
                        public void setCaller(Object caller){
                            this.caller = caller;
                        }
    
                        @Override
                        public void run() {
                            System.out.println("Hello World!");
                            try {
                                Class clazz = this.getClass().getEnclosingClass();
                                Method method = clazz.getDeclaredMethod("changeSelection", new Class[]{Integer.TYPE, Integer.TYPE, Boolean.TYPE, Boolean.TYPE});
                                method.invoke(caller, 1, 1, true, true);
                            } catch (SecurityException e) {
                                e.printStackTrace();
                            } catch (NoSuchMethodException e) {
                                e.printStackTrace();
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }
                        }
                    };
                    Method method;
                    try {
                        method = runnable.getClass().getDeclaredMethod("setCaller", new Class[]{Object.class});
                        method.invoke(runnable, this);
                    } catch (SecurityException e1) {
                        e1.printStackTrace();
                    } catch (NoSuchMethodException e1) {
                        e1.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    SwingUtilities.invokeLater(runnable);
                }
            };
            table.changeSelection(1, 1, true, true);
    
        }
    
    }
    

提交回复
热议问题