Recent Notes

Displaying keyword search results 1 - 10
Created by zhidao on January 02, 2012 15:17:43    Last update: January 03, 2012 07:18:15
SCIM is needed to input Chinese in Ubuntu. Install the needed packages: $ sudo apt-get install scim-qtimm im-switch scim-p... Goto "System Settings": click the "gear" icon at top right corner and select "System Settings..." Click "Language Support" Select scim for "Keyboard input method system" Log out and log back in. Switch between input methods with CTRL+Space A list of available input methods is available from: http://www.scim-im.org/projects/imengines
Created by Fang on November 10, 2011 09:26:12    Last update: November 10, 2011 09:26:12
Syntax highlighted XML schema for JSF 2.0 Application Configuration Resource File ( faces-config.xml ). Almost 3000 lines! <?xml version="1.0" encoding="UTF-8"?> <xsd:sch...
Created by Dr. Xi on July 14, 2011 07:54:32    Last update: July 14, 2011 07:54:32
This is how to split a string using java.util.StringTokenizer : StringTokenizer tok = new StringTokenizer("this \... But don't do it ! According to JavaDoc : StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. Using String.split , the equivalent code is: String[] tokens = "this \tis\n a\t test".split("\... Also note that the following yield different results: String[] tokens1 = "this \tis\n a\t test".split("...
Created by Dr. Xi on June 27, 2011 15:49:00    Last update: June 28, 2011 11:13:35
You should drop the generics notation when looking up a method by signature: import java.util.*; import java.lang.reflect.Me... In fact, Class.forName("java.util.List<java.lang.String>") fails with ClassNotFoundException ! However, all information about parameterized types are not lost at runtime. The Java reflection API does provide these methods to get information about parameterized types at runtime: Method.getGenericExceptionTypes Method.getGenericParameterTypes Method.getGenericReturnType for which the non-generic counterparts are: Method.getExceptionTypes Method.getParameterTypes Method.getReturnType
Created by Dr. Xi on June 16, 2011 14:23:44    Last update: June 16, 2011 14:25:04
This example shows how a service implementation can be loaded with a URLClassLoader . The files. HelloService.java : public interface HelloService { public void... HelloServiceImpl.java : public class HelloServiceImpl implements HelloServ... ServiceFactory.java uses URLClassLoader to load the service implementation: import java.io.*; import java.net.*; pub... Test.java : import java.io.*; import java.net.*; pub... Create a runnable client jar: C:\>jar -cfe client.jar Test Test.class ServiceFac... Create a service jar: jar -cf service.jar HelloServiceImpl.class Test: C:\>java -jar client.jar Class Loader: java.net... Extra Note: : JDK7 added a method URLClassLoader.close() , so that a URLClassLoader can be discarded, and a new one created to load a new implementation.
Created by alfa on June 07, 2011 11:34:26    Last update: June 07, 2011 11:36:37
This is an example that uses dynamic proxies to trace method calls (in logging) and print out elapsed times for them. Because dynamic proxies can only be generated for interfaces, the service classes must be implemented with interface-implementation pairs. Create services A and B. A.java : public interface A { public void service1()... AImpl.java : import java.util.Random; public class AImpl... B.java : public interface B { public void service1()... BImpl.java : public class BImpl implements B { public vo... The call trace proxy: import java.lang.reflect.*; class TraceProx... The performance proxy: import java.lang.reflect.*; class Performan... The service factory: import java.lang.reflect.*; public class Se... The test class: public class Test { public static void main... The output: Entering AImpl.service1 Entering BImpl.service1... The above example has no information...
Created by alfa on June 03, 2011 09:41:03    Last update: June 03, 2011 09:41:03
Dynamic proxy can be used to eliminate the need to stub out unused interface methods. This is an example for a simple SAX content handler for XML parsing. The org.xml.sax.ContentHandler interface requires 11 methods be implemented but we only need three: import java.io.IOException; import java.util.Ar... With a dynamic proxy, we don't need the empty blocks for the unused methods: import java.io.IOException; import java.util.Ar... Equivalently (with anonymous inner class): import java.io.IOException; import java.util.Ar... demo.xml : <breakfast-menu> <food> <name>Belgian W...
Created by alfa on June 02, 2011 15:49:26    Last update: June 02, 2011 15:51:08
Facts: Dynamic proxy classes are generated by the Java runtime, from a list of interfaces given by the user. The generated proxy class implements all interfaces given by the user. The dynamic proxy class is not synthetic . The dynamic proxy class is useless without a user supplied InvocationHandler class, since there's only one constructor for the proxy class and it takes a InvocationHandler as parameter. Example code: import java.lang.reflect.Constructor; import ja... Output: Class: $Proxy0 isSynthetic: false Constructo...
Created by alfa on June 02, 2011 15:26:37    Last update: June 02, 2011 15:26:37
While doing some Java reflection code, I noticed the method Class.isSynthetic() , which the JavaDoc says returns " true if and only if this class is a synthetic class as defined by the Java Language Specification". However, there's no definition of "synthetic class" in the JLS ! The only thing that I can find that remotely resembles a definition is in the JVM spec , where it defines the synthetic attribute : "The Synthetic attribute is a fixed-length attribute in the attributes table of ClassFile (§4.1), field_info (§4.5), and method_info (§4.6) structures. A class member that does not appear in the source code must be marked using a Synthetic attribute." By this definition, a default constructor, which does not appear in the source code, should...
Created by alfa on May 26, 2011 21:16:22    Last update: June 02, 2011 14:39:57
Given a class A : class A { public int doWork(String s, int i... it is OK to call method doWork with both primitive types and the corresponding wrapping object types: new A().doWork("Hello", 1, false); new A().doWo... However, if you find method by parameter types with Java reflection, the types must match exactly, i.e., Class<?> c = Class.forName("A"); // This call f... This is a utility to find methods with compatible parameter types: import java.lang.reflect.*; import java.util.*;... Example usage: Method m = ReflectionUtil.getCompatibleMethod(c, "...
Previous  1 2 3 Next