001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.intergral.fusiondebug.server.type.nat;
020
021import java.lang.reflect.Field;
022import java.lang.reflect.Method;
023import java.lang.reflect.Modifier;
024import java.util.ArrayList;
025import java.util.List;
026
027import lucee.commons.lang.ClassUtil;
028import lucee.intergral.fusiondebug.server.type.FDValueNotMutability;
029import lucee.intergral.fusiondebug.server.type.simple.FDSimpleVariable;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.ObjectWrap;
032
033import com.intergral.fusiondebug.server.IFDStackFrame;
034
035public class FDNative extends FDValueNotMutability {
036
037        private ArrayList children=new ArrayList();
038        
039        private String name;
040
041        private Object value;
042
043        /**
044         * Constructor of the class
045         * @param frame 
046         * @param name
047         * @param coll
048         */
049        public FDNative(IFDStackFrame frame, String name, Object value) {
050                this.name=name;
051                
052                if(value instanceof ObjectWrap){
053                        value=((ObjectWrap)value).getEmbededObject(value);
054                }
055                this.value=value;
056                
057                Class clazz = value.getClass();
058                
059                
060                // super
061                children.add(new FDSimpleVariable(frame,"Extends",ClassUtil.getName(clazz.getSuperclass()),null));
062                
063                // interfaces
064                Class[] faces = clazz.getInterfaces();
065                if(faces.length>0){
066                        ArrayList list = new ArrayList();
067                        
068                        children.add(new FDSimpleVariable(frame,"Interfaces","",list));
069                        for(int i=0;i<faces.length;i++){
070                                list.add(new FDSimpleVariable(frame,"["+(i+1)+"]",ClassUtil.getName(faces[i]),null));
071                        }
072                }
073                ArrayList el,list;
074                
075                // fields
076                Field[] flds = clazz.getFields();
077                if(flds.length>0){
078                        Field fld;
079                        list = new ArrayList();
080                        children.add(new FDSimpleVariable(frame,"Fields","",list));
081                        for(int i=0;i<flds.length;i++){
082                                fld=flds[i];
083                                el = new ArrayList();
084                                list.add(new FDSimpleVariable(frame,fld.getName(),"",el));
085                                el.add(new FDSimpleVariable(frame,"Type",ClassUtil.getName(fld.getType()),null));
086                                el.add(new FDSimpleVariable(frame,"Modifier",Modifier.toString(fld.getModifiers()),null));
087                        }
088                }
089                // methods
090                Method[] mths = clazz.getMethods();
091                if(mths.length>0){
092                        Method mth;
093                        list = new ArrayList();
094                        children.add(new FDSimpleVariable(frame,"Methods","",list));
095                        for(int i=0;i<mths.length;i++){
096                                mth=mths[i];
097                                el = new ArrayList();
098                                list.add(new FDSimpleVariable(frame,mth.getName(),"",el));
099                                
100                                el.add(new FDSimpleVariable(frame,"Modifier",Modifier.toString(mth.getModifiers()),null));
101                                
102                                // exceptions
103                                Class[] clsTypes = mth.getExceptionTypes();
104                                if(clsTypes.length>0){
105                                        ArrayList exps = new ArrayList();
106                                        el.add(new FDSimpleVariable(frame,"Exceptions",Caster.toString(clsTypes.length),exps));
107                                        for(int y=0;y<clsTypes.length;y++){
108                                                exps.add(new FDSimpleVariable(frame,"["+(y+1)+"]",ClassUtil.getName(clsTypes[y]),null));
109                                        }
110                                }
111                                
112                                // params
113                                Class[] clsParams = mth.getParameterTypes();
114                                if(clsParams.length>0){
115                                        ArrayList params = new ArrayList();
116                                        el.add(new FDSimpleVariable(frame,"Parameters",Caster.toString(clsParams.length),params));
117                                        for(int y=0;y<clsParams.length;y++){
118                                                params.add(new FDSimpleVariable(frame,"["+(y+1)+"]",ClassUtil.getName(clsParams[y]),null));
119                                        }
120                                }
121                                
122                                // return
123                                el.add(new FDSimpleVariable(frame,"Return",ClassUtil.getName(mth.getReturnType()),null));
124                        }
125                }
126        }
127
128        @Override
129        public List getChildren() {
130                return children;
131        }
132
133        public String getName() {
134                return name;
135        }
136
137        @Override
138        public boolean hasChildren() {
139                return true;
140        }
141        
142        @Override
143        public String toString() {
144                return Caster.toClassName(value);
145        }
146}