001    package railo.runtime.reflection.storage;
002    
003    import java.lang.reflect.Constructor;
004    import java.util.WeakHashMap;
005    
006    import railo.runtime.type.Array;
007    import railo.runtime.type.ArrayImpl;
008    
009    /**
010     * Constructor Storage Class
011     */
012    public final class WeakConstructorStorage {
013            private WeakHashMap map=new WeakHashMap();
014            
015            /**
016             * returns a constructor matching given criteria or null if Constructor doesn't exist
017             * @param clazz Class to get Constructor for
018             * @param count count of arguments for the constructor
019             * @return returns the constructors
020             */
021            public synchronized Constructor[] getConstructors(Class clazz,int count) {
022                    Object o=map.get(clazz);
023                    Array con;
024                    if(o==null) {
025                            con=store(clazz);
026                    }
027                    else con=(Array) o;
028    
029                    o=con.get(count+1,null);
030                    if(o==null) return null;
031                    return (Constructor[]) o;
032            }
033    
034            /**
035             * stores the constructors for a Class
036             * @param clazz 
037             * @return stored structure
038             */
039            private Array store(Class clazz) {
040                            Constructor[] conArr=clazz.getConstructors();
041                            Array args=new ArrayImpl();
042                            for(int i=0;i<conArr.length;i++) {
043                                    storeArgs(conArr[i],args);
044                            }
045                            map.put(clazz,args);
046                            return args;
047                    
048            }
049    
050            /**
051             * seperate and store the different arguments of one constructor
052             * @param constructor
053             * @param conArgs
054             */
055            private void storeArgs(Constructor constructor, Array conArgs) {
056                    Class[] pmt = constructor.getParameterTypes();
057                    Object o=conArgs.get(pmt.length+1,null);
058                    Constructor[] args;
059                    if(o==null) {
060                            args=new Constructor[1];
061                            conArgs.setEL(pmt.length+1,args);
062                    }
063                    else {
064                            Constructor[] cs=(Constructor[]) o;
065                            args = new Constructor[cs.length+1];
066                            for(int i=0;i<cs.length;i++) {
067                                    args[i]=cs[i];
068                            }
069                            conArgs.setEL(pmt.length+1,args);
070                    }
071                    args[args.length-1]=constructor;
072                    
073            }
074    }