net.anotheria.asg.generator.GeneratedClass Maven / Gradle / Ivy
package net.anotheria.asg.generator;
import net.anotheria.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* A java class generated by the ASG.
* @author lrosenberg
*/
public class GeneratedClass extends GeneratedArtefact{
/**
* List of imports.
*/
private List imports;
/**
* Lists of implemented interfaces.
*/
private List interfaces;
/**
* The body of the class.
*/
private StringBuilder body;
/**
* The name of the parent class for 'extends' keyword.
*/
private ClassDef parent;
/**
* The generic of the class.
*/
private String generic;
/**
* The packagename.
*/
private String packageName;
/**
* The comment of the type.
*/
private String typeComment;
/**
* The comment of the class.
*/
private String clazzComment;
/**
* The type of class. Can be a class or interface.
*/
private TypeOfClass type = TypeOfClass.getDefault();
/**
* True if the class needs a logger.
*/
private boolean generateLogger = false;
/**
* True if the class is an abstract class.
*/
private boolean abstractClass = false;
/**
* The generator which created this artefact.
*/
private IGenerator generator;
public boolean isAbstractClass() {
return abstractClass;
}
public void setAbstractClass(boolean anAbstractClass) {
abstractClass = anAbstractClass;
}
/**
* Default constructor.
*/
public GeneratedClass(){
body = new StringBuilder();
imports = new ArrayList();
interfaces = new ArrayList();
}
public String createClassFileContent(){
StringBuilder ret = new StringBuilder(body.length()+200);
if (typeComment==null || typeComment.length()==0){
if (generator==null)
typeComment = CommentGenerator.generateJavaTypeComment(getName());
else
typeComment = CommentGenerator.generateJavaTypeComment(getName(), generator);
}
ret.append(getTypeComment());
ret.append("package "+getPackageName()+";");
ret.append(CRLF).append(CRLF);
for (String imp : imports){
ret.append("import ").append(imp).append(";").append(CRLF);
}
ret.append(CRLF);
if (clazzComment!=null && clazzComment.length()>0){
ret.append("/**").append(CRLF);
ret.append(" * ").append(clazzComment).append(CRLF);
// ret.append(" * ").append("Generator: ").append(generator.getClass()).append(CRLF);
ret.append(" */").append(CRLF);
}
String nameDeclaration = "public "+(isAbstractClass()?"abstract ":"")+type.toJava()+" "+getName();
if(!StringUtils.isEmpty(getGeneric()))
nameDeclaration += " <" + getGeneric() + ">";
if (getParent()!=null)
nameDeclaration += " extends "+getParent().getNameWithGeneric();
if (interfaces!=null && interfaces.size()>0){
nameDeclaration += " implements ";
for (int i=0; i0)
nameDeclaration += ", ";
nameDeclaration += interfaces.get(i);
}
}
ret.append(nameDeclaration).append("{");
ret.append(CRLF).append(CRLF);
if (generateLogger){
ret.append("\tprivate static Logger log = LoggerFactory.getLogger("+getName()+".class);").append(CRLF).append(CRLF);
}
ret.append(getBody());
ret.append("}").append(CRLF);
return ret.toString();
}
public void addInterface(String anInterface){
if (!interfaces.contains(anInterface))
interfaces.add(anInterface);
}
public void addInterface(Class> anInterfaceClazz){
if (!anInterfaceClazz.getPackage().getName().equals(getPackageName()))
addImport(anInterfaceClazz);
interfaces.add(extractClassName(anInterfaceClazz));
}
public void addImport(Class> clazz){
addImport(clazz.getName());
}
public void addImport(String anImport){
if (!imports.contains(anImport))
imports.add(anImport);
}
/**
* @return list of imports
*/
public List getImports() {
return imports;
}
public void setImports(List imports) {
this.imports = imports;
}
/**
* @return list of interfaces
*/
public List getInterfaces() {
return interfaces;
}
public void setInterfaces(List interfaces) {
this.interfaces = interfaces;
}
/**
* @return the body of the class
*/
public StringBuilder getBody() {
return body;
}
/**
* Sets the body of the class.
* @param aBody the body to set.
*/
public void setBody(StringBuilder aBody) {
body = aBody;
}
@Override
public String createFileContent() {
return createClassFileContent();
}
@Override
public String getFileType() {
return ".java";
}
@Override
public String getPath() {
return FileEntry.package2fullPath(getPackageName());
}
/**
* @return the parent of the class
*/
public ClassDef getParent() {
return parent;
}
public void setParent(Class> parentClazz){
if (!parentClazz.getPackage().getName().equals(getPackageName()))
addImport(parentClazz);
setParent(extractClassName(parentClazz));
}
private String extractClassName(Class> aClazz){
return aClazz.getName().substring(aClazz.getName().lastIndexOf('.')+1);
}
public void setParent(String aParent) {
parent = new ClassDef(aParent);
}
public void setParent(String aParent, String aGeneric) {
parent = new ClassDef(aParent, aGeneric);
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String aPackageName) {
packageName = aPackageName;
}
public String getTypeComment() {
return typeComment;
}
public void setTypeComment(String aTypeComment) {
typeComment = aTypeComment;
}
public TypeOfClass getType() {
return type;
}
public void setType(TypeOfClass aType) {
type = aType;
}
public boolean isGenerateLogger() {
return generateLogger;
}
public void setGenerateLogger(boolean aGenerateLogger) {
if (aGenerateLogger) {
addImport("org.slf4j.Logger");
addImport("org.slf4j.LoggerFactory");
}
generateLogger = aGenerateLogger;
}
public String getClazzComment() {
return clazzComment;
}
public void setClazzComment(String aClazzComment) {
clazzComment = aClazzComment;
}
public IGenerator getGenerator() {
return generator;
}
public void setGenerator(IGenerator aGenerator) {
generator = aGenerator;
}
public String getGeneric() {
return generic;
}
public void setGeneric(String generic) {
this.generic = generic;
}
public static class ClassDef{
private String name;
private String generic;
public ClassDef(String aName){
name = aName;
}
public ClassDef(String aName, String aGeneric){
name = aName;
generic = aGeneric;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGeneric() {
return generic;
}
public void setGeneric(String generic) {
this.generic = generic;
}
public String getNameWithGeneric(){
return name + (StringUtils.isEmpty(generic)? "": "<" + generic + ">");
}
@Override
public String toString(){
return getName();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy