1 8 package com.tc.backport175.bytecode; 9 10 import java.util.List ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.io.Serializable ; 14 15 20 public class AnnotationElement implements Serializable { 21 22 public static final String DEFAULT_VALUE_NAME = "value"; 23 24 29 public static final class Type { 30 public static final Type ANNOTATION = new Type("ANNOTATION"); 31 public static final Type ARRAY = new Type("ARRAY"); 32 public static final Type ENUM = new Type("ENUM"); 33 public static final Type TYPE = new Type("TYPE"); 34 public static final Type STRING = new Type("STRING"); 35 public static final Type LONG = new Type("LONG"); 36 public static final Type INTEGER = new Type("INTEGER"); 37 public static final Type SHORT = new Type("SHORT"); 38 public static final Type DOUBLE = new Type("DOUBLE"); 39 public static final Type FLOAT = new Type("FLOAT"); 40 public static final Type BYTE = new Type("BYTE"); 41 public static final Type BOOLEAN = new Type("BOOLEAN"); 42 public static final Type CHAR = new Type("CHAR"); 43 44 private final String m_name; 45 46 private Type(final String name) { 47 m_name = name; 48 } 49 50 public String toString() { 51 return m_name; 52 } 53 } 54 55 60 public static class Annotation extends AnnotationElement implements NestedAnnotationElement { 61 static final long serialVersionUID = 8769673036736880936L; 62 63 private final String m_className; 64 private final List m_elements = new ArrayList (); 65 66 public Annotation(final String className) { 67 m_className = className; 68 } 69 70 public void addElement(final String name, final Object element) { 71 m_elements.add(new AnnotationElement.NamedValue(name, element)); 72 } 73 74 77 public String getInterfaceName() { 78 return m_className; 79 } 80 81 public List getElements() { 82 return m_elements; 83 } 84 85 public String toString() { 86 StringBuffer buf = new StringBuffer (); 87 for (Iterator it = m_elements.iterator(); it.hasNext();) { 88 NamedValue namedValue = (NamedValue)it.next(); 89 buf.append(namedValue.toString()); 90 if (it.hasNext()) { 91 buf.append(", "); 92 } 93 } 94 return buf.toString(); 95 } 96 97 102 public void mergeDefaultedElement(NamedValue defaultedElement) { 103 for (Iterator iterator = m_elements.iterator(); iterator.hasNext();) { 104 NamedValue namedValue = (NamedValue) iterator.next(); 105 if (namedValue.getName().equals(defaultedElement.getName())) { 106 return; } 108 } 109 m_elements.add(defaultedElement); 110 } 111 } 112 113 118 public static class Array extends AnnotationElement implements NestedAnnotationElement { 119 static final long serialVersionUID = -6792525450471409048L; 120 121 private final List m_elements = new ArrayList (); 122 123 public void addElement(final String name, final Object element) { 124 m_elements.add(new AnnotationElement.NamedValue(DEFAULT_VALUE_NAME, element)); 125 } 126 127 public List getElements() { 128 return m_elements; 129 } 130 131 public String toString() { 132 StringBuffer buf = new StringBuffer ("["); 133 for (Iterator it = m_elements.iterator(); it.hasNext();) { 134 NamedValue namedValue = (NamedValue)it.next(); 135 buf.append(namedValue.toString()); 136 if (it.hasNext()) { 137 buf.append(", "); 138 } 139 } 140 buf.append(']'); 141 return buf.toString(); 142 } 143 } 144 145 150 public static class NamedValue extends AnnotationElement { 151 static final long serialVersionUID = 4284696449802391088L; 152 153 private final String m_name; 154 private final Object m_value; 155 private final Type m_type; 156 private boolean m_isResolved = false; 157 private Object m_resolvedValue; 158 159 public NamedValue(final String name, final Object value) { 160 if (name == null) { 161 m_name = DEFAULT_VALUE_NAME; 162 } else { 163 m_name = name; 164 } 165 m_value = value; 166 if (value instanceof Enum ) { 167 m_type = Type.ENUM; 168 } else if (value instanceof Byte ) { 169 m_type = Type.BYTE; 170 } else if (value instanceof Boolean ) { 171 m_type = Type.BOOLEAN; 172 } else if (value instanceof Character ) { 173 m_type = Type.CHAR; 174 } else if (value instanceof Short ) { 175 m_type = Type.SHORT; 176 } else if (value instanceof Integer ) { 177 m_type = Type.INTEGER; 178 } else if (value instanceof Long ) { 179 m_type = Type.LONG; 180 } else if (value instanceof Float ) { 181 m_type = Type.FLOAT; 182 } else if (value instanceof Double ) { 183 m_type = Type.DOUBLE; 184 } else if (value instanceof String ) { 185 m_type = Type.STRING; 186 } else if (value instanceof com.tc.asm.Type) { 187 m_type = Type.TYPE; 188 } else if (value instanceof Array) { 189 m_type = Type.ARRAY; 190 } else if (value instanceof Annotation) { 191 m_type = Type.ANNOTATION; 192 } else { 193 throw new IllegalArgumentException ( 194 "not valid type for named value in annotation [" + value.toString() + "]" 195 ); 196 } 197 } 198 199 public String getName() { 200 return m_name; 201 } 202 203 public Object getValue() { 204 return m_value; 205 } 206 207 public Type getType() { 208 return m_type; 209 } 210 211 public void setResolvedValue(final Object value) { 212 m_isResolved = true; 213 m_resolvedValue = value; 214 } 215 216 public boolean isResolved() { 217 return m_isResolved; 218 } 219 220 public Object getResolvedValue() { 221 return m_resolvedValue; 222 } 223 224 public String toString() { 225 StringBuffer buf = new StringBuffer (); 226 if (!m_name.equals(DEFAULT_VALUE_NAME)) { 227 buf.append(m_name); 228 buf.append('='); 229 } 230 if (m_type.equals(Type.TYPE)) { 231 buf.append(((com.tc.asm.Type)m_value).getClassName()).append(".class"); 232 } else { 233 buf.append(m_value); 234 } 235 return buf.toString(); 236 } 237 } 238 239 244 public static class Enum extends AnnotationElement { 245 static final long serialVersionUID = 1136400223420236391L; 246 247 private final String m_desc; 248 private final String m_value; 249 250 public Enum(final String desc, final String value) { 251 m_desc = desc; 252 m_value = value; 253 } 254 255 public String getDesc() { 256 return m_desc; 257 } 258 259 public String getValue() { 260 return m_value; 261 } 262 263 public String toString() { 264 StringBuffer buf = new StringBuffer (); 265 buf.append(m_desc.substring(1, m_desc.length() - 1).replace('/', '.')); 266 buf.append('.'); 267 buf.append(m_value); 268 return buf.toString(); 269 } 270 } 271 272 277 public static interface NestedAnnotationElement { 278 void addElement(String name, Object element); 279 280 List getElements(); 281 } 282 } 283 | Popular Tags |