package rmi.rmiSynth.lex;
public class LexBase extends LexStructure {
private final String CLASS = "class";
private final String INTERFACE = "interface";
private final String EXT = "extends";
private final String IMP = "implements";
private final String IMPORT = "import";
private final String PACK = "package";
private boolean kind; private Reflector ref;
private StringVector packages; private StringVector imports; private StringVector extend; private StringVector implem; private LexMethod[] methods; private LexField[] fields; private boolean hasConstructor;
private LexMethod constr; private boolean hasMain;
private LexMethod m;
public LexBase(boolean isInterface, String _name, Object o) {
kind = isInterface;
ref = new Reflector(o);
if (isInterface)
getType().setName(INTERFACE);
else
getType().setName(CLASS);
getType().setArray(false);
setName(_name);
setModif(ref.getModifiers());
setFields(ref.getFields());
setMethods(ref.getMethods());
extend = new StringVector();
implem = new StringVector();
imports = new StringVector();
packages = new StringVector();
constr = new LexMethod();
hasConstructor = false;
hasMain = false;
}
public boolean isClass() {
return kind;
}
public boolean isInterface() {
return !kind;
}
public void addPackage(String packName) {
packages.add(packName);
}
public String packToString() {
String s = "";
for (int i = 0; i < packages.size(); i++) {
s = s + PACK + " " + packages.elementAt(i) + ";\n";
}
return s;
}
public void addImport(String impPack) {
imports.add(impPack);
}
public String importToString() {
String s = "";
for (int i = 0; i < imports.size(); i++) {
s = s + IMPORT + " " + imports.elementAt(i) + ";\n";
}
return s;
}
public void addInherit(String action, String className) {
if (action.equals(EXT)) { extend.add(className);
}
if (action.equals(IMP)) { implem.add(className);
}
}
public LexField[] getFields() {
return fields;
}
public void setFields(LexField[] _fields) {
fields = _fields;
}
public LexMethod[] getMethods() {
return methods;
}
public void setMethods(LexMethod[] _methods) {
methods = _methods;
}
public void setConstr(String exName) {
constr = new LexMethod();
constr.getModif().setVisibility(1); constr.setName(this.getName()); constr.getBody().setRet(false); constr.getBody().setDelegObj(""); constr.getBody().setName("super"); hasConstructor = true; }
public LexMethod getConstr() {
return constr;
}
public void setMain(String code) {
m = new LexMethod();
m.getModif().setVisibility(1); m.getModif().setStatic(true); m.getType().setName("void"); m.setName("main"); m.setParams(new LexParam[1]); LexParam p[] = m.getParams(); p[0] = new LexParam();
p[0].setName("args[]");
p[0].getType().setName("String");
p[0].getType().setArray(true);
LexBody b = m.getBody();
b.setTryCatch(true); b.setCode(code); hasMain = true; }
public LexMethod getMain() {
return m;
}
public String toString() {
return toString(true, true, false);
}
public String toString(boolean onlyPublic, boolean noMain, boolean withDelegate) {
String s = "";
int i;
s = s + "//Created automatically by CentiJ\n";
s = s + "package graphics.raytracer;\n";
s = s + importToString(); s = s + getHeader(); s = s + getExtendsImplementsString(EXT) + getExtendsImplementsString(IMP); s = s + "{\n\n";
if (withDelegate) { s = s + getDelegateString() + "\n";
}
if (isClass() && hasConstructor) { s = s + constr.toString(true, false);
}
for (i = 0; i < fields.length; i++) { if (onlyPublic && !fields[i].getModif().isPublic()) {
continue;
}
s = s + fields[i].toString();
}
s = s + "\n";
for (i = 0; i < methods.length; i++) { if (onlyPublic && !methods[i].getModif().isPublic()) {
continue;
}
if (noMain && methods[i].getName().equals("main")) {
continue;
}
if (methods[i].getName().equals("hashCode")) {
continue;
} if (methods[i].getName().equals("clone")) {
continue;
} if (methods[i].getName().equals("toString")) {
continue;
} s = s + methods[i].toString(kind, false);
}
if (isClass() && hasMain) { s = s + m.toString(true, true);
}
s = s + "}";
return s;
}
public String getExtendsImplementsString(String action) {
String s = "";
StringVector sv = new StringVector();
if (action.equals(EXT)) { sv = extend;
}
if (action.equals(IMP)) { sv = implem;
}
int svSize = sv.size();
if (svSize > 0) {
s = s + "\n\t" + action + " " + sv.toCSV();
}
return s;
}
public String getDelegateString() {
String s = "";
if (isClass()) {
s = s + "\tprivate ";
s = s + ref.getName();
s = s + " v = new ";
s = s + ref.getName();
s = s + "();\n";
;
}
return s;
}
}