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.util.Arrays;
015    import java.util.HashSet;
016    
017    import org.fusesource.hawtjni.runtime.FieldFlag;
018    import org.fusesource.hawtjni.runtime.JniField;
019    import org.fusesource.hawtjni.runtime.T32;
020    
021    import static org.fusesource.hawtjni.generator.util.TextSupport.*;
022    import static org.fusesource.hawtjni.runtime.FieldFlag.*;
023    
024    /**
025     * 
026     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
027     */
028    public class ReflectField implements JNIField {
029        
030        private ReflectClass parent;
031        private Field field;
032        private ReflectType type;
033        private JniField annotation;
034        private HashSet<FieldFlag> flags;
035        private boolean allowConversion;
036    
037        public ReflectField(ReflectClass parent, Field field) {
038            this.parent = parent;
039            this.field = field;
040            lazyLoad();
041        }
042    
043        public int hashCode() {
044            return field.hashCode();
045        }
046    
047        public boolean equals(Object obj) {
048            if (!(obj instanceof ReflectField))
049                return false;
050            return ((ReflectField) obj).field.equals(field);
051        }
052        
053        public String toString() {
054            return field.toString();
055        }
056    
057        ///////////////////////////////////////////////////////////////////
058        // JNIField interface methods
059        ///////////////////////////////////////////////////////////////////
060    
061        public JNIClass getDeclaringClass() {
062            return parent;
063        }
064    
065        public int getModifiers() {
066            return field.getModifiers();
067        }
068    
069        public String getName() {
070            return field.getName();
071        }
072    
073        public JNIType getType() {
074            return type.asType32(allowConversion);
075        }
076    
077        public JNIType getType64() {
078            return type.asType64(allowConversion);
079        }
080    
081        public String getAccessor() {
082            return annotation == null ? "" : annotation.accessor();
083        }
084    
085        public String getCast() {
086            String rc = annotation == null ? "" : annotation.cast().trim();
087            return cast(rc);
088        }
089    
090        public boolean isPointer() {
091            if( annotation == null ) {
092                return false;
093            }
094            return getFlag(POINTER_FIELD) || ( type.getWrappedClass() == Long.TYPE && getCast().endsWith("*)") );
095        }
096    
097        public String getConditional() {
098            String parentConditional = getDeclaringClass().getConditional();
099            String myConditional = annotation == null ? null : emptyFilter(annotation.conditional());
100            if( parentConditional!=null ) {
101                if( myConditional!=null ) {
102                    return parentConditional+" && "+myConditional;
103                } else {
104                    return parentConditional;
105                }
106            }
107            return myConditional;
108        }
109    
110        public boolean getFlag(FieldFlag flag) {
111            return flags.contains(flag);
112        }
113    
114        ///////////////////////////////////////////////////////////////////
115        // Helper methods
116        ///////////////////////////////////////////////////////////////////
117        static public String emptyFilter(String value) {
118            if( value==null || value.length()==0 )
119                return null;
120            return value;
121        }
122        
123        private void lazyLoad() {
124            this.type = new ReflectType(field.getType());
125            this.annotation = this.field.getAnnotation(JniField.class);
126            this.flags = new HashSet<FieldFlag>();
127            if( this.annotation!=null ) {
128                this.flags.addAll(Arrays.asList(this.annotation.flags()));
129            }
130            
131            allowConversion = this.field.getAnnotation(T32.class)!=null;
132        }
133    
134    }