Hmm. .. public String transform(String s){?
I am trying to make an object textscripting kind of thing(based on something made by Silabsoft). How would I get this to work?
public String transform(String s){
while (s.contains("@")){
String varString = s.substring((s.indexOf("@"))+1, s.indexOf(" ", (s.indexOf("@"))+1));
try{
Class clazz = this.getClass();
Field theField = clazz.getField(varString);
if(theField.getType() == java.lang.String.class){
String someString = (String)theField.get(this);
s = s.replace("@"+varString, someString);
}
if(theField.getType().getName().equals("int")){
String someString = Integer.toString((Integer)theField.get(this));
s = s.replace("@"+varString, someString);
}
if(theField.getType().getName().equals("boolean")){
String someString = Boolean.toString((Boolean)theField.get(this));
s = s.replace("@"+varString, someString);
}
}catch (Throwable e){
System.err.println(e);
}
}
while(s.contains("$")){
String ComString = s.substring((s.indexOf("$"))+1, s.lastIndexOf("$")){
try{
if(ComString.contains("(") && ComString.contains(")")){
String args = s.substring((s.indexOf("("))+1, s.lastIndexOf(")"))
args.replaceAll(" ","");
String[] token3 = args.split(",");
Object[] o =new Object[1000];
int i = 0;
while(i <= token3.length){
i++;
Object output;
try{
output = Integer.parseInt(token[3]);
}catch (Exception e){
try{
output = Boolean.parseBoolean(token[3]);
}catch (Exception e){
output = token[3]
}
}
Object[i] == output;
}
Method meth = clazz.getMethod(ComString, o);
meth.invoke(this,null);
s = s.replace("$"+ComString+"$","");
s = s.replace("$"+ComString,"");
}else{
Method meth = clazz.getMethod(ComString,null);
meth.invoke(this,null);
s = s.replace("$"+ComString+"$","");
s = s.replace("$"+ComString,"");
}
}catch (Throwable e){
System.err.println(e);
}
}
return s;
}
Message was edited by:
JToolTip because i updated something
[4737 byte] By [
JToolTipa] at [2007-11-27 10:36:06]

# 2
NVM i got it no errors, it may or may not work
public String transform(String s){
while (s.contains("@")){
String varString = s.substring((s.indexOf("@"))+1, s.indexOf(" ", (s.indexOf("@"))+1));
try {
Class clazz = this.getClass();
Field theField = clazz.getField(varString);
if(theField.getType() == java.lang.String.class){
String someString = (String)theField.get(this);
s = s.replace("@"+varString, someString);
}
if(theField.getType().getName().equals("int")){
String someString = Integer.toString((Integer)theField.get(this));
s = s.replace("@"+varString, someString);
}
if(theField.getType().getName().equals("boolean")){
String someString = Boolean.toString((Boolean)theField.get(this));
s = s.replace("@"+varString, someString);
}
}catch (Throwable e) {
System.err.println(e);
}
}
while(s.contains("$")){
String ComString = s.substring((s.indexOf("$"))+1, s.lastIndexOf("$"));
try {
if(ComString.contains("(") && ComString.contains(")")) {
Class clazz = this.getClass();
String args = s.substring((s.indexOf("("))+1, s.lastIndexOf(")"));
args.replaceAll(" ","");
String[] token = args.split(",");
Object[] o = new Object[1000];
int i = 0;
while(i <= token.length) {
Boolean keepGoing = true;
i++;
Object output;
try {
output = Boolean.parseBoolean(token[3]);
} catch (Exception e) {
output = token[3];
}
if(keepGoing) {
try {
output = Integer.parseInt(token[3]);
} catch (Exception e) {
output = token[3];
}
}
o[i] = output;
}
Method meth = clazz.getMethod(ComString/*, null*/);
meth.invoke(this,o);
s = s.replace("$"+ComString, "");
} else {
Class clazz = this.getClass();
Method meth = clazz.getMethod(ComString/*, null*/);
meth.invoke(this,null);
s = s.replace("$"+ComString, "");
}
}catch (Throwable e) {
System.err.println(e);
}
}
return s;
}
Hows that code for a 12 yr old
# 4
Is this more of a design review type question?
1. Don't catch exceptions and then print them.
Let them be thrown all the way out to the top, or else your app will keep on running with corrupt data. If you can name some specific exceptions which -when thrown - are ok to continue after cleaning up, then catch those explicitly.
2. Every app has room for refactoring.
There are some simple things, like the "line s = s.replace("@"+varString, someString);" being duplicated unecessarily. If there was a bug fix in this line, or some major overhaul, you would have to fix it three times (once for each caught field type, speaking of which, aren't there possibly other field types?) identically. You do this again with the reflection code.
Those are my two big concerns. Overall it's very functionally programmed, not OO. Foe example, in the variable string transformations, if you leveraged delegation instead of using so many if/else clauses, that would make the code more legible.
addendum: not that functional programming is bad. It seems to be typical for transformation code. But if you can leverage OOP, then why not?
Message was edited by:
bheilers