1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.nuiton.eugene.java;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.codehaus.plexus.component.annotations.Component;
29 import org.nuiton.eugene.Template;
30 import org.nuiton.eugene.models.object.ObjectModelAttribute;
31 import org.nuiton.eugene.models.object.ObjectModelInterface;
32 import org.nuiton.eugene.models.object.ObjectModelJavaModifier;
33
34 import java.util.Set;
35
36
37
38
39
40
41
42
43
44
45
46
47 @Component(role = Template.class, hint = "org.nuiton.eugene.java.JavaInterfaceTransformer")
48 public class JavaInterfaceTransformer extends ObjectModelTransformerToJava {
49
50 private static final Log log =
51 LogFactory.getLog(JavaInterfaceTransformer.class);
52
53 @Override
54 public void transformFromInterface(ObjectModelInterface input) {
55
56 boolean canGenerate = canGenerate(input);
57 if (!canGenerate) {
58
59 return;
60 }
61
62 ObjectModelInterface output = createInterface(input.getName(),
63 input.getPackageName());
64
65 if (log.isDebugEnabled()) {
66 log.debug("generate interface " +
67 output.getQualifiedName());
68 }
69
70
71
72 for (ObjectModelInterface extend : input.getInterfaces()) {
73 addInterface(output, extend.getQualifiedName());
74 }
75
76 String prefix = getConstantPrefix(input);
77
78 if (StringUtils.isEmpty(prefix)) {
79
80
81 if (log.isWarnEnabled()) {
82 log.warn("[" + input.getName() + "] Will generate constants with NO prefix, not a good idea...");
83 }
84 }
85 setConstantPrefix(prefix);
86
87 Set<String> constants = addConstantsFromDependency(input, output);
88
89
90 for (ObjectModelAttribute attr : input.getAttributes()) {
91
92 if (attr.isStatic() ||
93 !StringUtils.isNotEmpty(attr.getDefaultValue())) {
94
95
96 continue;
97 }
98
99 String constantName = attr.getName();
100
101 if (constants.contains(constantName)) {
102
103
104 continue;
105 }
106
107
108 addConstant(output,
109 constantName,
110 attr.getType(),
111 attr.getDefaultValue(),
112 ObjectModelJavaModifier.PUBLIC
113 );
114 }
115
116
117
118 JavaGeneratorUtil.cloneOperations(this,
119 input.getOperations(),
120 output,
121 false
122 );
123 }
124
125 protected boolean canGenerate(ObjectModelInterface input) {
126
127
128 boolean b = !getResourcesHelper().isJavaFileInClassPath(input.getQualifiedName());
129
130 if (b) {
131
132
133 b = input.getStereotypes().isEmpty();
134 }
135 return b;
136 }
137
138 }