|
1 |
| package com.agical.rdoc.core.service.code.impl; |
|
2 |
| |
|
3 |
| import java.lang.reflect.Modifier; |
|
4 |
| |
|
5 |
| import com.agical.rdoc.core.model.CodeBlockProvider; |
|
6 |
| import com.agical.rdoc.core.model.CodeFormatter; |
|
7 |
| import com.agical.rdoc.core.service.code.Method; |
|
8 |
| import com.agical.rdoc.core.service.code.Parameter; |
|
9 |
| import com.agical.rdoc.core.service.code.Type; |
|
10 |
| import com.agical.rdoc.core.service.code.TypeFactory; |
|
11 |
| |
|
12 |
| public class LazyMethod implements Method { |
|
13 |
| private final java.lang.reflect.Method method; |
|
14 |
| private final TypeFactory typeFactory; |
|
15 |
| private final CodeFormatter codeFormatter; |
|
16 |
| private final CodeBlockProvider codeBlockProvider; |
|
17 |
| private final ModifierAppender modifierAppender; |
|
18 |
| |
|
19 |
| |
|
20 |
45
| public LazyMethod(java.lang.reflect.Method method, TypeFactory typeFactory, CodeFormatter codeFormatter, CodeBlockProvider codeBlockProvider) {
|
|
21 |
45
| this.method = method;
|
|
22 |
45
| this.typeFactory = typeFactory;
|
|
23 |
45
| this.codeFormatter = codeFormatter;
|
|
24 |
45
| this.codeBlockProvider = codeBlockProvider;
|
|
25 |
| |
|
26 |
45
| modifierAppender = new ModifierAppender(Modifier.PUBLIC, "public", codeFormatter,
|
|
27 |
| new ModifierAppender(Modifier.PRIVATE, "private", codeFormatter)); |
|
28 |
| } |
|
29 |
| |
|
30 |
35
| public String getName() {
|
|
31 |
35
| return method.getName();
|
|
32 |
| } |
|
33 |
| |
|
34 |
1
| public Type getReturnType() {
|
|
35 |
1
| return typeFactory.create(method.getReturnType());
|
|
36 |
| } |
|
37 |
| |
|
38 |
1
| public Parameter[] getParameters() {
|
|
39 |
1
| Class[] parameterTypes = method.getParameterTypes();
|
|
40 |
1
| Parameter[] parameters = new Parameter[parameterTypes.length];
|
|
41 |
1
| for (int i = 0; i < parameterTypes.length; i++) {
|
|
42 |
1
| Class parameterType = parameterTypes[i];
|
|
43 |
1
| parameters[0] = new LazyParameter(typeFactory.create(parameterType));
|
|
44 |
| } |
|
45 |
1
| return parameters;
|
|
46 |
| } |
|
47 |
| |
|
48 |
5
| public String toString() {
|
|
49 |
5
| StringBuffer result = new StringBuffer();
|
|
50 |
| |
|
51 |
5
| boolean interfaceMethod = method.getDeclaringClass().isInterface();
|
|
52 |
| |
|
53 |
5
| if(!interfaceMethod) {
|
|
54 |
3
| appendModifiers(result);
|
|
55 |
| } |
|
56 |
5
| result.append(typeToString(method.getReturnType(), codeFormatter));
|
|
57 |
5
| result.append(' ');
|
|
58 |
5
| result.append(codeFormatter.identifier(method.getName()));
|
|
59 |
5
| result.append("( ");
|
|
60 |
| |
|
61 |
5
| Class[] parameterTypes = method.getParameterTypes();
|
|
62 |
5
| for (int i = 0; i < parameterTypes.length; i++) {
|
|
63 |
1
| result.append(typeToString(parameterTypes[i], codeFormatter));
|
|
64 |
1
| result.append(' ');
|
|
65 |
| } |
|
66 |
5
| if(interfaceMethod) {
|
|
67 |
2
| result.append(");");
|
|
68 |
| } |
|
69 |
| else { |
|
70 |
3
| result.append(") {\n");
|
|
71 |
3
| writeBody(result);
|
|
72 |
3
| result.append("\n}");
|
|
73 |
| } |
|
74 |
5
| return result.toString();
|
|
75 |
| } |
|
76 |
| |
|
77 |
3
| private void writeBody(StringBuffer result) {
|
|
78 |
3
| result.append(codeBlockProvider.getCodeBlock());
|
|
79 |
| } |
|
80 |
| |
|
81 |
3
| private void appendModifiers(StringBuffer result) {
|
|
82 |
3
| modifierAppender.appendModifierTo(method.getModifiers(), result);
|
|
83 |
| } |
|
84 |
| |
|
85 |
6
| private String typeToString(Class parameterType, CodeFormatter codeFormatter) {
|
|
86 |
6
| String name = parameterType.getName();
|
|
87 |
6
| if (parameterType.isPrimitive()) {
|
|
88 |
5
| return codeFormatter.reservedWord(name);
|
|
89 |
| } |
|
90 |
| else { |
|
91 |
1
| String simpleName = name.substring(name.lastIndexOf('.')+1);
|
|
92 |
1
| return codeFormatter.identifier(simpleName);
|
|
93 |
| } |
|
94 |
| } |
|
95 |
| } |
|
96 |
| |
|
97 |
| class ModifierAppender { |
|
98 |
| private final ModifierAppender next; |
|
99 |
| private final CodeFormatter codeFormatter; |
|
100 |
| private final int modifier; |
|
101 |
| private final String text; |
|
102 |
| |
|
103 |
90
| public ModifierAppender( final int modifier, final String text, final CodeFormatter codeFormatter, final ModifierAppender next) {
|
|
104 |
90
| super();
|
|
105 |
90
| this.next = next;
|
|
106 |
90
| this.codeFormatter = codeFormatter;
|
|
107 |
90
| this.modifier = modifier;
|
|
108 |
90
| this.text = text;
|
|
109 |
| } |
|
110 |
| |
|
111 |
45
| public ModifierAppender( final int modifier, final String text, final CodeFormatter codeFormatter) {
|
|
112 |
45
| this(modifier, text, codeFormatter, null);
|
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
6
| public void appendModifierTo(int modifiers, StringBuffer result) {
|
|
117 |
6
| if ((modifier & modifiers) > 0) {
|
|
118 |
3
| result.append(codeFormatter.reservedWord(text)).append(' ');
|
|
119 |
| } |
|
120 |
6
| if(next != null) {
|
|
121 |
3
| next.appendModifierTo(modifiers, result);
|
|
122 |
| } |
|
123 |
| } |
|
124 |
| } |