001    /*******************************************************************************
002     * Copyright (C) 2009-2011 FuseSource Corp.
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.annotation.Annotation;
014    import java.util.Arrays;
015    import java.util.HashSet;
016    
017    import org.fusesource.hawtjni.runtime.ArgFlag;
018    import org.fusesource.hawtjni.runtime.JniArg;
019    import org.fusesource.hawtjni.runtime.T32;
020    
021    import static org.fusesource.hawtjni.generator.util.TextSupport.*;
022    import static org.fusesource.hawtjni.runtime.ArgFlag.*;
023    
024    /**
025     * 
026     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
027     */
028    public class ReflectParameter implements JNIParameter {
029        
030        private ReflectMethod method;
031        private ReflectType type;
032        private int parameter;
033        
034        private JniArg annotation;
035        private boolean allowConversion;
036        private HashSet<ArgFlag> flags;
037    
038        public ReflectParameter(ReflectMethod method, int parameter, Annotation[] annotations) {
039            this.method = method;
040            this.parameter = parameter;
041            this.type = new ReflectType(method.getWrapedMethod().getParameterTypes()[parameter]);
042            this.flags = new HashSet<ArgFlag>();
043            if( annotations!=null ) {
044                for (Annotation annotation : annotations) {
045                    if( annotation instanceof JniArg ) {
046                        this.annotation = (JniArg) annotation;
047                        this.flags.addAll(Arrays.asList(this.annotation.flags()));
048                    } else if( annotation instanceof T32 ) {
049                        this.allowConversion = true;
050                    }
051                }
052            }
053        }
054    
055        public String getCast() {
056            String rc = annotation == null ? "" : annotation.cast();
057            return cast(rc);
058        }
059    
060        public boolean isPointer() {
061            if( annotation == null ) {
062                return false;
063            }
064            return getFlag(POINTER_ARG) || ( type.getWrappedClass() == Long.TYPE && getCast().endsWith("*)") );
065        }
066    
067        public JNIMethod getMethod() {
068            return method;
069        }
070    
071        public boolean getFlag(ArgFlag flag) {
072            return flags.contains(flag);
073        }
074    
075        public JNIType getType32() {
076            return type.asType32(allowConversion);
077        }
078    
079        public JNIType getType64() {
080            return type.asType64(allowConversion);
081        }
082    
083        public JNIClass getTypeClass() {
084            ReflectType type = (ReflectType) getType32();
085            return new ReflectClass(type.getWrappedClass());
086        }
087    
088        public int getParameter() {
089            return parameter;
090        }
091    
092    }