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

Friday, October 16, 2015

Custom PMD Rule creation

based on PMD 5.4, following XML should be just copied to pmd configuration file

This rule is extension to ShortVariable with update to skip validation of methods with Override annotation

Rule:
<rule name="CustomShortVariable"
message="Avoid variables with short names that shorter than 2 symbols: {0}"
language="java"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="">
<description>
Fields, local variables, or parameter names that are very short are not helpful to the reader.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//VariableDeclaratorId[string-length(@Image) < 2]
[not(ancestor::ForInit)]
[not(../../VariableDeclarator and ../../../LocalVariableDeclaration and ../../../../ForStatement)]
[not((ancestor::FormalParameter) and (ancestor::TryStatement))]
[not(ancestor::ClassOrInterfaceDeclaration[//MarkerAnnotation/Name[pmd-java:typeof(@Image, 'java.lang.Override', 'Override')]])]
]]>
</value>
</property>
</properties>
</rule>

Tuesday, March 10, 2015

Catch Exception or catch Throwable when you need to log anything


When you ask yourself a question what to catch in main method Exception and Throwable - use Throwable to catch all unexpected problems for sure.

if you choose to catch Exception you will lose Errors ,and Errors are not only system problems.

interesting Error that could happen in business logic + misconfiguration
http://docs.oracle.com/javase/7/docs/api/java/lang/ExceptionInInitializerError.html

initialization of non static field

Configuration.getBean(FileToCsvExtractor.class);

declaration of Configuration

public enum Configuration {
INSTANCE;

private AbstractApplicationContext context;

private Configuration() {
this.context = new AnnotationConfigApplicationContext("com.mycompany");
}

public static <T> T getBean(Class<T> clazz) {
return INSTANCE.context.getBean(clazz);
}

public void destroy() {
context.close();
}

}


If you somewhow did a mistake in from and Configuration will fail to initialize then getBean is failed with ExceptionInInitializerError, but it have a cause Exception that could be like:

Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mycompany.ExecutableImpl.execute(LdLogRenaissanceDeployJob.java:23)
at com.revere.xfeed.customclients.renaissance.MyTestClass.main(MyTestClass.java:8)
Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: myproperties.properties (No such file or directory)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)


so you have to catch Thowable in main method to log all problems before exiting a application.

interface Executable {
void execute(Object args) throws Throwable;
}

class ExecutableImpl implements Executable {

public void doAll() throws IOException, FTPException {
                        Configuration.getBean(FileToCsvExtractor.class);
throw new IOException();
}

@Override
public void execute(Object args) throws Throwable {
try {
doAll();
} catch (Throwable th) {
throw th;
}
}

}

or do "throws Exception" in interface , no other changes are required,  but that will kind if lie to developers as you know that code could throw ExceptionInInitializerError :) even it mean to be RuntimeException.

More interesting cases where catching Errors is ok:
http://blog.igorminar.com/2008/05/catching-stackoverflowerror-and-bug-in.html
http://programmers.stackexchange.com/questions/209099/is-it-ever-okay-to-catch-stackoverflowerror-in-java

Smb in code could use Errors http://docs.oracle.com/javase/7/docs/api/java/lang/AssertionError.html :) as a validation of some sort, .....


One more case of Error that is ok finish gently application  - is
java.lang.NoClassDefFoundError: com.mycompany.ISqlRowMapper
....
Caused by: java.lang.ClassNotFoundException: com.mycompany.ISqlRowMapper
it is completely to ok to catch it log it!! Yes it was terrible misconfiguration in maven level, but UTs were passed and problem appear only in fully deployed application  and in the middle of execution (not at start).

Friday, June 27, 2014

Get java process heap dump and analyse it by jhat


1. Get all java processes ids
17:17 ~/java $ jps -vm
14970 Jps -vm -Dapplication.home=/usr/lib/jvm/java-7-oracle -Xms8m
8850 org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar -data /home/rivanov/workspace -os linux -ws gtk -arch x86_64 -showsplash /home/rivanov/ ..................... -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx512m

2. generate heap dump for application, in my case it is Eclipse.
17:19 ~/java $ jmap -dump:format=b,file=eclipse_heap.bin 8850
Dumping heap to /home/rivanov/java/git/dmt/eclipse_heap.bin ...
Heap dump file created

3. run jhat tool from JDK to analyse heap dump.
17:19 ~/java $ jhat eclipse_heap.bin 
Reading from eclipse_heap.bin...
Dump file created Fri Jun 27 17:19:37 PDT 2014
Snapshot read, resolving...
Resolving 1548547 objects...
Chasing references, expect 309 dots.....................................................................................................................................................................................................................................................................................................................
Eliminating duplicate references.....................................................................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.


4. Open in browser "http://localhost:7000/".
jhat is far from fancy heap dump analyser in comparison to Eclipse's MAT, jvisualvm, ...... but it is provided together with JDK and nothing should be installed .


Monday, February 17, 2014

Hashcode and Equals method usages in Java


Known practice - do not override neither of equals(Object obj) and hashcode() or override them together.
But even after override all of them and implementing properly or even generating code for them by smart tool - there could be a problem.

Advise: All class implementations that override hashcode() and are going to be used in Hashed collections - have to be immutable.

Without following advice above you can end up in situation then you put object in HashSet, change object's field , try to find it in collection by contains() - and you will not find it in collection as hashcode is changed and bucket will be misleading as object is placed in old bucket but hashcode is calculated to point to another bucket (wikivisualization).

Advise: "hashcode()" is Java system related method - do not override it till you have no other choice !!!

Advise: If hashcode() is demand after already overridden "equals(Object obj)" change code to use Comparable or any other approach, it will not cost any time for additional codding but will safe you form problems in future when your systems will be huge.

Reference to other  tools:
FindBug:
HE: Class defines equals() but not hashCode()
HE: Class defines equals() and uses Object.hashCode()
HE: Class defines hashCode() but not equals()
HE: Class defines hashCode() and uses Object.equals()
HE: Class inherits equals() and uses Object.hashCode()

PMD:
http://pmd.sourceforge.net/pmd-4.3.0/rules/basic.html#OverrideBothEqualsAndHashcode

Checkstyle:
http://checkstyle.sourceforge.net/config_coding.html#EqualsHashCode

Summary:
All tool check for methods existence but non of them for immutable state.
Is it on purpose or it is non covered problem of Static Code Analyzers for Java ?

Tuesday, November 19, 2013

Wednesday, November 6, 2013

Checkstyle idea - require toString() method for Classes

toString() is required for classes that do logging as error to print context of them-self and around.

it might be reasonable to put in toString() only simple types fields that are provided by setXXXXX().

Johua Bloh idea about toString() for classes - Effective Java 2 book - "Item 10: Always override toString" - http://jtechies.blogspot.com/2012/07/item-10-always-override-tostring.html.

Having useful toString() make logging of context during error a simple and pleasant task.

Questions: What criteria we could follow to make a Check that will not force all Classes to have toString() on domains and critical for logging?

Override toString() everywhere is not practical, so we need figure out distinctive characteristics of Classes that have to do it.
- Class that is POJO
- ....

if somebody have ideas please share them on https://groups.google.com/forum/?hl=en#!forum/sevntu-checkstyle or https://groups.google.com/forum/?hl=en#!forum/checkstyle-devel

Friday, October 18, 2013

Saturday, August 31, 2013

Exception logging: 'log and rethrow' vs 'wrap and throw' vs 'log context and re-throw'

If Exception happen during any SQL call to DB server - Where it is good to catch and log it and how to do it?

There is no exact answer, but there are few ways to do it, and some of them have problems that you need to consider.

Case "log and re-throw":
} catch (SQLException e) {
               log.error("Exception occurred ,for sql [" + sql + ]", e);
               throw e;
}

Case "wrap and throw":
} catch (SQLException e) {
           throw new XXXXXDataAccessException("Exception occurred ,for sql [" + sql + ]", e) ;
}

Case "log context and rethrow":

} catch (SQLException e) {
               log.warn("Exception is appear, stack-trace see further in logs, failed sql is [" + sql + ]");
               throw e;
}

Case "log and re-throw":
In log and throw - log with Error or with Warning ?
If we do it with ERROR level - you will brake good pattern "one event - one ERROR message in logs", link to read.
If you will log with WARN - you will spoil logs with duplicated print of stacktrace. On moment of logs review it will not be clear (without review of sources) either exception happen twice or it was logged twice or ....   

General rules for logging and re-throwing:
 - if you develop Library - never log with ERROR level - library does not know how it is used so he have to return all to application and application decide how and when to log it, or even ignore it. Such problem existed in Hibernate, Hibernate, Hibernate, Hibernate, and looks like resolved in Hibernate 4.X only.
- if you are in Application - do logging on level that do know how to treat/translate/process that exception and make exception part of business logic(your algorithm).

Case "wrap and throw":
If you do not log sql and trow exception up, upper level does not have information what sql was in call. You can not log sql with ERROR level at place where you have SQL as is SQL is not an ERROR , exception is a ERROR event - log and rethrow is also bad practice (link to read). Wrapping to your exception make you have custom exception objects and ask yourself and Google why there is no such standard exception - it is so obvious (ones you end up in creation set of custom exceptions in each application).

if you so like to follow "big brothers" - non of these exceptions classes store SQL:
for 15 year off massive SQL usage in java non of them wanted to have it in class as member, because SQL is only one part of problem not a exact reason. 
Non of them have sql as member in class:
http://docs.oracle.com/javase/6/docs/api/java/sql/SQLException.html
https://github.com/hibernate/hibernate-orm/tree/master/hibernate-core/src/main/java/org/hibernate/exception


Case "log context and rethrow":
So logging with WARN is ok, but smb could yell "Error should encapsulate all details of it", and here is nuance - you will never put in Exception object all context, even previous events in logs is a context as you need to see what application did before ERROR. So I recommend to log context of SQL and parameters if any with WARN level and do rethrow exception without wrap.

Main idea is - as you got exception and that level can not log it or process it properly - just print context of exception with WARN level an re-throw it further. Context could be anything: all set of class members, any local variables, whatever will be useful to know state of class when exception happen.

Make a rule for yourself to search for exception in logs and then view events before ERROR event to see a context of problem not only be cause you did logging of exception context but due to the fact that previous events are also context , and they could be even more informational and helpful than smth that you did.

Attention: You can not fix a problem by just looking at exception stacktrace and its message - you need wide context of what happen in application before!

Wednesday, July 10, 2013

How to resolve system variables in spring xml configuration

during Tomcat launch you can specify system variable as ".... -Dmy.value=Hello .... "

but do not forget to put in you Spring configuration following bean:
<bean id="system-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

to make this variable resolvable in any other bean configuration like:
...

<entry key="myValue" value="${my.value}" />
..

Sunday, April 7, 2013

Java DecimalFormat example


Sites to launch java code online:
http://www.compileonline.com/compile_java_online.php
http://www.javalaunch.com/JavaLaunch.jsp (no restrictions)
http://rextester.com/runcode (attention: it restrict class to be named as Rextester) list of all, here .

Task: Print 0.1278 as 12.8%. java doc.

 Code:
import java.text.DecimalFormat;

public class Test {

 static public void customFormat(String pattern, double value) {
  DecimalFormat myFormatter = new DecimalFormat(pattern);
  String output = myFormatter.format(value);
  System.out.println(value + "  " + pattern + "  " + output);
 }

 static public void main(String[] args) {
  customFormat("000.0%", 0.1278);
  customFormat("#0.00%", 0.1278);
  customFormat("###.##%", 0.1278);
  customFormat("##0.0%", 0.1278); ////!!!!!
  customFormat("#.####%", 0.1278);
 }

}

Friday, February 8, 2013

Jps and jstack for java process management


Get process id:
/usr/java/bin/jps -v | grep my_app
16831 ......

Show stack of execution in process to define place of hang:
/usr/java/bin/jstack 16831


You can kill that process:
kill 16831

Monday, December 3, 2012

Books to read about Java and best practices

Good todo list for reading to improve your skills of java developer: http://habrahabr.ru/post/153373/
one day I will have to time, to do it :) .

Tuesday, September 11, 2012

eCoberture is dead, Eclemma is a king


There are two major projects for coverage testing in Eclipse: eCobertura, EclEmma.

EclEmma is active and good for Eclipse.

eCoberture is outdated and have painful bug with clean up coverage, but cobertura it is used in Sonar project.

Friday, March 16, 2012

Fedora 16 java plugin to FireFox

Even after installing jre, you will have problem with java applets in FireFox.
javaws http:/........xxxxxx.jnlp does not work - error "Command not found".

Useful link:
http://java.com/en/download/help/linux_install.xml#rpm

Logs of commands:

cd /usr/lib/firefox/plugins/
sudo ln -s /usr/java/default/lib/i386/libnpjp2.so
Restart FireFox