1 package org.kite9.framework.alias;
2
3 import java.lang.annotation.Annotation;
4 import java.lang.reflect.Field;
5 import java.lang.reflect.Method;
6 import java.lang.reflect.ParameterizedType;
7 import java.lang.reflect.Type;
8
9 import org.kite9.framework.common.Kite9ProcessingException;
10
11
12 /***
13 * Handles the {@link K9Alias} tag and deferring to the getDefinedAlias tag
14 * for user-defined aliases.
15 *
16 * Default alias functionality is to strip everything before and including the last period,
17 * except in the case of package names.
18 *
19 * @author moffatr
20 *
21 */
22 public abstract class AbstractAliaser implements Aliaser {
23
24
25 public String getAlias(Class<?> c) {
26 K9Alias ann = c.getAnnotation(K9Alias.class);
27 if (ann!=null) {
28 return ann.value();
29 }
30
31 if (c.isMemberClass()) {
32 return getAlias(c.getSimpleName());
33 }
34
35
36 return getAlias(c.getName());
37 }
38
39 public String getAlias(Annotation a) {
40 Class<?> cl = a.annotationType();
41 K9Alias ann = cl.getAnnotation(K9Alias.class);
42
43 if (ann!=null) {
44 return ann.value();
45 }
46
47 return getAlias(cl);
48 }
49
50 public String getAlias(Method m) {
51 K9Alias ann = m.getAnnotation(K9Alias.class);
52 if (ann!=null) {
53 return ann.value();
54 }
55
56 return getAlias(m.getDeclaringClass().getName()+"."+m.getName());
57 }
58
59 public String getAlias(Field f) {
60 K9Alias ann = f.getAnnotation(K9Alias.class);
61 if (ann!=null) {
62 return ann.value();
63 }
64 return getAlias(f.getDeclaringClass().getName()+"."+f.getName());
65 }
66
67 public String getAlias(Type t) {
68 if (t instanceof Class<?>) {
69 return getAlias((Class<?>) t);
70 } else if (t instanceof ParameterizedType) {
71 return getAlias((Class<?>) ((ParameterizedType)t).getRawType());
72 } else {
73 throw new Kite9ProcessingException("Not handled yet");
74 }
75 }
76
77 public String getAlias(Package p) {
78 K9Alias ann = p.getAnnotation(K9Alias.class);
79 if (ann!=null) {
80 return ann.value();
81 }
82 String da = getDefinedAlias(p.getName());
83 if (da!=null) {
84 return da;
85 }
86
87 return p.getName();
88 }
89
90 /***
91 * Strips off the package name or class name from the alias
92 */
93 protected String getDefaultAlias(String fullName) {
94 int prefix = fullName.lastIndexOf(".");
95 return fullName.substring(prefix+1);
96 }
97
98 protected abstract String getDefinedAlias(String fullName);
99
100 public String getAlias(String fullName) {
101 String al = getDefinedAlias(fullName);
102 if (al!=null) {
103 return al;
104 }
105
106 return getDefaultAlias(fullName);
107 }
108
109 public String getObjectAlias(Object o) {
110 while (o instanceof AliasEnabled) {
111 o = ((AliasEnabled)o).getObjectForAlias();
112 }
113
114 if (o instanceof Field) {
115 return getAlias((Field) o);
116 } else if (o instanceof Method) {
117 return getAlias((Method) o);
118 }else if (o instanceof Package) {
119 return getAlias((Package) o);
120 }else if (o instanceof Annotation) {
121 return getAlias((Annotation) o);
122 }else if (o instanceof String) {
123 return getAlias((String) o);
124 } else if (o instanceof Enum<?>) {
125 return getAlias((Enum<?>) o);
126 }else if (o instanceof Class<?>) {
127 return getAlias((Class<?>) o);
128 } else if (o instanceof Method) {
129 return getAlias((Type) o);
130 } else {
131 throw new Kite9ProcessingException("Could not determine Alias for "+o.toString());
132 }
133 }
134
135 public String getAlias(Enum<?> o) {
136 return o.toString().toLowerCase();
137 }
138
139 public String getObjectTypeAlias(Object o) {
140 if (o instanceof AliasEnabled) {
141 o = ((AliasEnabled)o).getObjectForAlias();
142 }
143
144 if (o instanceof Field) {
145 return getAlias("field");
146 } else if (o instanceof Method) {
147 return getAlias("method");
148 }else if (o instanceof Package) {
149 return getAlias("package");
150 }else if (o instanceof String) {
151 return getAlias("string");
152 }else if (o instanceof Class<?>) {
153 return getAlias("class");
154 } else if (o instanceof Method) {
155 return getAlias("method");
156 } else if (o instanceof Enum<?>) {
157 return getAlias("enum");
158 } else {
159 throw new Kite9ProcessingException("Could not determine Alias for "+o.toString());
160 }
161 }
162 }
163