ru_java, posts by tag: tips - LiveJournal (original) (raw)
Как в JDK 1.6 реализован SQLException.iterator() и как это можно упростить
June 25th, 2007
Начиная с 1.6, SQLException реализует Iterable; подробности здесь.
**public** Iterator<Throwable> iterator() {
**return** **new** Iterator<Throwable>() {
SQLException firstException = SQLException.**this**;
SQLException nextException =
firstException.getNextException();
Throwable cause = firstException.getCause();
**public** **boolean** hasNext() {
**if**(firstException != null || nextException != null
|| cause != null)
**return** true;
**return** false;
}
**public** Throwable next() {
Throwable throwable = null;
**if**(firstException != null){
throwable = firstException;
firstException = null;
}
**else** **if**(cause != null){
throwable = cause;
cause = cause.getCause();
}
**else** **if**(nextException != null){
throwable = nextException;
cause = nextException.getCause();
nextException = nextException.getNextException();
}
**else**
**throw** **new** NoSuchElementException();
**return** throwable;
}
**public** **void** remove() {
**throw** **new** UnsupportedOperationException();
}
};
}
[Tip of the day][Очередное извращение] Return a Property Without System.getProperty
June 11th, 2004
Return a Property Without System.getProperty
On the JVM, System.getProperty()will only return a property if that property has been set using the -D flag. But what if a property has been set using EXPORT on a UNIX platform? Use this code to return the property:
Process p = null;
try
{
p = Runtime.getRuntime().exec("env");
java.util.Properties pr = new java.util.Properties();
pr.load(p.getInputStream());
System.out.println("Property : " + pr.getProperty(propName));
}
catch(Exception e)
{
e.printStackTrace();
}