Results tagged “Java”

Since I've started developing Java in Eclipse I have realized that this is in no way different than with most other IDEs: the speed of development highly depends on the knowledge and intuitive usage of keyboard shortcuts.

Occassionally I'm asked to slow down a bit so that one can follow my actions or to tell which shortcut I've just used. For this reason I'm making this small list of those shortcuts which I'm using pretty much all the time.

I've tested these shortcuts specifically on Eclipse Galileo (3.5.1) and on Windows but most of them are also already present on earlier versions of Eclipse. As for other platforms I don't really know if all of them (or even any one of it) are available (or at the same combination). Furthermore many of them should also be present when developing different programming languages than Java but I cannot confirm that. You just have to find out yourself. Just in case you do, please let me know in the comments :)

So without further ado:

Shortcut Function Description
CTRL+SHIFT+ROpen Resourcebrings up a dialog where you can quickly locate any file or resource in your workspace. Wildcards are supported in the search.
CTRL+SHIFT+OOrganize Importscleans up the imports-block at the beginning of the file. Adds missing imports (if neccessary) and removes unused ones.
CTRL+SHIFT+FFormatapplies proper formatting to the source code according to the style-settings in the *Java->Code Style->Formatter* preferences.
ALT+UP/DOWNShiftmoves the current line or selection up or down one line. Very useful for fast relocation of code, much faster than cut/paste if just moving a few lines.
CTRL+ALT+UP/DOWNDuplicateduplicates the current line or selection above or below the current line/selection. Allows the fast population of cases in switch/case statements if they differ just marginally or similar situations like variable-initializer blocks. Beware, that this shortcut is often also overridden by some Intel display drivers. If this one does not work for you, try to turn off or change the hotkey assignment in your display driver helper application.
CTRL+OQuick Outlnebrings up a small dialog which allows to search and navigate the outline of the current file. Just start typing to narrow down the results. Supports wildcards. Pressing CTRL-O again toggles the display of inherited methods.
CTRL+PGUP/PGDOWNSwitch Tabchanges to the next/previous editor-tab.
ALT+SHIFT+LExtract Local VariableCreates a local variable from the currently selected expression. Types are automatically added. Try it on the code 'new HashMap();' to understand how this can speed up your development. Works on variables, static strings, methods with return values, etc.
ALT+SHIFT+IInline Variableopposite of local variable extractions. Replaces all occurences of the currently selected variable with its assigned value. Only works if the value of the variable is set exactly once.
CTRL+ALT+HCall Hirarchyshows all calls/usages of the currently highligted variable/interface/class/method.
F3Open DeclarationJumps to the declaration of the currently highlighted variable/interface/class/method. Similar to holding down CTRL and clicking with the mouse on a variable/interface/class/method.
CTRL+SHIFT+LShow Key AssistDisplays a popup which displays currently assigned keyboard shortcuts. Very useful if you are looking for further shortcuts quickly. A shortcut for shortcuts ;)

There are many more shortcuts available, including the marking and navigation shortcuts which you should know from Microsoft Word, but the above ones are the ones which became ingrained in me and are now speeding up my Java development enormously. Try to get used to them and use them whenever you can and you'll realize that there will be fewer and shorter interruptions to your stream of thought and it also aids your concentration.

If you want to go a tad further and let Eclipse tell you about possible shortcuts (or even force you to use one by cancelling all mouse-events which have a shortcut) you can install the MouseFeed Eclipse Plugin which does exactly this by displaying a small shortcut reminder each time you issue a shortcutted command with the mouse. Although it's a plugin for Eclipse 3.3, it still works with Galileo.

| | Comments (1)

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.

|

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#.

|

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.

|

The past two weeks I've working on an exercise for the university. The task was to create Enterprise Java Beans (EJB) and Web Services (WS) which were then deployed to and run on the JBoss Application Server. 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 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.<init>(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 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 along with an initial solution. 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.

| | Comments (1)

  1 2

Archives