Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, August 05, 2010

Java verbosity again

(Repost of a comment I made somewhere on the topic of the verbosity of Java, and particularly its APIs)

The Reflection API is a fairly shocking example of the needless verbosity of Java. And I would consider the official API to be part of Java.

Here's a simplistic example in Java and Ruby, replacing characters in a string ("putty" => "puppy") first directly and then via reflective invocation:

// Java
String s = "putty";
System.out.println(s.replace('t', 'p'));
try {
Class c = s.getClass();
Method m = c.getMethod("replace", char.class, char.class);
System.out.println(m.invoke(s, 't', 'p'));
} catch (Exception e) {
e.printStackTrace();
}

# Ruby
s = "putty"
puts s.gsub("t", "p")
puts s.send("gsub", "t", "p")


This kind of verbosity is prevalent in Java's standard libraries, particularly when (anonymous) inner classes are involved. At the moment I'm struggling with "doPrivileged" blocks in some Java code which deals with sandboxing, and it is syntactically very unpleasant.

Ok, we could chain the Java calls together and knock some lines off, but it's still not nice:
try {
System.out.println(s.getClass().getMethod("replace", char.class, char.class).invoke(s, 't', 'p'));
} catch (Exception e) {
e.printStackTrace();
}

Friday, April 09, 2010

Subclipse: "An existing connection was forcibly closed by the remote host"

Subclipse "suddenly" stopped working, so I couldn't commit or synchronise to a svn repository anymore:
RA layer request failed
svn: Commit failed (details follow):
svn: OPTIONS of 'http://big-long-svn-path': Could not read status line: An existing connection was forcibly closed by the remote host.


Maybe a recent update of TortoiseSVN bolloxed it up, who knows... anyway, I worked around it by going to Team->SVN in Eclipse's preferences dialogue and changing the client in the "SVN Interface" section from JavaHL (JNI) to SVNKit (Pure Java). Works so far, although I had to re-enter the username/password which had been stored before.