001    /*******************************************************************************
002     * Copyright (c) 2009 Progress Software, Inc.
003     * Copyright (c) 2004, 2008 IBM Corporation and others.
004     *
005     * All rights reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     *
010     *******************************************************************************/
011    package org.fusesource.hawtjni.generator.model;
012    
013    import java.lang.reflect.Field;
014    import java.lang.reflect.Method;
015    import java.lang.reflect.Modifier;
016    import java.util.ArrayList;
017    import java.util.Arrays;
018    import java.util.HashSet;
019    import java.util.List;
020    
021    import org.fusesource.hawtjni.runtime.ClassFlag;
022    import org.fusesource.hawtjni.runtime.JniClass;
023    
024    /**
025     * 
026     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
027     */
028    public class ReflectClass implements JNIClass {
029        
030        private Class<?> clazz;
031        private ArrayList<ReflectField> fields;
032        private ArrayList<ReflectMethod> methods;
033        private JniClass annotation;
034        private HashSet<ClassFlag> flags;
035    
036        public ReflectClass(Class<?> clazz) {
037            this.clazz = clazz;
038        }
039    
040        public String toString() {
041            return clazz.toString();
042        }
043        public int hashCode() {
044            return clazz.hashCode();
045        }
046        public boolean equals(Object obj) {
047            if (!(obj instanceof ReflectClass))
048                return false;
049            return ((ReflectClass) obj).clazz.equals(clazz);
050        }
051        
052        public Class<?> getWrapedClass() {
053            return clazz;
054        }
055    
056        ///////////////////////////////////////////////////////////////////
057        // JNIClass interface methods
058        ///////////////////////////////////////////////////////////////////
059        
060        public String getName() {
061            return clazz.getName();
062        }
063    
064        public JNIClass getSuperclass() {
065            return new ReflectClass(clazz.getSuperclass());
066        }
067        
068        public String getSimpleName() {
069            return clazz.getSimpleName();
070        }
071        
072        public List<JNIField> getDeclaredFields() {
073            lazyLoad();
074            return new ArrayList<JNIField>(fields);
075        }
076    
077        public List<JNIMethod> getDeclaredMethods() {
078            lazyLoad();
079            return new ArrayList<JNIMethod>(methods);
080        }
081    
082        public List<JNIMethod> getNativeMethods() {
083            ArrayList<JNIMethod> rc = new ArrayList<JNIMethod>();
084            for (JNIMethod method : getDeclaredMethods()) {
085                if ((method.getModifiers() & Modifier.NATIVE) == 0)
086                    continue;
087                rc.add(method);
088            }
089            return rc;
090        }
091    
092        public String getConditional() {
093            lazyLoad();
094            return annotation == null ? null : emptyFilter(annotation.conditional());
095        }
096    
097        public boolean getGenerate() {
098            return !getFlag(ClassFlag.CLASS_SKIP);
099        }
100        
101        public boolean getFlag(ClassFlag flag) {
102            lazyLoad();
103            return flags.contains(flag);
104        }
105    
106        ///////////////////////////////////////////////////////////////////
107        // Helper methods
108        ///////////////////////////////////////////////////////////////////
109        static public String emptyFilter(String value) {
110            if( value==null || value.length()==0 )
111                return null;
112            return value;
113        }
114    
115        private void lazyLoad() {
116            if (fields != null)
117                return;
118            
119            this.annotation = this.clazz.getAnnotation(JniClass.class);
120            this.flags = new HashSet<ClassFlag>();
121            if( this.annotation!=null ) {
122                this.flags.addAll(Arrays.asList(this.annotation.flags()));
123            }
124            
125            Field[] fields = clazz.getDeclaredFields();
126            this.fields = new ArrayList<ReflectField>(fields.length);
127            for (Field field : fields) {
128                this.fields.add(new ReflectField(this, field));
129            }
130            Method[] methods = clazz.getDeclaredMethods();
131            this.methods = new ArrayList<ReflectMethod>(methods.length);
132            for (int i = 0; i < methods.length; i++) {
133                this.methods.add(new ReflectMethod(this, methods[i]));
134            }
135        }
136    
137    }