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.runtime.type; 020 021import java.io.Externalizable; 022import java.io.IOException; 023import java.io.ObjectInput; 024import java.io.ObjectOutput; 025 026import lucee.commons.lang.CFTypes; 027import lucee.commons.lang.ExternalizableUtil; 028 029/** 030 * a single argument of a function 031 */ 032public final class FunctionArgumentImpl implements FunctionArgument,Externalizable { 033 034 035 private static final long serialVersionUID = -7275048405949174352L; // do not change 036 037 private String dspName; 038 private String hint; 039 private Collection.Key name; 040 private short type; 041 private String strType; 042 private boolean required; 043 private Struct meta; 044 private int defaultType; 045 private boolean passByReference; 046 047 048 /** @deprecated use other constructor */ 049 public FunctionArgumentImpl(String name,String type,boolean required) { 050 this(name,type,required,"",""); 051 } 052 /** @deprecated use other constructor */ 053 public FunctionArgumentImpl(String name,String type,boolean required,String dspName,String hint) { 054 this(name,type,required,DEFAULT_TYPE_RUNTIME_EXPRESSION,true,dspName,hint,null); 055 } 056 /** @deprecated use other constructor */ 057 public FunctionArgumentImpl(String name,String type,boolean required,String dspName,String hint,StructImpl meta) { 058 this(name,type,required,DEFAULT_TYPE_RUNTIME_EXPRESSION,true,dspName,hint,meta); 059 } 060 /** @deprecated use other constructor */ 061 public FunctionArgumentImpl(String name,String type,boolean required,int defaultType,String dspName,String hint,StructImpl meta) { 062 this(name, type, required, defaultType,true, dspName, hint, meta); 063 } 064 /** @deprecated use other constructor */ 065 public FunctionArgumentImpl(String name,String type,boolean required,double defaultType,String dspName,String hint,StructImpl meta) { 066 this(name, type, required, (int)defaultType,true, dspName, hint, meta); 067 } 068 /** @deprecated use other constructor */ 069 public FunctionArgumentImpl(String name,String type,boolean required,double defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) { 070 this(name, type, required, (int)defaultType,passByReference, dspName, hint, meta); 071 } 072 /** @deprecated use other constructor */ 073 public FunctionArgumentImpl(String name,String type,boolean required,int defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) { 074 this(KeyImpl.init(name),type,required,defaultType,passByReference,dspName,hint,meta); 075 } 076 /** @deprecated use other constructor */ 077 public FunctionArgumentImpl(String name,String strType,short type,boolean required,int defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) { 078 this(KeyImpl.init(name), strType, type, required, defaultType, passByReference, dspName, hint, meta); 079 } 080 /** @deprecated use other constructor */ 081 public FunctionArgumentImpl(Collection.Key name,String type,boolean required,int defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) { 082 this.name=name; 083 this.strType=(type); 084 this.type=CFTypes.toShortStrict(type,CFTypes.TYPE_UNKNOW); 085 this.required=required; 086 this.defaultType=defaultType; 087 this.dspName=dspName; 088 this.hint=hint; 089 this.meta=meta; 090 this.passByReference=passByReference; 091 } 092 093 /** 094 * NEVER USE THIS CONSTRUCTOR, this constructor is only for deserialize this object from stream 095 */ 096 public FunctionArgumentImpl() {} 097 098 099 public FunctionArgumentImpl(Collection.Key name) { 100 this(name, "any", CFTypes.TYPE_ANY, false, DEFAULT_TYPE_NULL, true, "", "", null); 101 } 102 103 public FunctionArgumentImpl(Collection.Key name,short type) { 104 this(name, CFTypes.toString(type,"any"), type, false, DEFAULT_TYPE_NULL, true, "", "", null); 105 } 106 107 public FunctionArgumentImpl(Collection.Key name,String strType,short type) { 108 this(name, strType, type, false, DEFAULT_TYPE_NULL, true, "", "", null); 109 } 110 111 public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required) { 112 this(name, strType, type, required, DEFAULT_TYPE_NULL, true, "", "", null); 113 } 114 115 public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType) { 116 this(name, strType, type, required, defaultType, true, "", "", null); 117 } 118 119 public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType,boolean passByReference) { 120 this(name, strType, type, required, defaultType, passByReference, "", "", null); 121 } 122 123 public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType,boolean passByReference,String dspName) { 124 this(name, strType, type, required, defaultType, passByReference, dspName, "", null); 125 } 126 127 public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType,boolean passByReference,String dspName,String hint) { 128 this(name, strType, type, required, defaultType, passByReference, dspName, hint, null); 129 } 130 131 public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) { 132 this.name=name; 133 this.strType=strType; 134 this.type=type; 135 this.required=required; 136 this.defaultType=defaultType; 137 this.dspName=dspName; 138 this.hint=hint; 139 this.meta=meta; 140 this.passByReference=passByReference; 141 } 142 143 144 145 146 147 148 149 //private static StructImpl sct=new StructImpl(); 150 151 152 /** 153 * @return the defaultType 154 */ 155 public int getDefaultType() { 156 return defaultType; 157 } 158 159 160 @Override 161 public Collection.Key getName() { 162 return name; 163 } 164 165 @Override 166 public boolean isRequired() { 167 return required; 168 } 169 170 @Override 171 public short getType() { 172 return type; 173 } 174 175 @Override 176 public String getTypeAsString() { 177 return strType; 178 } 179 180 @Override 181 public String getHint() { 182 return hint; 183 } 184 185 186 @Override 187 public String getDisplayName() { 188 return dspName; 189 } 190 191 @Override 192 public Struct getMetaData() { 193 return meta; 194 } 195 196 public boolean isPassByReference() { 197 return passByReference; 198 } 199 200 201 public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException { 202 dspName=ExternalizableUtil.readString(in); 203 hint=ExternalizableUtil.readString(in); 204 name=KeyImpl.init(ExternalizableUtil.readString(in)); 205 type=in.readShort(); 206 strType=ExternalizableUtil.readString(in); 207 required=in.readBoolean(); 208 meta=(Struct) in.readObject(); 209 defaultType=in.readInt(); 210 passByReference=in.readBoolean(); 211 } 212 213 214 public void writeExternal(ObjectOutput out) throws IOException { 215 ExternalizableUtil.writeString(out, dspName); 216 ExternalizableUtil.writeString(out, hint); 217 ExternalizableUtil.writeString(out, name.getString()); 218 out.writeShort(type); 219 ExternalizableUtil.writeString(out, strType); 220 out.writeBoolean(required); 221 out.writeObject(meta); 222 out.writeInt(defaultType); 223 out.writeBoolean(passByReference); 224 } 225 226 227 public boolean equals(Object obj){ 228 if(!(obj instanceof FunctionArgument)) return false; 229 return equals(this,(FunctionArgument)obj); 230 } 231 232 public static boolean equals(FunctionArgument left, FunctionArgument right) { 233 if( 234 left.getDefaultType()!=right.getDefaultType() 235 || left.getType()!=right.getType() 236 || !_eq(left.getName(), right.getName()) 237 || !_eq(left.getTypeAsString(), right.getTypeAsString()) 238 || left.isPassByReference()!=right.isPassByReference() 239 || left.isRequired()!=right.isRequired() 240 ) 241 return false; 242 243 244 return true; 245 } 246 247 private static boolean _eq(Object left, Object right) { 248 if(left==null) return right==null; 249 return left.equals(right); 250 } 251}