Search in sources :

Example 1 with ParameterSource

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;
    }
}
Also used : ParameterSource(org.jboss.forge.roaster.model.source.ParameterSource) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) IOException(java.io.IOException)

Aggregations

BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1 ParameterSource (org.jboss.forge.roaster.model.source.ParameterSource)1