Search

Google

Sunday, August 19, 2007

Testing Private Methods



It is possible to call Private method of a class using the Refletion API:


Method myMethod = MyClass.class.getDeclaredMethod("myMethod", ...);
myMethod.setAccessible(true);

myMethod.invoke(...);


Above snippet of code can be used to call private method, of a class

Launching an Application with a Space in its Name



Using Runtime.getRuntime().exec() to launch an application is pretty straight
forward, except when you're trying to start an application whose name contains one or more spaces.

Under Windows, you need to quote the application name:

Runtime.getRuntime().exec("\"My Appliaction.exe\"");

or use a String array:

Runtime.getRuntime().exec(new String[]{"My Application.exe"});

Under Mac OS X, you need to call open:

Runtime.getRuntime().exec(new String[]{"open", "My Application.app"});

Under Linux, you must use a String array:

Runtime.getRuntime().exec(new String[]{"My Application"});

Blinking Keyboard



How to blink the keyboard lights using Java? look out the Java code here...

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class KeyboardFlasher implements AWTEventListener {
//pick one of the folowing VK_CAPS_LOCK, VK_NUM_LOCK, VK_SCROLL_LOCK
private int LOCK = KeyEvent.VK_CAPS_LOCK;

public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
KeyboardFlasher flasher = new KeyboardFlasher();
tk.addAWTEventListener(flasher, AWTEvent.KEY_EVENT_MASK);

JFrame frame = new JFrame();
JTextField tf = new JTextField(20);
frame.getContentPane().add(tf);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public void eventDispatched(AWTEvent evt) {
if(evt instanceof KeyEvent) {
KeyEvent kevt = (KeyEvent)evt;
if(kevt.getID() == KeyEvent.KEY_PRESSED)
if(kevt.getKeyCode() != LOCK)
flipScrollLock();
}
}

public void flipScrollLock() {
Toolkit tk = Toolkit.getDefaultToolkit();
boolean state = tk.getLockingKeyState(LOCK);
tk.setLockingKeyState(LOCK,!state);
}
}