Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-dev] Aspect for AspectJ Library: Transparent Usage of Preferences

Hello to everyone,

Here is a proposal for an aspect to be included in AspectJ library.

Problem statement:

Applications require preference and configuration data to adapt to the needs of different users and environments. JSDK provides Preferences API to store and retrieve such preferences. Below is an example how they can be used: import java.util.prefs.Preferences;public class Application { public static void main(String args[]) { Preferences appPrefs = Preferences.userNodeForPackage(Application.class); if (userPrefs.getBoolean("showWelcomeScreen", true) { showWelcomeScreen(); } ... }}public class PreferencesEditor { public void setWelcomeScreen(boolean on) { Preferences appPrefs = Preferences.userNodeForPackage(Application.class); appPrefs.setBoolean("showWelcomeScreen", on); } ...}The problem with such usage is that it is error prone. For example we can mistype the name of the property "showWelcomeScreen", we can also make wrong assumption about its type, for example it can be stored using numbers 0 and 1. Default value must be provided in each place where the property is requested. Refactorings which imply renaming or removing prefences can lead to undetected problems. Finally, it would be difficult to change the way application stores its properties.


Solution:

The idea is to provide preferences as static class fields and write a reusable aspect, which takes care about retrieving and storing these properties. The classes containing preferences are marked by implementing PreferenceContainer interface. public class AppPreferences implements PreferenceContainer { public static boolean showWelcomeScreen = true; /* initializer specifies the default value */ public static String logFile = "./application.log"; ...}public class Application { public static void main(String args[]) { if (AppPreferences.showWelcomeScreen) { showWelcomeScreen(); } ... }}public class PreferencesEditor { public void setWelcomeScreen(boolean on) { AppPreferences.showWelcomeScreen = on; } ...}Such solution make their usage of preferences simple and type safe. This way of preference usage supports useful IDE features, such as code completion and reference search. The way preferences are actually retrieved and stored (Windows registry, properties file, xml file, etc.) can be easily changed by configuring the aspect.

See the first version of the aspect in the attachment (tested on AJDT 1.2.0M3 + Eclipse 3.0.2). Comments are welcome.

Regards,
-----------------
Vaidas Gasiunas
Software Technology Group
Darmstadt University of Technology
Hochschulstr. 10
64284 Darmstadt, Germany

e-mail: gasiunas@xxxxxxxxxxxxxxxxxxxxxxxxxx
phone: +49 6151 16 5478
fax: +49 6151 16 5410
www: http://www.st.informatik.tu-darmstadt.de/

Attachment: Preferences.zip
Description: Binary data


Back to the top