Newsletters Subscriptions Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 37 -- June 4, 2003

Deprecating and Renaming Java Classes


Hey, David:

I have been using Java for several months now and realize that it would be best to move some of my Java classes to new packages. I would also like to break up some of my packages and rename others. Are there any cross-reference utilities that show where a Java class is used to make this easier?

--James


The best tool I know of for moving around and renaming Java classes is Eclipse. Since you are an iSeries Java developer, I recommend that you look into getting a copy of WebSphere Development Studio client (WDSc) for iSeries, which is based on Eclipse. Most iSeries development shops can order WDSc for free. (For more information on obtaining WDSc, read "WebSphere Development Studio Client for iSeries 5.0: Worth the Wait.")

Both Eclipse and WDSc have what you need to rename your classes and packages. To get started, install WDSc or Eclipse and bring your Java source files into a new project. To do this, open the Java perspective by selecting the Window drop-down menu and click Open Perspective and then Java. Next create a new Java project. Create the project using the File drop-down menu and click New and then Project.

After you have your new project created, right-click the project and select Import, followed by File System. In the file system import wizard, use the Browse button, then locate and select the root package directory for your Java source.

Once you have your project imported, you may see red X's on some or all of your packages, along with error messages, in the task panel. Those red X's appear on all packages that have errors, and in this case are likely to be related to class path problems. To clear up those errors, right-click your project and select Properties, followed by Java Build Path. Now click the Libraries tab.

In the libraries dialog, click the Add External Jars button to add JAR files that your project references to your class path. Use the Advanced button to add directories containing class files to your class path.

Once you have the class path errors corrected, you are ready to start renaming your Java packages and classes. Right-click any package or Java source file, and click Refactor and then Rename or Move. The refactoring wizard will guide you through renaming your packages and classes and will update any references to changed packages or class names.

One technique you might consider is to deprecate classes and methods you are not satisfied with. A deprecated class is a class that is no longer supported. When you deprecate a class, you are notifying classes that use the class that your intention is to remove the class at some point. Most IDEs and Java compilers will recognize that a class is deprecated and send a warning message. You deprecate a class or method by adding an @deprecated tag. You should add a comment after the @deprecated tag that explains why the class or method was deprecated and how the replacement works. Sun's Web site provides more information: How and When to Deprecate APIs. Here is an example of a deprecated method.

    /**
     * Method setProperty sets a named value by name. Setting the 
     * unique value to false allows duplicate properties with the same 
     * name to exist in a document.
     * @param property The name of the value to set
     * @param unique Pass false to allow multiple values for the same 
     * name
     * @deprecated Pass a NamedValueSingleton that implements 
     * com.demo.Singleton for unique values. See 
     * {@link #setProperty(NamedValue)}
     */
    public void setProperty(NamedValue property, boolean unique) {
        Element element = null;

        if (unique) {
            element = root.getChild(property.getName());

            if (null == element) {
                element = new Element(property.getName()).setText(
                        property.getValue().toString());
                properties.add(element);
            }
            else {
                element.setText(property.getValue().toString());
            }
        }
        else {
            properties.add(
                new Element(property.getName()).setText(
                    property.getValue().toString()));
        }
    }

Most IDEs, like Eclipse or WDSc, will not display a warning in any class that refers to the setProperty(NamedValue, boolean) method. Also, the generated JavaDoc for this method will identify it as deprecated, with a link to the replacement method. Using this technique gives developers fair warning and helps to track down classes that refer to obsolete classes and methods.

--David


Sponsored By
ADVANCED SYSTEMS CONCEPTS

TOP TEN REASONS

Why You Should
Retire Query/400

Yes, there is a much better way!

Learn how you can access and present
AS/400 and iSeries
database information
without jumping through hoops.

Read More


THIS ISSUE
SPONSORED BY:

Advanced Systems Concepts
Infinium UserNet


BACK ISSUES

TABLE OF
CONTENTS

Deprecating and Renaming Java Classes

Joining on Incompatible Fields in Query/400

Reader Feedback and Insights: The Top 10 Reasons to Learn Java


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris
Shannon O'Donnell

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.