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.component; 020 021import lucee.commons.lang.StringUtil; 022import lucee.runtime.Component; 023import lucee.runtime.converter.ConverterException; 024import lucee.runtime.converter.ScriptConverter; 025import lucee.runtime.exp.PageException; 026import lucee.runtime.op.Caster; 027import lucee.runtime.op.Duplicator; 028import lucee.runtime.type.Struct; 029import lucee.runtime.type.StructImpl; 030import lucee.runtime.type.util.KeyConstants; 031import lucee.runtime.type.util.StructUtil; 032import lucee.transformer.bytecode.util.ASMProperty; 033import lucee.transformer.bytecode.util.ASMUtil; 034 035import org.objectweb.asm.Type; 036 037/** 038 */ 039public final class PropertyImpl extends MemberSupport implements Property,ASMProperty { 040 041 042 private static final long serialVersionUID = 3206074213415946902L; 043 044 045 private String type="any"; 046 private String name; 047 private boolean required; 048 private boolean setter=true; 049 private boolean getter=true; 050 051 052 private String _default; 053 private String displayname=""; 054 private String hint=""; 055 private Struct dynAttrs=new StructImpl(); 056 private Struct metadata; 057 058 private String ownerName; 059 060 public PropertyImpl() { 061 super(Component.ACCESS_REMOTE); 062 } 063 064 /** 065 * @return the _default 066 */ 067 public String getDefault() { 068 return _default; 069 } 070 071 /** 072 * @param _default the _default to set 073 */ 074 public void setDefault(String _default) { 075 this._default = _default; 076 } 077 078 /** 079 * @return the displayname 080 */ 081 public String getDisplayname() { 082 return displayname; 083 } 084 085 /** 086 * @param displayname the displayname to set 087 */ 088 public void setDisplayname(String displayname) { 089 this.displayname = displayname; 090 } 091 092 /** 093 * @return the hint 094 */ 095 public String getHint() { 096 return hint; 097 } 098 099 /** 100 * @param hint the hint to set 101 */ 102 public void setHint(String hint) { 103 this.hint = hint; 104 } 105 106 /** 107 * @return the name 108 */ 109 public String getName() { 110 return name; 111 } 112 113 /** 114 * @param name the name to set 115 */ 116 public void setName(String name) { 117 this.name = name; 118 } 119 120 /** 121 * @return the required 122 */ 123 public boolean isRequired() { 124 return required; 125 } 126 127 /** 128 * @param required the required to set 129 */ 130 public void setRequired(boolean required) { 131 this.required = required; 132 } 133 134 /** 135 * @return the type 136 */ 137 public String getType() { 138 return type; 139 } 140 141 /** 142 * @param type the type to set 143 */ 144 public void setType(String type) { 145 this.type = type; 146 } 147 148 @Override 149 public Object getValue() { 150 return _default; 151 } 152 153 @Override 154 public Type getASMType() throws PageException { 155 return ASMUtil.toType(getType(), true); 156 } 157 158 /** 159 * @return the setter 160 */ 161 public boolean getSetter() { 162 return setter; 163 } 164 165 /** 166 * @param setter the setter to set 167 */ 168 public void setSetter(boolean setter) { 169 this.setter = setter; 170 } 171 172 /** 173 * @return the getter 174 */ 175 public boolean getGetter() { 176 return getter; 177 } 178 179 /** 180 * @param getter the getter to set 181 */ 182 public void setGetter(boolean getter) { 183 this.getter = getter; 184 } 185 186 187 188 public Object getMetaData() { 189 Struct sct=new StructImpl(); 190 191 // meta 192 if(metadata!=null) 193 StructUtil.copy(metadata, sct, true); 194 195 sct.setEL(KeyConstants._name,name); 196 if(!StringUtil.isEmpty(hint,true))sct.setEL(KeyConstants._hint,hint); 197 if(!StringUtil.isEmpty(displayname,true))sct.setEL(KeyConstants._displayname,displayname); 198 if(!StringUtil.isEmpty(type,true))sct.setEL(KeyConstants._type,type); 199 200 // dyn attributes 201 202 StructUtil.copy(dynAttrs, sct, true); 203 204 return sct; 205 } 206 207 public Struct getDynamicAttributes() { 208 return dynAttrs; 209 } 210 public Struct getMeta() { 211 if(metadata==null) metadata=new StructImpl(); 212 return metadata; 213 } 214 215 @Override 216 public Class getClazz() { 217 return null; 218 } 219 220 public boolean isPeristent() { 221 return Caster.toBooleanValue(dynAttrs.get(KeyConstants._persistent,Boolean.TRUE),true); 222 } 223 224 public void setOwnerName(String ownerName) { 225 this.ownerName=ownerName; 226 } 227 public String getOwnerName() { 228 return ownerName; 229 } 230 231 232 233 @Override 234 public String toString() { 235 String strDynAttrs=""; 236 try{ 237 strDynAttrs=new ScriptConverter().serialize(dynAttrs); 238 } 239 catch(ConverterException ce){} 240 241 return "default:"+this._default+";displayname:"+this.displayname+";hint:"+this.hint+ 242 ";name:"+this.name+";type:"+this.type+";ownerName:"+ownerName+";attrs:"+strDynAttrs+";"; 243 } 244 245 public boolean equals(Object obj) { 246 if(this == obj) return true; 247 if(!(obj instanceof Property)) return false; 248 Property other=(Property)obj; 249 250 return toString().equals(other.toString()); 251 } 252 253 public Object duplicate(boolean deepCopy) { 254 PropertyImpl other = new PropertyImpl(); 255 other._default=_default; 256 other.displayname=displayname; 257 other.getter=getter; 258 other.hint=hint; 259 other.dynAttrs=deepCopy?(Struct) Duplicator.duplicate(dynAttrs,deepCopy):dynAttrs; 260 other.name=name; 261 other.ownerName=ownerName; 262 other.required=required; 263 other.setter=setter; 264 other.type=type; 265 266 return other; 267 } 268}