In this blog I want to show implementation pitfalls of the singleton pattern and show you why enums are the better singletons. The singleton pattern is widely known and discussed a lot of times. Developers often prefer to implement a singleton using a final static field. But this approach has some pitfalls or rather malicious code can compromise the singleton pattern. In the next sections I will show you the possible attacks on singleton implementations that compromise the pattern. Maybe you will use this techniques to implement a kind of workaround some day, but I hope you will not. Static Final Field Singleton The purpose of a singleton is that only 1 instance of a… Read More »Singleton implementation pitfalls
An instanceof check is often an indication for misplaced source code. Some developer say that instanceof checks are ugly code, but there are reasons against instancof checks beyond a subjective feeling. In this blog I want to compare instanceof checks with polymorphism and discuss the downsides. This blog is intended to be used as a basis for desing decisions and discussions about code design. Instanceof motivations Everytime we write an instanceof check in java we are interessted in the capabilities of an object. We want to know if an object has specific features or methods or if it has not. This can also be implicit features that are derived from the type. In this case we have 2 options: Introduce… Read More »Instanceof vs. polimorphism
Reverting commits in git is quite simple if you want to revert a whole commit. But how to partially revert a commit? In this blog I want to show you a safe approach to partially revert a commit.
A lot of projects run integration and unit tests, but often miss a little something – logging. When you use logging you often check if the log level is enabled or not before you log. But this means that you introduce a control flow statement that enables or disables some code. So if you miss to enable logging for your tests you let code untested and bugs love untested code, because they can hide there. But it is easy to remove this hiding places for bugs. Avoid log level checks if possible If you can avoid log level enabled checks, you avoid control flow statements and this means that you remove hiding… Read More »Bugs love logging to hide
Some time ago I wrote the blog “Remove directories and files permanently from git“. Removing files from a git history is not an unusual task and I thought that it would be nice if there is an easier and faster way to do it. I thought that there should be a gui that help you to find big files in the history and that let you select the files you want to remove with just a click of a button. Therefore I started the GitDirStat project on github and developed a java swing application based on the JGIT library provided by the eclipse community. The name was derived from the popular WinDirStat application or… Read More »GitDirStat – a git maintenance gui
When multiple developers work on one project it is also essential that they all commit source code that matches some formatting rules. Otherwise reviewing changes and merging source code can be a great challenge. When using git repositories you can install hooks. Hooks are any kind of executables that can be run before something happens in a git repository like creating a commit or updating the refs during a push command. I developed a small bash script that can be used to execute checkstyle on every push and that rejects changes that have checkstyle errors. The script is simple and easy to configure so I want to share it here. Just… Read More »A git checkstyle pre-receive hook
In a git repository everything normally starts with an initial commit and all other commits are ancestors of this first commit. But is it possible to create a second initial commit or even a third? Another initial commit with a complete different working directory? Yes it is.
As of java 1.7 the java language specification allows integer literals to contain underscores in order to make them more readable. If it is applied in a good way it can enormously increase the readability of your code. In java 1.6 one had to define integer literals like this: int iterations = 10000000; long bytes = 0b11010010011010011001010010010010; With java 1.7 such integer literals can be written more readably by using the underscore as a thousands separators. int iterations = 10_000_000; or when using binary literals one can use the underscores to group bytes long bytes = 0b11010010_01101001_10010100_10010010; Further information can be found at http://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1 Recommended Reading
How to fix java.lang.ClassNotFoundException: com.ibm.websphere.ssl.protocol.SSLSocketFactory in eclipse
If you use IBM’s WebSphere plugin for eclipse you might have the problem that other plugins can’t establish an SSL connection anymore. The result is most times a ClassNotFoundException: com.ibm.websphere.ssl.protocol.SSLSocketFactory. This blog explains the reason behind the problem and gives a workaround to prevent that exception. Problem description When a component within eclipse (e.g. atlassian jira connector) tries to connect to an SSL socket a ClassNotFoundException: com.ibm.websphere.ssl.protocol.SSLSocketFactory is thrown. This exception is often the root cause of other exceptions. Caused by: java.net.SocketException: java.lang.ClassNotFoundException: com.ibm.websphere.ssl.protocol.SSLSocketFactory at javax.net.ssl.DefaultSSLSocketFactory.throwException(SSLSocketFactory.java:198) at javax.net.ssl.DefaultSSLSocketFactory.createSocket(SSLSocketFactory.java:205) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:116) at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:130) at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707) at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361) at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387) at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397) at com.sun.jersey.client.apache.DefaultApacheHttpMethodExecutor.executeMethod(DefaultApacheHttpMethodExecutor.java:210) … 18 more… Read More »How to fix java.lang.ClassNotFoundException: com.ibm.websphere.ssl.protocol.SSLSocketFactory in eclipse