/
StructureBuilder.java
198 lines (165 loc) · 7.08 KB
/
StructureBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// Copyright 2010 EXANPE <exanpe@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package fr.exanpe.tapestry.tldgen.taglib.builder;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.tapestry5.annotations.Parameter;
import org.reflections.Reflections;
import org.reflections.Store;
import org.reflections.scanners.TypesScanner;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import org.reflections.util.Utils;
import com.google.common.collect.Multimap;
import fr.exanpe.tapestry.tldgen.ext.impl.SingleTypeFieldAnnotationScanner;
import fr.exanpe.tapestry.tldgen.taglib.mapping.Attribute;
import fr.exanpe.tapestry.tldgen.taglib.mapping.Tag;
import fr.exanpe.tapestry.tldgen.taglib.mapping.Taglib;
import fr.exanpe.tapestry.tldgen.utils.TapestryTldGenConstants;
/**
* This class builds the final structure of the taglib to generate
*
* @author attonnnn
*/
public class StructureBuilder
{
private final Log log;
/**
* Contructor
*
* @param log the plugin log to output
*/
public StructureBuilder(Log log)
{
this.log = log;
}
/**
* Builds the output taglib structure
*
* @param rootPackage the root package to look the components for
* @param supportedPackages all sub packages to scan
* @param urls the urls used to scan the packages
* @return the structure containing the information on the taglib to generate
* @throws MojoExecutionException if any unexpected error occurs
*/
public Taglib build(String rootPackage, String[] supportedPackages, URL[] urls) throws MojoExecutionException
{
Taglib taglib = new Taglib();
log.debug("Creating taglib object model...");
for(String subPackage : supportedPackages){
String pkgname = rootPackage +"."+ subPackage;
log.debug("Processing taglib for full package named : " + pkgname);
Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(pkgname)))
.setUrls(urls).setScanners(new TypesScanner()));
Store store = reflections.getStore();
// Return classes anaylised by TypeScanner
Multimap<String, String> classes = store.getStoreMap().values().iterator().next();
log.debug(String.format("%s classes to analyse for %s package...", classes.keySet().size(), pkgname));
// Loop on found classes
for (final String s : classes.keySet())
{
Class<?> c;
try
{
log.debug(String.format("Load class %s into classloader", s));
c = Thread.currentThread().getContextClassLoader().loadClass(s);
}
catch (ClassNotFoundException e)
{
// should not happen as it has just been parsed by Reflection...
log.error(e);
throw new MojoExecutionException("Class loader internal error for class :" + s, e);
}
if (!c.isAnnotation() && !c.isAnonymousClass() && !c.isEnum() && !c.isInterface() && !c.isLocalClass() && !c.isMemberClass()
&& !c.isSynthetic() && !Modifier.isAbstract(c.getModifiers()))
{
log.debug("Processing Tag : " + c.getName());
Tag tag = buildTagFromClass(rootPackage, c);
taglib.getTags().add(tag);
}
}
}
log.debug("Taglib object model completed");
return taglib;
}
/**
* Return the field annotated with @Parameter for a class
*
* @param rootPackage The rootPackage to look for class and eventually parent abstract class
* @param c the class
* @return a list of {@link Field}
*/
private List<Field> getFieldAnnotatedWithParameter(String rootPackage, Class<?> c)
{
Reflections reflection = new Reflections(rootPackage, new SingleTypeFieldAnnotationScanner(c).filterResultsBy(new FilterBuilder.Include(
Parameter.class.getCanonicalName())));
Collection<String> fieldsAsString = reflection.getStore().get(SingleTypeFieldAnnotationScanner.class).values();
List<Field> fields = new ArrayList<Field>();
for (String fAsString : fieldsAsString)
{
fields.add(Utils.getFieldFromString(fAsString));
}
return fields;
}
/**
* Build the <tag> tag corresponding to a class
*
* @param rootPackage The rootPackage to look for class and eventually parent abstract class
* @param c the class for the tag
* @return the {@link Tag} build
*/
private Tag buildTagFromClass(String rootPackage, Class<?> c)
{
Tag tag = new Tag();
tag.setTagClass(c.getName());
tag.setName(c.getSimpleName());
List<Field> fields = getFieldAnnotatedWithParameter(rootPackage, c);
for (Field f : fields)
{
tag.getAttributes().add(buildAttributeFromField(f));
}
return tag;
}
/**
* Build the <attribute> tag corresponding to a field
*
* @param field the field
* @return the {@link Attribute} object
*/
private Attribute buildAttributeFromField(Field field)
{
log.debug("Processing Attribute : " + field.getName());
Attribute attribute = new Attribute();
attribute.setName(StringUtils.isNotEmpty(field.getAnnotation(Parameter.class).name())?field.getAnnotation(Parameter.class).name():field.getName());
attribute.setDeferredValue(field.getType().getName());
attribute.setRequired(field.getAnnotation(Parameter.class).required());
attribute.setParameterDescription(buildParameterDescription(field.getAnnotation(Parameter.class)));
return attribute;
}
private String buildParameterDescription(Parameter p){
StringBuilder builder = new StringBuilder();
builder.append(TapestryTldGenConstants.ALLOW_NULL_TXT).append(p.allowNull()).append("\n");
builder.append(TapestryTldGenConstants.DEFAULT_TLD_SEPARATOR).append(TapestryTldGenConstants.DEFAULT_PREFIX_TXT).append(p.defaultPrefix()).append("\n");
return builder.toString();
}
}