import sun.tools.attach.HotSpotVirtualMachine; //导入方法依赖的package包/类
public static void testSetFlag(String flagName, String initialFlagValue, String flagValue) throws Exception {
ProcessBuilder pb = runTarget(flagName, initialFlagValue);
Process target = pb.start();
try {
waitForReady(target);
int pid = (int)target.getPid();
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
// First set the value.
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(
vm.setFlag(flagName, flagValue)));
String line;
while((line = remoteDataReader.readLine()) != null) {
System.out.println("setFlag: " + line);
// Just empty the stream.
}
remoteDataReader.close();
// Then read and make sure we get back the set value.
remoteDataReader = new BufferedReader(new InputStreamReader(vm.printFlag(flagName)));
boolean foundExpectedLine = false;
line = null;
while((line = remoteDataReader.readLine()) != null) {
System.out.println("getFlag: " + line);
if (line.equals("-XX:" + flagName + "=" + flagValue)) {
foundExpectedLine = true;
}
}
Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");
vm.detach();
} finally {
target.destroy();
target.waitFor();
}
}