Recent Notes
Displaying keyword search results 11 - 20
Created by Fang on November 10, 2011 20:33:46
Last update: November 10, 2011 20:33:46
The stack trace is like this:
java.lang.IllegalArgumentException: Component prop...
You get this error because you are using the class attribute with a JSF UI component, for which the class attribute cannot be altered. Of course you meant CSS class, not Java class! You can use the styleClass attribute instead of the class attribute. The styleClass attribute becomes the class attribute when the component is rendered.
If you can add a tag handler to the UI component, you can alias class to styleClass , which will allow you to use the class attribute on the UI component:
import javax.faces.view.facelets.*;
pub...
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 Fang on October 28, 2011 13:49:40
Last update: October 30, 2011 19:23:25
This is a simple example to demonstrate the templating power of JSF facelets. If you've used struts tiles before, you'll recognize the simplicity of templating with facelets. I've stripped out everything else except the pages themselves, just to put our focus on facelets. This is a Maven based project, and you need Tomcat (or any servlet container) to run the resulting webapp. To begin with this is the list of files:
./pom.xml ./src/main/webapp/home.xhtml ./src... I left faces-config.xml in there for completeness sake, it may not be needed. The Maven POM ( pom.xml ): <?xml version="1.0" encoding="UTF-8"?> <project... Web app configuration ( WEB-INF/web.xml ): <?xml version="1.0" encoding="UTF-8"?> <web-app... Empty WEB-INF/faces-config.xml : <?xml version="1.0" encoding="UTF-8"?> <!-- Thi... index.jsp is simply a redirect to home.jsf : <% response.sendRedirect("home.jsf"); %>...
Created by Fang on October 22, 2011 20:43:31
Last update: October 22, 2011 20:45:13
The only explanation of why some Java EE API classes are stripped off methods implementations I can find is this JBos forum post: What's the cause of this exception: java.lang.ClassFormatError: Absent Code? which also provides some workarounds for these crippled API classes. The explanation offered was: When one compiles, they want to run as well. By the way, we have been promoting full set of Java EE APIs which can only be used for compilation - they are stripped off method details. That way, user can't take those artifacts and try to use it in runtime. Honestly, I don't see any logic in those statements. This is the only place any such explanation is offered. Yes only from this JBos forum post! There's no public...
Created by Fang on October 22, 2011 19:51:05
Last update: October 22, 2011 20:31:48
I built a very basic JSF application and deployed to Tomcat 7.0.22, but it failed with this error:
Caused by: java.lang.ClassFormatError: Absent Code... That looks weird and I wasn't able to find a sensible explanation! So I copied the jsf-api-2.1.jar , which was downloaded from the java.net Maven repository by Maven, into a temp folder. And tested it with this simple program: public class ClassFormatErrorTest { public ... I also copied servlet-api.jar from Tomcat's lib folder to the temp folder. Sure enough it failed with the same error: C:\tmp>java -cp .;jsf-api-2.1.jar;servlet-api.jar ... But when I replaced the javax.faces.webapp.FacesServlet class with one I compiled from source, the error disappears! Conclusions: The jar file jsf-api-2.1.jar from java.net Maven repository is good for compilation only (cannot be used...
Created by freyo on September 07, 2011 16:46:14
Last update: September 07, 2011 19:23:00
The Android unit test framework is based on JUnit 3 , not JUnit 4. Test cases have to extend junit.framework.TestCase or a subclass (such as android.test.InstrumentationTestCase ). Tests are identified by public methods whose name starts with test , not methods annotated with @Test (as in JUnit 4). An Android test suite is packaged as an APK, just like the application being tested. To create a test package, first you need to identify the application package it is testing. Google suggests to put the test package source in a directory named tests/ alongside the src/ directory of the main application. At runtime, Android instrumentation loads both the test package and the application under test into the same process. Therefore, the tests can invoke methods on...
Created by Dr. Xi on July 15, 2011 09:25:15
Last update: July 15, 2011 09:25:15
Some methods to search for a substring within a string:
To know that a substring indeed exists within a string:
boolean found = wholeString.contains(substring);
To find where the substring is contained:
int index = wholeString.indexOf(substring);
If the substring is regex:
boolean match = wholeString.matches(".*" + substri...
Case insensitive match: convert both whole string and substring to lowercase, then compare. Or, use case insensitive flag for regex.
Test code:
import java.util.regex.*;
public class Stri...
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 22, 2011 15:15:15
Last update: June 22, 2011 15:15:15
There's no such thing as an immutable byte array in Java. What we are trying to achieve here is to protect the byte array inside the object from which it is being returned. We want to caller to have the contents of the byte array, but not be able to change the contents inside the object. The trick is to return a copy instead of the internal reference:
public class ReturnByteArray {
public stati...
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...