Re: Style Police (a rant)
In article <slrnj6mj4j.6gl.avl@gamma.logic.tuwien.ac.at>, avl@gamma.logic.tuwien.ac.at says...
Wanja Gayk <brixomatic@yahoo.com> wrote:
It's a shame that in Java not all references are implicitly final and
only real variables get marked with "var" instead - that would serve the
same purpose with less effort and less visual clutter.
It seems like your general coding style differs from mine. The
percentage of re-assigned variables versus those assigned only
once is large enough, that a "var" keyword would cause more
clutter than putting "final" on each other variable.
I'd favor a different change: let final variables optionally
have their type inferred:
final myList = new ArrayList<String>();
Afterall, it is just a handle for some previously obtained value.
Well, is it?
I have a hard time believing that.
Could you be so kind to post some code?
Here's some undocumented reflection code I once wrote for a utility
(I also stripped the first indentation for this posting).
I thought I'd use that as I saw the class containted actually some code that
looked quite typical to my eyes at the first glance.
Though I know from my experience that most of my code has hardly any changing
variable in it, apart from some index or so, but I was pretty surprised to
see that there is actually not a single "changing" reference in it that had to
be marked as "var" in ther languages.
Just see for yourself and compare your coding style to mine, is it really that different?
class ApplicationWorkerUtil {
static interface InternalExceptionHandler {
void handle(final TimeoutException e);
void handle(final Exception e);
}
static Iterable<CancellableRunnable> getRunnables(final Object target, final Schedule lifecycle, final InternalExceptionHandler exceptionHandler) {
final List<CancellableRunnable> tasks = new ArrayList<CancellableRunnable>();
for (final Method m : getMethods(target.getClass(), lifecycle)) {
tasks.add(methodAsRunnable(target, m, exceptionHandler));
}
return tasks;
}
private static CancellableRunnable methodAsRunnable(final Object target, final Method method, final InternalExceptionHandler exceptionHandler) {
final Runnable r = new Runnable() {
public void run() {
try {
method.invoke(target);
} catch (IllegalArgumentException e) {
exceptionHandler.handle(new IllegalStateException("Unexpected state running a GUI-Job", e));
} catch (IllegalAccessException e) {
exceptionHandler.handle(e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
exceptionHandler.handle((Exception) e.getCause());
} else {
exceptionHandler.handle(e);
}
}
}
};
return new CancellableRunnable(r);
}
private static List<Method> getMethods(final Class<?> targetClass, final Schedule lifecycle){
final List<Method> methods=getDeclaredMethods(targetClass, new Predicate<Method>(){
public boolean matches(final Method candidate){
assert candidate != null;
final Job jobAnnot=candidate.getAnnotation(Job.class);
return jobAnnot != null && lifecycle.equals(jobAnnot.value());
}
});
ensureNoParameters(methods);
sort(methods);
return methods;
}
static <T extends Annotation> List<Method> getDeclaredMethods(final Class<?> c, final Predicate<Method> pred){
final List<Method> found=new ArrayList<Method>();
for(final Method m : c.getDeclaredMethods()){
if(pred.matches(m)){
AccessController.doPrivileged(new PrivilegedAction<Void>(){
public Void run(){
m.setAccessible(true);
return null;
}
});
found.add(m);
}
}
return found;
}
private static void ensureNoParameters(final List<Method> methods){
for(final Method method : methods){
if(method.getParameterTypes().length > 0){
final Job jobAnnot=method.getAnnotation(Job.class);
assert jobAnnot != null;
final Schedule lifecycle=jobAnnot.value();
final Class<?> declaringClass=method.getDeclaringClass();
throw new IllegalArgumentException("The method " + declaringClass.getName() + "#" + method.getName() + " annotated with " + lifecycle
+ " must not have any parameters.");
}
}
}
private static void sort(final List<Method> methods){
Collections.sort(methods, new Comparator<Method>(){
public int compare(final Method o1, final Method o2){
final int i1=o1.getAnnotation(Job.class).index();
final int i2=o2.getAnnotation(Job.class).index();
return i1 > i2 ? 1 : i1 == i2 ? 0 : -1;
}
});
}
static void waitForCompletion(final Iterable<Future<?>> futures, final long timeoutMs) throws InterruptedException, TimeoutException {
try {
final long timeoutTime = System.currentTimeMillis() + timeoutMs;
for (final Future<?> future : futures) {
if (!future.isDone()) {
if (timeoutMs < 0) {
future.get();
} else {
future.get(Math.max(0, timeoutTime - System.currentTimeMillis()), TimeUnit.MILLISECONDS);
}
}
}
} catch (ExecutionException e) {
throw new RuntimeException("Unhandled Exception:", e.getCause());
}
}
}
Kind regards,
Wanja
--
...Alesi's problem was that the back of the car was jumping up and down
dangerously - and I can assure you from having been teammate to
Jean Alesi and knowing what kind of cars that he can pull up with,
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---