From: <js...@us...> - 2006-11-27 16:46:05
|
Revision: 24 http://svn.sourceforge.net/jcontracts/?rev=24&view=rev Author: jstuyts Date: 2006-11-27 08:46:00 -0800 (Mon, 27 Nov 2006) Log Message: ----------- Formatted code. Modified Paths: -------------- trunk/source/java/net/sf/jcontracts/util/JavaEvaluator.java trunk/source/java/net/sf/jcontracts/util/StringComparator.java Modified: trunk/source/java/net/sf/jcontracts/util/JavaEvaluator.java =================================================================== --- trunk/source/java/net/sf/jcontracts/util/JavaEvaluator.java 2006-11-27 16:44:57 UTC (rev 23) +++ trunk/source/java/net/sf/jcontracts/util/JavaEvaluator.java 2006-11-27 16:46:00 UTC (rev 24) @@ -10,191 +10,242 @@ import sun.tools.javac.Main; -public class JavaEvaluator { +public class JavaEvaluator +{ - public static Object eval(String s) throws IOException { - String uniqueS = "" + System.currentTimeMillis(); - uniqueS = uniqueS.substring(uniqueS.length() - 6, uniqueS.length()); - String className = "TE_" + uniqueS; - String mName = "getValue"; - String filename = className + ".java"; - Object o = null; - PrintWriter ostream = new PrintWriter(new FileOutputStream(filename)); - try { - ostream.println("// default package"); - ostream.println("public class " + className + " {"); - ostream.println("public static Object " + mName + "() {"); - ostream.println(s); - ostream.println("}"); - ostream.println("}"); - ostream.close(); - String fargs[] = new String[2]; - fargs[0] = "-g"; - fargs[1] = filename; - java.io.OutputStream javacout = new ByteArrayOutputStream(1024); - Main theCompiler = new Main(javacout, ""); - boolean boo = theCompiler.compile(fargs); - if (!boo) { - System.err.println("\nJavaEvaluator: error: compilation failed!\n" + javacout.toString()); - return null; - } - try { - Class c = java.lang.Class.forName(className); - Class noArgArray[] = new Class[0]; - try { - Method theMethod = c.getMethod(mName, noArgArray); - try { - o = theMethod.invoke(c, noArgArray); - } - catch (InvocationTargetException _ex) { } - catch (IllegalArgumentException _ex) { } - catch (IllegalAccessException _ex) { } + public static Object eval(String s) throws IOException + { + String uniqueS = "" + System.currentTimeMillis(); + uniqueS = uniqueS.substring(uniqueS.length() - 6, uniqueS.length()); + String className = "TE_" + uniqueS; + String mName = "getValue"; + String filename = className + ".java"; + Object o = null; + PrintWriter ostream = new PrintWriter(new FileOutputStream(filename)); + try + { + ostream.println("// default package"); + ostream.println("public class " + className + " {"); + ostream.println("public static Object " + mName + "() {"); + ostream.println(s); + ostream.println("}"); + ostream.println("}"); + ostream.close(); + String fargs[] = new String[2]; + fargs[0] = "-g"; + fargs[1] = filename; + java.io.OutputStream javacout = new ByteArrayOutputStream(1024); + Main theCompiler = new Main(javacout, ""); + boolean boo = theCompiler.compile(fargs); + if (!boo) + { + System.err.println("\nJavaEvaluator: error: compilation failed!\n" + javacout.toString()); + return null; } - catch (NoSuchMethodException _ex) { } - } - catch (ClassNotFoundException _ex) { } - Object obj = o; - return obj; - } - finally { - (new File(filename)).delete(); - (new File(className + ".class")).delete(); - } - } - - public static Object eval(String s, String arg1) throws IOException { - String uniqueS = "" + System.currentTimeMillis(); - uniqueS = uniqueS.substring(uniqueS.length() - 6, uniqueS.length()); - String className = "TE_" + uniqueS; - String mName = "getValue_1Arg"; - String filename = className + ".java"; - Object o = null; - PrintWriter ostream = new PrintWriter(new FileOutputStream(filename)); - try { - ostream.println("// default package"); - ostream.println("public class " + className + " {"); - ostream.println("public static java.lang.Object " + mName + "(java.lang.Object arg1) {"); - ostream.println(s); - ostream.println("}"); - ostream.println("}"); - ostream.close(); - String fargs[] = new String[2]; - fargs[0] = "-g"; - fargs[1] = filename; - java.io.OutputStream javacout = new ByteArrayOutputStream(1024); - Main theCompiler = new Main(javacout, ""); - boolean boo = theCompiler.compile(fargs); - if (!boo) { - System.err.println("\nJavaEvaluator: error: compilation failed!\n" + javacout.toString()); - return null; - } - try { - Class c = java.lang.Class.forName(className); - Class argTypeArray[] = new Class[2]; - argTypeArray[0] = java.lang.Class.forName("java.lang.Object"); - argTypeArray[1] = java.lang.Class.forName("java.lang.Object"); - try { - Method theMethod = c.getMethod(mName, argTypeArray); - try { - Object argValArray[] = new Object[1]; - argValArray[0] = arg1; - o = theMethod.invoke(c, argValArray); - } - catch (InvocationTargetException _ex) { } - catch (IllegalArgumentException _ex) { } - catch (IllegalAccessException _ex) { } + try + { + Class c = java.lang.Class.forName(className); + Class noArgArray[] = new Class[0]; + try + { + Method theMethod = c.getMethod(mName, noArgArray); + try + { + o = theMethod.invoke(c, noArgArray); + } + catch (InvocationTargetException _ex) + { + } + catch (IllegalArgumentException _ex) + { + } + catch (IllegalAccessException _ex) + { + } + } + catch (NoSuchMethodException _ex) + { + } } - catch (NoSuchMethodException _ex) { } - } - catch (ClassNotFoundException _ex) { } - Object obj = o; - return obj; - } - finally { - (new File(filename)).delete(); - (new File(className + ".class")).delete(); - } - } + catch (ClassNotFoundException _ex) + { + } + Object obj = o; + return obj; + } + finally + { + (new File(filename)).delete(); + (new File(className + ".class")).delete(); + } + } - public static Object evalWithPrefix(String s, String prefix) throws Exception { - String uniqueS = "" + System.currentTimeMillis(); - uniqueS = uniqueS.substring(uniqueS.length() - 6, uniqueS.length()); - String className = "TE_" + uniqueS; - String mName = "getValue"; - String filename = className + ".java"; - Object o = null; - PrintWriter ostream = new PrintWriter(new FileOutputStream(filename)); - try { - ostream.println("// default package"); - ostream.println(prefix); - ostream.println("public class " + className + " {"); - ostream.println("public static Object " + mName + "() {"); - ostream.println(s); - ostream.println("}"); - ostream.println("}"); - ostream.close(); - String fargs[] = new String[2]; - fargs[0] = "-g"; - fargs[1] = filename; - java.io.OutputStream javacout = new ByteArrayOutputStream(1024); - boolean boo = false; - try { + public static Object eval(String s, String arg1) throws IOException + { + String uniqueS = "" + System.currentTimeMillis(); + uniqueS = uniqueS.substring(uniqueS.length() - 6, uniqueS.length()); + String className = "TE_" + uniqueS; + String mName = "getValue_1Arg"; + String filename = className + ".java"; + Object o = null; + PrintWriter ostream = new PrintWriter(new FileOutputStream(filename)); + try + { + ostream.println("// default package"); + ostream.println("public class " + className + " {"); + ostream.println("public static java.lang.Object " + mName + "(java.lang.Object arg1) {"); + ostream.println(s); + ostream.println("}"); + ostream.println("}"); + ostream.close(); + String fargs[] = new String[2]; + fargs[0] = "-g"; + fargs[1] = filename; + java.io.OutputStream javacout = new ByteArrayOutputStream(1024); Main theCompiler = new Main(javacout, ""); - boo = theCompiler.compile(fargs); - } - catch (InternalError e) { - System.err.println("\nJavaEvaluator: error: compilation failed due to VM INTERNAL ERROR!\n" + e.toString()); - return null; - } - if (!boo) { - System.err.println("\nJavaEvaluator: error: compilation failed!\n" + javacout.toString()); - return null; - } - Class c = java.lang.Class.forName(className); - Class noArgArray[] = new Class[0]; - Method theMethod = c.getMethod(mName, noArgArray); - o = theMethod.invoke(c, noArgArray); - Object obj = o; - return obj; - } - finally { - (new File(filename)).delete(); - (new File(className + ".class")).delete(); - } - } + boolean boo = theCompiler.compile(fargs); + if (!boo) + { + System.err.println("\nJavaEvaluator: error: compilation failed!\n" + javacout.toString()); + return null; + } + try + { + Class c = java.lang.Class.forName(className); + Class argTypeArray[] = new Class[2]; + argTypeArray[0] = java.lang.Class.forName("java.lang.Object"); + argTypeArray[1] = java.lang.Class.forName("java.lang.Object"); + try + { + Method theMethod = c.getMethod(mName, argTypeArray); + try + { + Object argValArray[] = new Object[1]; + argValArray[0] = arg1; + o = theMethod.invoke(c, argValArray); + } + catch (InvocationTargetException _ex) + { + } + catch (IllegalArgumentException _ex) + { + } + catch (IllegalAccessException _ex) + { + } + } + catch (NoSuchMethodException _ex) + { + } + } + catch (ClassNotFoundException _ex) + { + } + Object obj = o; + return obj; + } + finally + { + (new File(filename)).delete(); + (new File(className + ".class")).delete(); + } + } - public static void main(String args[]) throws Exception { - try { - Object result = eval("return \"Hello World 1\";"); - System.out.println("Result = " + result); - } - catch (Exception e) { - System.err.println("exception: " + e); - throw e; - } - try { - Object result = eval("return \"Hello World 2\";"); - System.out.println("Result = " + result); - } - catch (Exception e) { - System.err.println("exception: " + e); - throw e; - } - } + public static Object evalWithPrefix(String s, String prefix) throws Exception + { + String uniqueS = "" + System.currentTimeMillis(); + uniqueS = uniqueS.substring(uniqueS.length() - 6, uniqueS.length()); + String className = "TE_" + uniqueS; + String mName = "getValue"; + String filename = className + ".java"; + Object o = null; + PrintWriter ostream = new PrintWriter(new FileOutputStream(filename)); + try + { + ostream.println("// default package"); + ostream.println(prefix); + ostream.println("public class " + className + " {"); + ostream.println("public static Object " + mName + "() {"); + ostream.println(s); + ostream.println("}"); + ostream.println("}"); + ostream.close(); + String fargs[] = new String[2]; + fargs[0] = "-g"; + fargs[1] = filename; + java.io.OutputStream javacout = new ByteArrayOutputStream(1024); + boolean boo = false; + try + { + Main theCompiler = new Main(javacout, ""); + boo = theCompiler.compile(fargs); + } + catch (InternalError e) + { + System.err.println("\nJavaEvaluator: error: compilation failed due to VM INTERNAL ERROR!\n" + + e.toString()); + return null; + } + if (!boo) + { + System.err.println("\nJavaEvaluator: error: compilation failed!\n" + javacout.toString()); + return null; + } + Class c = java.lang.Class.forName(className); + Class noArgArray[] = new Class[0]; + Method theMethod = c.getMethod(mName, noArgArray); + o = theMethod.invoke(c, noArgArray); + Object obj = o; + return obj; + } + finally + { + (new File(filename)).delete(); + (new File(className + ".class")).delete(); + } + } - public static void main2(String args[]) throws Exception { - try { - Object result = eval("return \"Hello World\";"); - System.out.println("Result = " + result); - result = eval("return \"Hello World from \"+(String)arg1;", "Reto"); - System.out.println("Result = " + result); - } - catch (Exception e) { - System.err.println("exception: " + e); - throw e; - } - } + public static void main(String args[]) throws Exception + { + try + { + Object result = eval("return \"Hello World 1\";"); + System.out.println("Result = " + result); + } + catch (Exception e) + { + System.err.println("exception: " + e); + throw e; + } + try + { + Object result = eval("return \"Hello World 2\";"); + System.out.println("Result = " + result); + } + catch (Exception e) + { + System.err.println("exception: " + e); + throw e; + } + } - public JavaEvaluator() { - } + public static void main2(String args[]) throws Exception + { + try + { + Object result = eval("return \"Hello World\";"); + System.out.println("Result = " + result); + result = eval("return \"Hello World from \"+(String)arg1;", "Reto"); + System.out.println("Result = " + result); + } + catch (Exception e) + { + System.err.println("exception: " + e); + throw e; + } + } + + public JavaEvaluator() + { + } } Modified: trunk/source/java/net/sf/jcontracts/util/StringComparator.java =================================================================== --- trunk/source/java/net/sf/jcontracts/util/StringComparator.java 2006-11-27 16:44:57 UTC (rev 23) +++ trunk/source/java/net/sf/jcontracts/util/StringComparator.java 2006-11-27 16:46:00 UTC (rev 24) @@ -3,139 +3,171 @@ import java.util.Enumeration; import java.util.Vector; -public class StringComparator { - public class Interval { +public class StringComparator +{ + public class Interval + { - private int start; - private int end; - private String section; + private int start; - public int getStart() { - return start; - } + private int end; - public int getEnd() { - return end; - } + private String section; - public String getSection() { - return section; - } + public int getStart() + { + return start; + } - public String toString() { - return "Section from " + getStart() + " to " + getEnd() + " is " + getSection(); - } + public int getEnd() + { + return end; + } + public String getSection() + { + return section; + } - private Interval(int start, int end, String section) { - this.start = 0; - this.end = 0; - this.section = null; - this.start = start; - this.end = end; - this.section = section; - } - } + public String toString() + { + return "Section from " + getStart() + " to " + getEnd() + " is " + getSection(); + } + private Interval(int start, int end, String section) + { + this.start = 0; + this.end = 0; + this.section = null; + this.start = start; + this.end = end; + this.section = section; + } + } - public Vector getEqualIntervals(String s1, String s2, int min_section_length) { - Vector result = new Vector(); - int shorterLength = Math.min(s1.length(), s2.length()); - int p_s1 = 0; - int p_s2 = 0; - int start = 0; - int end = 0; - while (p_s1 < shorterLength) { - while (p_s1 < shorterLength && s1.charAt(p_s1) != s2.charAt(p_s2)) { - p_s1++; - p_s2++; - } - if (p_s1 < shorterLength) { - start = p_s1; - for (; p_s1 < shorterLength && s1.charAt(p_s1) == s2.charAt(p_s2); p_s2++) { - p_s1++; + public Vector getEqualIntervals(String s1, String s2, int min_section_length) + { + Vector result = new Vector(); + int shorterLength = Math.min(s1.length(), s2.length()); + int p_s1 = 0; + int p_s2 = 0; + int start = 0; + int end = 0; + while (p_s1 < shorterLength) + { + while (p_s1 < shorterLength && s1.charAt(p_s1) != s2.charAt(p_s2)) + { + p_s1++; + p_s2++; } + if (p_s1 < shorterLength) + { + start = p_s1; + for (; p_s1 < shorterLength && s1.charAt(p_s1) == s2.charAt(p_s2); p_s2++) + { + p_s1++; + } - end = p_s1; - if (end - start > min_section_length) { - String section = s1.substring(start, end); - Interval interval = new Interval(start, end - 1, section); - result.addElement(interval); + end = p_s1; + if (end - start > min_section_length) + { + String section = s1.substring(start, end); + Interval interval = new Interval(start, end - 1, section); + result.addElement(interval); + } } - } - } - return result; - } + } + return result; + } - public static void main(String args[]) { - Vector result = null; - result = (new StringComparator()).getEqualIntervals("abcdef 1234567890 xyz", "abcdef------------xyz", 1); - for (Enumeration e = result.elements(); e.hasMoreElements(); System.out.println("Success!")) { - Interval i = (Interval)e.nextElement(); - System.err.println(i); - if (i.getStart() != 0) { - throw new RuntimeException("test failed."); - } - if (i.getEnd() != 5) { - throw new RuntimeException("test failed."); - } - i = (Interval)e.nextElement(); - if (i.getStart() != 18) { - throw new RuntimeException("test failed."); - } - if (i.getEnd() != 20) { - throw new RuntimeException("test failed."); - } - } + public static void main(String args[]) + { + Vector result = null; + result = (new StringComparator()).getEqualIntervals("abcdef 1234567890 xyz", "abcdef------------xyz", 1); + for (Enumeration e = result.elements(); e.hasMoreElements(); System.out.println("Success!")) + { + Interval i = (Interval) e.nextElement(); + System.err.println(i); + if (i.getStart() != 0) + { + throw new RuntimeException("test failed."); + } + if (i.getEnd() != 5) + { + throw new RuntimeException("test failed."); + } + i = (Interval) e.nextElement(); + if (i.getStart() != 18) + { + throw new RuntimeException("test failed."); + } + if (i.getEnd() != 20) + { + throw new RuntimeException("test failed."); + } + } - result = (new StringComparator()).getEqualIntervals("xyz", "xyz", 1); - for (Enumeration e = result.elements(); e.hasMoreElements(); System.out.println("Success!")) { - Interval i = (Interval)e.nextElement(); - System.err.println(i); - if (i.getStart() != 0) { - throw new RuntimeException("test failed."); - } - if (i.getEnd() != 2) { - throw new RuntimeException("test failed."); - } - } + result = (new StringComparator()).getEqualIntervals("xyz", "xyz", 1); + for (Enumeration e = result.elements(); e.hasMoreElements(); System.out.println("Success!")) + { + Interval i = (Interval) e.nextElement(); + System.err.println(i); + if (i.getStart() != 0) + { + throw new RuntimeException("test failed."); + } + if (i.getEnd() != 2) + { + throw new RuntimeException("test failed."); + } + } - result = (new StringComparator()).getEqualIntervals("----1234567890AB--", "++++123+++++++AB++", 1); - for (Enumeration e = result.elements(); e.hasMoreElements(); System.out.println("Success!")) { - Interval i = (Interval)e.nextElement(); - System.err.println(i); - if (i.getStart() != 4) { - throw new RuntimeException("test failed."); - } - if (i.getEnd() != 6) { - throw new RuntimeException("test failed."); - } - i = (Interval)e.nextElement(); - if (i.getStart() != 14) { - throw new RuntimeException("test failed."); - } - if (i.getEnd() != 15) { - throw new RuntimeException("test failed."); - } - } + result = (new StringComparator()).getEqualIntervals("----1234567890AB--", "++++123+++++++AB++", 1); + for (Enumeration e = result.elements(); e.hasMoreElements(); System.out.println("Success!")) + { + Interval i = (Interval) e.nextElement(); + System.err.println(i); + if (i.getStart() != 4) + { + throw new RuntimeException("test failed."); + } + if (i.getEnd() != 6) + { + throw new RuntimeException("test failed."); + } + i = (Interval) e.nextElement(); + if (i.getStart() != 14) + { + throw new RuntimeException("test failed."); + } + if (i.getEnd() != 15) + { + throw new RuntimeException("test failed."); + } + } - result = (new StringComparator()).getEqualIntervals("----1234567890AB--", "++++123+++++++AB++", 2); - if (result.size() != 1) { - throw new RuntimeException("test failed."); - } - for (Enumeration e = result.elements(); e.hasMoreElements(); System.out.println("Success!")) { - Interval i = (Interval)e.nextElement(); - System.err.println(i); - if (i.getStart() != 4) { + result = (new StringComparator()).getEqualIntervals("----1234567890AB--", "++++123+++++++AB++", 2); + if (result.size() != 1) + { throw new RuntimeException("test failed."); - } - if (i.getEnd() != 6) { - throw new RuntimeException("test failed."); - } - } + } + for (Enumeration e = result.elements(); e.hasMoreElements(); System.out.println("Success!")) + { + Interval i = (Interval) e.nextElement(); + System.err.println(i); + if (i.getStart() != 4) + { + throw new RuntimeException("test failed."); + } + if (i.getEnd() != 6) + { + throw new RuntimeException("test failed."); + } + } - } + } - public StringComparator() { - } + public StringComparator() + { + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |