use of java.io.PrintStream in project spring-loaded by spring-projects.
the class SpringLoadedTests method runUnguarded.
public Result runUnguarded(Class<?> clazz, String methodname, Object... params) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
PrintStream oldo = System.out;
PrintStream olde = System.err;
Object result = null;
ByteArrayOutputStream oso = new ByteArrayOutputStream();
ByteArrayOutputStream ose = new ByteArrayOutputStream();
try {
if (capture) {
System.setOut(new PrintStream(oso));
System.setErr(new PrintStream(ose));
}
Object o = clazz.newInstance();
Method m = null;
Method[] ms = clazz.getMethods();
for (Method mm : ms) {
if (mm.getName().equals(methodname)) {
m = mm;
break;
}
}
if (m == null) {
Assert.fail("Invocation failure: could not find method '" + methodname + "' on type '" + clazz.getName());
}
m.setAccessible(true);
result = m.invoke(o, params);
} finally {
if (capture) {
System.setOut(oldo);
System.setErr(olde);
}
}
return new Result(result, oso.toString().replace("\r", ""), ose.toString().replace("\r", ""));
}
use of java.io.PrintStream in project spring-loaded by spring-projects.
the class SpringLoadedTests method captureOn.
/**
* Start intercepting the System.out/System.err streams
*/
protected void captureOn() {
oldo = System.out;
olde = System.err;
oso = new ByteArrayOutputStream();
ose = new ByteArrayOutputStream();
System.setOut(new PrintStream(oso));
System.setErr(new PrintStream(ose));
}
use of java.io.PrintStream in project platform_frameworks_base by android.
the class SettingsStateTest method testUpgrade.
/**
* In version 120, value "null" meant {code NULL}.
*/
public void testUpgrade() throws Exception {
final File file = new File(getContext().getCacheDir(), "setting.xml");
file.delete();
final Object lock = new Object();
final PrintStream os = new PrintStream(new FileOutputStream(file));
os.print("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>" + "<settings version=\"120\">" + " <setting id=\"0\" name=\"k0\" value=\"null\" package=\"null\" />" + " <setting id=\"1\" name=\"k1\" value=\"\" package=\"\" />" + " <setting id=\"2\" name=\"k2\" value=\"v2\" package=\"p2\" />" + "</settings>");
os.close();
final SettingsState ss = new SettingsState(lock, file, 1, SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, Looper.getMainLooper());
synchronized (lock) {
SettingsState.Setting s;
s = ss.getSettingLocked("k0");
assertEquals(null, s.getValue());
assertEquals("null", s.getPackageName());
s = ss.getSettingLocked("k1");
assertEquals("", s.getValue());
assertEquals("", s.getPackageName());
s = ss.getSettingLocked("k2");
assertEquals("v2", s.getValue());
assertEquals("p2", s.getPackageName());
}
}
use of java.io.PrintStream in project groovy-core by groovy.
the class Java2GroovyProcessor method nodePrinter.
public static String nodePrinter(String input) throws Exception {
JavaRecognizer parser = getJavaParser(input);
String[] tokenNames = parser.getTokenNames();
parser.compilationUnit();
AST ast = parser.getAST();
// modify the Java AST into a Groovy AST
modifyJavaASTintoGroovyAST(tokenNames, ast);
String[] groovyTokenNames = getGroovyTokenNames(input);
// groovify the fat Java-Like Groovy AST
groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
// now output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Visitor visitor = new NodePrinter(new PrintStream(baos), groovyTokenNames);
AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
traverser.process(ast);
return new String(baos.toByteArray());
}
use of java.io.PrintStream in project groovy-core by groovy.
the class Java2GroovyProcessor method convert.
public static String convert(String filename, String input, boolean withHeader, boolean withNewLines) throws Exception {
JavaRecognizer parser = getJavaParser(input);
String[] tokenNames = parser.getTokenNames();
parser.compilationUnit();
AST ast = parser.getAST();
// which is a really nice way of seeing the AST, folding nodes etc
if ("mindmap".equals(System.getProperty("ANTLR.AST".toLowerCase()))) {
// uppercase to hide from jarjar
try {
PrintStream out = new PrintStream(new FileOutputStream(filename + ".mm"));
Visitor visitor = new MindMapPrinter(out, tokenNames);
AntlrASTProcessor treewalker = new PreOrderTraversal(visitor);
treewalker.process(ast);
} catch (FileNotFoundException e) {
System.out.println("Cannot create " + filename + ".mm");
}
}
// modify the Java AST into a Groovy AST
modifyJavaASTintoGroovyAST(tokenNames, ast);
String[] groovyTokenNames = getGroovyTokenNames(input);
// groovify the fat Java-Like Groovy AST
groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
// now output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Visitor visitor = new SourcePrinter(new PrintStream(baos), groovyTokenNames, withNewLines);
AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
traverser.process(ast);
String header = "";
if (withHeader) {
header = "/*\n" + " Automatically Converted from Java Source \n" + " \n" + " by java2groovy v0.0.1 Copyright Jeremy Rayner 2007\n" + " \n" + " !! NOT FIT FOR ANY PURPOSE !! \n" + " 'java2groovy' cannot be used to convert one working program into another" + " */\n\n";
}
return header + new String(baos.toByteArray());
}
Aggregations