Refreshing eclipse workspace with ant script

Did you knew that if you have ant build.xml in eclipse Ant view then you can refresh your eclipse workspace simply using:

<eclipse.convertPath filesystempath=”${basedir}” property=”resource.basedir”/>
<eclipse.refreshLocal resource=”${resource.basedir}” depth=”one”/>

?

Very handy for build scripts to make sure eclipse resources are in sync after the build.

Thanks to Roland for the tip!

Getting unsigned bytes in java

Welcome back after long overwork and vacation period :) Let’s start small, it’s still almost summer.

Bytes in java are always signed and are quite horrible to use because of the casting and weird side-effects (or I’m just stupid enough to call it weird).

But we need unsigned bytes quite a lot. Most of the time for reading various streams (networks, 8bit strings etc) some text stream you still end up with byte streams.

There is a nice trick converting them from signed to unsigned in case you want to get normal 0..255 range for the chars.

byte mybyte = -104;
long mynewint = mybyte & 0xFF;

Hope it helps!

I had quite a nice puzzling while trying to find some easier way. Is there?

Getting TPTP installed in Ubuntu – first post from Ivar

One of the Codehoopers started to blog about Eclipse and programming, welcome aboard! :)

As he doesn’t have much readers yet I guess it’s ok to point to his first post: Eclipse TPTP and Ubuntu”

It’s about getting TPTP running. Especially under Linux. Don’t underestimate the complexity of this – it’s not that easy :)

Nice post, thank you!

PS. Some old-time readers might remember that Ivar has been featured at this blog also before – Four is the Magical Number – and so is 5920.

Enabling 3D with ATI and NVIDIA – ubuntu and debian

hmm.. getting 3D enabled for ATI and NVIDIA is still very tricky. With ubuntu you can recompile a kernel package AND/OR recompile restricted modules AND/OR compile a custom kernel with ATI/NVIDIA modules. There are 4-5 different options for getting it done. Each has it’s drawbacks. And even after spending lot of time figuring it out – next time you update the kernel, you’re screwed again :)

You can scratch your head for hours wondering why everything should work but glxinfo shows Mesa as active driver. You need direct rendering! Even if you’re not playing any games. 2D rendering saves a lot of CPU and linux runs much faster.

For ATI, when you download the linux proprietary driver installer – I’ve never seen it working without any special tweaks. Plus their installation guide is quite out-dated and not in sync with the real installer.

SO – Here’s the lifesaver – Envy! It’s a small utility that takes care of these tweaks and with a very simple user-interface fetches all the important packages, installs all the properietary stuff and makes sure that conflicting drivers are disabled/uninstalled/removed.

I gave it a try and – this thingy started from scratch and – I was done in 5minutes – everything working great!



Envy main screen

Eclipse workspace fails to start after crash – quick fix

Here’s a quick HOWTO for resolving Eclipse startup workspace loading problems. If you have a better suggestion then please leave a comment!

There can be a lot of other issues but here is one of the quite common ones – eclipse resources cache or state gets out of sync/corrupted.

This can happen when your IDE crashes OR also without crashing. For non-crashing case it’s more likely a logic/bug problem and whenever possible, try to file a bug at bugs.eclipse.org!

Sometimes, just SOMETIMES, you’ll get exceptions like following when launching Eclipse IDE after crashing it:

Root exception:
org.eclipse.core.internal.resources.ResourceException(null)[567]:
java.io.UTFDataFormatException: malformed input around byte 394
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at org.eclipse.core.internal.resources.WorkspaceTreeReader_1.
readPluginsSavedStates(WorkspaceTreeReader_1.java:111)
at org.eclipse.core.internal.resources.WorkspaceTreeReader_1.
readTree(WorkspaceTreeReader_1.java:159)
at org.eclipse.core.internal.resources.SaveManager.restoreTree(SaveManager.java:916)
at org.eclipse.core.internal.resources.SaveManager.restore(SaveManager.java:647)
at org.eclipse.core.internal.resources.SaveManager.startup(SaveManager.java:1319)
at org.eclipse.core.internal.resources.Workspace.startup(Workspace.java:1949)
at org.eclipse.core.internal.resources.Workspace.open(Workspace.java:1713)
at org.eclipse.core.resources.ResourcesPlugin.start(ResourcesPlugin.java:363)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$2.
run(BundleContextImpl.java:999)
at java.security.AccessController.doPrivileged(Native Method)

The key here is row “org.eclipse.core.internal.resources.SaveManager.restore(SaveManager.java:647)”.

This stakctrace means your root .tree file is damaged and thereby unable to restore your opened projects.

The quickest way to fix this or any other resource-related exception is to:

  1. Shut down Eclipse IDE
  2. Remove and backup your workspace/.metadata/.plugins/org.eclipse.core.resources directory
  3. Start Eclipse IDE (with -clean to be super-safe)
  4. Reimport all projects (UPDATE: Just use File->Import->Existing Project into Workspace and browse your workspace/project directory)
  5. Enjoy

Maybe deleting workspace/.metadata/.plugins/org.eclipse.core.resources/.root/*tree would have been good enough for my case but it’s definitely not a workaround for other possible workspace resource state and cache related exceptions.

I’m not sure if this information without any reproducible steps is useful for bugs.eclipse.org. Not filing it this time. Or should I?

Configuring Eclipse SVN (Subclipse) to use JavaHL(JNI)

Configuring JavaHL for Eclipse is not as straightforward as it could be so posting a quick HOWTO. Following applies mostly to Linux users.

With Ubuntu the fool-proof steps are:

  1. Make sure libsvn-java, subversion and libsvn1 packages are installed
  2. Add following to the end of your eclipse.ini file: -Djava.library.path=/usr/lib/jni
  3. Restart eclipse and make sure JavaHL is selected under Window->Preferences->Team->SVN

By using JavaHL it gets more likely that your svn repo doesn’t get screwed up when using both command-line/shell and subclipse for svn.



Configuring Eclipse to use native SVN client – JavaHL

New Memberships

Just for the record, Codehoop is now the member of “Eclipse Friends” and official OSGi Supporter.

Avoid useless Plugin Initialization and/or Class Loading

Here’s a tip for initializing your plugins or any other singleton-like repository only when needed.

Consider a code like this:

if (isConfigurable()) {
  Bundle bundleCopy = bundle;
  Preferences[] preferencesCopy = new Preferences[1];
  preferencesCopy[0] = new org.eclipse.core.internal.
			preferences.legacy.PreferenceForwarder(
			this, bundleCopy.getSymbolicName());
  return preferencesCopy;
}
return null;

Do you see what’s wrong there?

The problem is that if your #isConfigurable is always FALSE then your code still:

  1. loads PreferencesForwarder class
  2. initializes all PreferencesForwarder’s static variables
  3. forces Preferences plugin to load

Always, if possible and makes sense, isolate your class loading into a simple Runnable inner class if there is some static init or plugin activation trigger involved!

Then the code would become:

if (isConfigurable()) {
  final Bundle bundleCopy = bundle;
  final Preferences[] preferencesCopy = new Preferences[1];
  Runnable innerCall = new Runnable() {
    public void run() {
      preferencesCopy[0] = new org.eclipse.core.internal.
			       preferences.legacy.PreferenceForwarder(
				 this, bundleCopy.getSymbolicName());
    }
  };
  innerCall.run();
}
return null;

When you think of it – this pattern is somewhat similar to the singleton pattern implementation – the one of the very few thread-safe and fast singleton patterns that is bugfree and truly lazy!

Quoting wonderful singleton impl from wikipedia:

public class Singleton {
   private Singleton() {}
   private static class SingletonHolder {
     private final static Singleton INSTANCE = new Singleton();
   }
   public static Singleton getInstance() {
     return SingletonHolder.INSTANCE;
   }
 }

(OK, to be fully honest, since java1.5 you can also trust volatile flags but – trust noone, use the old stuff:P)

For the reference, the PreferencesForwarder example was taken from the org.eclipse.core.runtime.Plugin#getPluginPreferences class and modified a bit.

UPDATE: This post is not fully correct! Never assume something to be true even if it comes from a trustworthy source like eclipse core runtime source. Please see the comments for explanation!

Eclipse Top 5 Annoyances

Here is the list of Top 5 Eclipse Annoyances we came up today at the coffee-table, possibly nice to share (in the order of importance).

Also serves as a good wish-list to my Santa Claus!

1. Update Manager

With the help of p2 provisioning and hard work this will be hopefully solved for 3.5. Please don’t mess it up this time! :)

There was also some provisioning-related work done as part of 2007 Google Summer of Code.

When talking about “enemies” – Netbeans update manager is much more decent (yet less flexible)

2. Cannot have editors for “no-extension” files
no-Extension-Support bugzilla

Well, files without an extensions SHOULD be treated as any regular file – don’t make them feel like crap! :)

YET! At the same time we treat filenames without a filename properly – .project has the extension but no filename :P

3. Opening files from command line
command-line file opening bugzilla

Getting closer I hope, there is a summer-of-code project related to command line commands (about triggering eclipse actions from web-sites). Plus a proof-of-concept thin text-editor padclipse.

Dear Eclipse, give command-line file-open and other configurable command line options. My neighbor Netbeans has it, I want it too.

4. Word Wrap
word wrap bugzilla

HAHAHA! You can take it from here, the official eclipse word wrap page. It works but has it’s drawbacks like bugs with ruler line numbers etc.
More seriously – it won’t be there in any near timeframe IMHO. Architecturally there has been a little flaw in the very core of Eclipse – SWT.

In SWT StyledText making up all these editors is missing a difference between physical and visual line numbers. Because of that a lot of plugins use one set of API methods for both visual and physical line numbers. To make sure transition is causing no harm, existing API should probably keep showing physical line numbers. But that would break quite a few things – rulers etc.

Not sure how many 3rd party plugins use line info and how to treat them nice while transitioning. Or if that ever happens.

5. Split Editor
Split Editor bugzilla link

What the heck is with this feature? People vote and ask for it.

Do you really need this? You can have many editors for the same file anyway just by choosing New Editor from the tab view popup.

What’s your greatest Eclipse New-Year-Wish?

Summer of Code: writing PHP plugins

Toomas finished the official part of the google summer of code program. It was an honor to be the mentor and I had/still have a lot of fun with him.

Grab The PHP writing plugin for Eclipse from the update site and check it out if it works for you!

Sorry, but java6 and eclipse 3.3 only! (jsr-223 scripting engine starts from java 6)

Quercus GPL-licensed PHP Fragment is not mandatory – the plugin supports also javascript (and possibly all JSR223 scripting engines).

Have fun and let him know if you find some cool usage for it. Commenting at his blog is a good start.
I had to disable comments at my blog :(

A cool feature would be to write a small snippet that sends selected text to some rafb/etc pastebin and prints the url to the eclipse console view.