use of org.jboss.forge.roaster.model.source.ParameterSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method isGeneratedConstructor.
/**
* @param constructor a Constructor method to check.
* @return true, if the given constructor was generated by the data modeler.
*/
public boolean isGeneratedConstructor(MethodSource<JavaClassSource> constructor) {
if (constructor.isAbstract() || constructor.isStatic() || constructor.isFinal()) {
return false;
}
if (!constructor.isPublic()) {
// we only generate public constructors.
return false;
}
if (constructor.getAnnotations() != null && constructor.getAnnotations().size() > 0) {
// we never add annotations to constructors
return false;
}
List<ParameterSource<JavaClassSource>> parameters = constructor.getParameters();
List<String> expectedLines = new ArrayList<String>();
String expectedLine;
if (parameters != null) {
for (ParameterSource<JavaClassSource> param : parameters) {
if (param.getAnnotations() != null && param.getAnnotations().size() > 0) {
// we never add annotations to parameters
return false;
}
// ideally we should know if the parameter is final, but Roaster don't provide that info.
expectedLine = "this." + param.getName() + "=" + param.getName() + ";";
expectedLines.add(expectedLine);
}
}
String body = constructor.getBody();
if (body == null || (body = body.trim()).isEmpty()) {
return false;
}
try {
BufferedReader reader = new BufferedReader(new StringReader(body));
String line = null;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
lineNumber++;
if (lineNumber > expectedLines.size()) {
return false;
}
if (!line.trim().equals(expectedLines.get(lineNumber - 1))) {
return false;
}
}
return lineNumber == expectedLines.size();
} catch (IOException e) {
return false;
}
}
Aggregations