Tag: Java

  • SIMsalabim

    As already announced a bit more than a week ago, I’ve published one smaller project from the University on Google Code. Now I’m proudly presenting you…

    SIMsalabim

    SIMsalabim is an Android application which allows you to manage the contacts which are stored on the SIM card inside your phone. Google Android itself has so far only minor possibilities to work with SIM contacts, it’s just possible to copy the contacts from the SIM card over to the phone contacts and that’s it. SIMsalabim adds the missing management functionality to copy the contacts between these two lists and also remove contacts from the SIM card.

    Currently the project is in a usable state (and a preview APK package is also available here) but only tested so far on my HTC Magic phone using Android 1.6. Furthermore error handling in extreme situations (like a full SIM card storage) is not yet perfect. But I think it’s in a state where it should cause no unexpected effects on your phone.

    Nevertheless: use it on your own risk. It still may cause all sorts of bad effects and even harm your contact information. Just to be on the safe side 😉

    Some documentation and usage instructions are available in the wiki.

  • Java language pitfalls

    A recent blog entry posted in Argonauts blog deals with a C# codepiece which is valid at the first look and even compiles cleanly but failes gracefully with a runtime exception when executed.

    Argo shows the C# code for his example but (as he also mentions) the same code can be used in a Java example. The following class hirarchy…

    class BaseType {
    }
    
    class SubTypeA : BaseType {
    }
    
    class SubTypeA : BaseType {
    }

    looks innocent so far. But if it is used the following way

    public static void main(String[] args) {
      BaseType[] baseArray = new SubTypeA[3];
    
      baseArray[0] = new BaseType();
      baseArray[1] = new SubTypeA();
      baseArray[2] = new SubTypeB();
    }

    things get interesting. The code compiles without any problems (as in C#) but when executed, one is faced with an java.lang.ArrayStoreException. The cause for this is burried in the Java Language Specification 4.6 (thanks for the hint in the posts comments which saved me some searching). Upon compilation the type of the array is BaseType and after that it gets the SubTypeA-array assigned. The compiler does not know at this position that it would have to re-type the array it is assigning to to avoid the problems lying ahead. That’s why arrays are also checking their assigned objects at runtime and cause this exception if something invalid occours (as specified in JLS 4.7).

    I think that this problem would be solvable in Java, C# and most other languages which treat arrays the same way. But I also think that this would open a pandoras box ful of problems and complexity arising from this new constraint just for this specific issue. And personally I think this issue is not a common one and appears just in border-cases so that most developers can deal with it for now. Nevertheless I also think, this issue can be solved, it just may be too late for Java and C#.

  • My first steps with Eclipse Mylyn

    This is my long promised posting about my experiences with Eclipse Mylyn. It took quite some time because I did not stick to it all the time but somehow fell back to my old development-habbits where everything and much more was visible on the screen. Maybe this is caused by the always-changing project situation or changing technologies but I cannot completely blame it to that. Nevertheless, let’s focus on my experiences 🙂

    Mylyn works by hiding everything from your visual workspace which is not connected to your current task at hand. This means, you only see the files in the Package Explorer and all other many views which were touched during your current task. This also means that you have to create tasks for your work in the Task List, which is a good idea anyway. The Mylyn-filtering is activated in the moment when you click on the small circle next to such a task in the Task List. If you have worked before on that task, your previous state of the workplace (which is called Context here) gets restored, like the open files, the touched methods and landmarks. If this is a fresh task, you’re presented with empty views all around. From here you then can open your initial file to work on either by using the "Open Resource" shortcut or by ALT-clicking into the Package explorer, where the hidden files are displayed then.

    From that initial file on you just navigate through the structures and methods with your usual Eclipse navigation (F3, STRG-click, or whatever) and Mylyn takes care to just display and highlight the necessary information for your work. It even cleans up the stuff and removes old and never-again places if you didn’t came back to them for a longer time. The same thing applies to all content-assists like the method-overview or all of the Find-shortcuts where the list gets two-parted with your context-relevant results at the top and only the remaining hits after a separator.

    You can also mark certain methods or files as "Landmarks" which are then displayed in bold text in your Outline or Package Explorer. This is especially handy if you’re hunting a bug and found important places you want to remember or where you nailed down the cause but will fix it at a later time.

    I also tried to connect my Mylyn Task List with a local installation of Bugzilla to better manage my tasks and make them available even outside of my own workspace. This worked quite good. It’s also possible to attach the Context to each task if you’re working with an external task repository. This would allow other people on the same repository to open the Context in the same state as it was when you last saved it to the task repo. Easy moving tasks from one person to another 🙂 But we didn’t come around to test this, as I’ve been the only one to work with Mylyn longer than just for a short tryout.

    All in all I can say that if you get used to the way how Mylyn hides everything unnecessary from your display and how it presents the important information, the speed of development really goes up and the amount of distraction minimizes. But to get to that point you have to really work through the initial get-used phase which can be somewhat confusing and make you switch back to the previous development-mode where everything is displayed. I also fell into this a few times. But if you stick to it, you’ll soon get the benefit of being able to focus!

    If you also want to try it out or are just curious how it feels to work with it, take a few minutes and watch the Eclipse Mylyn 3.0 webcast video and read through the Mylyn 2.0 Tutorial (still applies 100% even if it’s been written for 2.0) how to get started, you won’t regret it!

  • Know your frameworks when developing software

    In the last few weeks I’ve got the chance to look into the sourcecode in a lot of different modules of the product our department is working on. The more code I had a look at the more a certain insight was forming in my head. What I saw when I looked at the different parts of the sourcecode from a lot of different people is, that it’s cruical for the maintainability of code to know the advantages and possibilities of the frameworks you’re working with. Otherwise you’re going to reinvent the wheel over and over again. And I recognized many different wheels in this code.

    We’re working with Java here and among our used frameworks is also the Apache Commons. It’s a framework which greatly eases the solving of common problems every Java developer faces every day. To give examples I’ll focus on working with collections here. The Java framework itself has already some convenience methods available when working with Collection classes in the their generic interface classes (like Collection, List, Set or Map) but the Commons Collections Classes provides further methods which cover many day-to-day operations with their xyzUtil classes (see CollectionUtils or ListUtils for examples).

    A lot of the operations I came across in our code deal with creation and manipulation of different collections, transforming from/to arrays, intersecting or summing them and so on. Lots of loops and different ways to solve always-repeating problems. With the utility-classes and methods from the frameworks we’re using a lot of these sourcecode-parts can be reduced and simplified from up to a few dozend lines to just a few ones or in a lot of cases even a single line.

    Not only does this reduction aid the readability and comprehensibility of the code it also makes it more robust as most of the framework-methods are already for example null-safe and thus take the burden of null-checks from the developer. And less code is easier maintainable than endless pages of difficult logic. Of course, the usage of frameworks also requires the developers who get at it later on to have at least a little knowledge of these but the invested time to get familiar with the framework almost always pays off manifold.

    PS: Another extremely useful utility class in the Apache Commons is the StringUtils class. Just have a look at the impressive convenience methods in its interface and I bet there are many methods in there which you immediately could use at some places of your code.

  • JBoss Web Service issues with JDK6

    The past two weeks I’ve working on an exercise for the university. The task was to create [Enterprise Java Beans][1] (EJB) and [Web Services][2] (WS) which were then deployed to and run on the [JBoss Application Server][3]. We were using Eclipse Ganymede EE, Java JDK6 and JBoss version 5.0.1 GA for our purposes.

    My colleagues and I had quite some troubles getting it all to work but eventually we managed it because most of the issues are documented and their resolutions are available on several sites somewhere on the net. Only this particular issue seemed to be covered nowhere or if it was, the solutions suggested did not work for us. I’m giving you the solution that worked for us here, maybe it helps somebody who runs into the same issues we’ve experienced.

    Many people seem to succeed with the [Instructions for using JBoss5 on Java6][6] from the JBoss Getting Started Guide, but for whatever reason this did not work for us. If you have the same problem persisting even after copying around the library-files, read on.

    I hope the solution present here can also help you with your problem. If you have anything to add, find a mistake or can give any other feedback, please leave me a comment.


    The issue

    JBoss is up and running without trouble, having webservices deployed and visible on the webservice-endpoint overview at http://localhost:8080/jbossws/services. But upon calling one of the methods provided the JBoss chokes with the exception:

    ERROR [SOAPFaultHelperJAXWS] SOAP request exception
    java.lang.UnsupportedOperationException: setProperty must be overridden by all subclasses of SOAPMessage
        at javax.xml.soap.SOAPMessage.setProperty(SOAPMessage.java:445)
        at org.jboss.ws.core.soap.SOAPMessageImpl.
    (SOAPMessageImpl.java:82) at org.jboss.ws.core.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:215) … (and some more exceptions with “setProperty must be overriden…” appearing several times) and the webservice call fails with no result. ## Quickly explained solution 1. install JDK5 and add it to Eclipses installed Runtime Environments 2. set JBoss execution environment to use JRE5 3. set JRE in webservice-projects buildpath to JRE5 4. set Project Facet in webservice-project for Java to 5.0 ## Detailed solution 1. [download JDK5][4] from Sun 2. install JDK5 (only JDK needed, no seperate JRE, docs, etc.) 3. add JDK5 to Eclipse as Installed JRE (Window->Preferences->Java->Installed JREs->Search…) 4. change startup configuration of JBoss to JDK5 (doublecklick on JBoss-entry in Servers-tab->General Information->Open launch configuration->JRE->Alternate JRE) 5. change the projects JRE System Library to use JDK5. Either via right-click on the system library entry in the project->Properties or right-click on the project->Build Path->Configure Build Path->select “JRE System Library”->Edit. Then set the Alternate JRE to JDK5 6. change the projects Java facet to JDK5 (right click on project->Properties->Project Facets->Change the setting next to the “Java” entry to “5.0” 7. check, that in %JBOSS_PATH%/server/default/deploy (or your own configured deploy-path in JBoss) there are no .war-files left from your current project 8. if not already suggested by Eclipse itself, rebuild the webservice project (if building automatically just clean the project via Project->Clean…) 9. start JBoss via Eclipse 10. check that somewhere near the beginning of the JBoss console log something like “Java Version: 1.5.xxx Sun Microsystems Inc” or “Java HotSpot(TM) Client VM 1.5.xxx” appears as “INFO [ServerInfo]” log line If you are receiving errors like “java.lang.UnsupportedClassVersionError: Bad version number in .class file” then there is still something connected with JDK6 left in your project. Check the steps again and also your included libraries if there is something suspicious and then rebuild the project. After all these steps you should finally be able to call web service methods on your endpoint without causing those exception anymore. ## Issue background The technical details and origins of this error are explained in [JBWS-2649][5] along with an [initial solution][6]. In short JRE/JDK6 includes a dummy-implementation for this setProperty() method which overrides the required implementation which is supplied with JBoss. The mentioned initial solution solves the problem by copying the supplied libraries to a location in the classpath where it should be loaded before the JRE libraries but as already mentioned this did not work for most of our class. [1]: http://java.sun.com/products/ejb/ “Enterprise JavaBeans Technology” [2]: http://java.sun.com/javase/technologies/webservices.jsp “Web Services” [3]: http://www.jboss.org/jbossas/ “JBoss Application Server” [4]: http://java.sun.com/javase/downloads/5/jdk “java.sun.com – Download SE Development Kit 5.0” [5]: https://jira.jboss.org/jira/browse/JBWS-2649 “JBWS-2649: SOAPMessage implementation bug” [6]: http://www.jboss.org/file-access/default/members/jbossas/freezone/docs/Installation_And_Getting_Started_Guide/5/html_single/index.html#Java6_Notes “Installation and getting started with JBoss on JDK6”