Bug 21428 - DOM/AST: AST class unnecessarily plug-in dependent?
Summary: DOM/AST: AST class unnecessarily plug-in dependent?
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P3 normal with 1 vote (vote)
Target Milestone: 2.1 M1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-07-10 02:34 EDT by Yann-Gaël Guéhéneuc CLA
Modified: 2002-09-10 08:51 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yann-Gaël Guéhéneuc CLA 2002-07-10 02:34:38 EDT
Hello,

    I wrote the following code, which is part of a Java project, *not* a
plug-in. It compiles fine with the appropriate class-path:

public class ASTTest {
    public static void main(final String[] args) {
        CompilationUnit compilationUnit =
            AST.parseCompilationUnit(
                "class A { private int i; }".toCharArray());
        compilationUnit.accept(new ASTVisitor() {
            public boolean visit(final TypeDeclaration node) {
                System.out.println(node.getName());
                return true;
            }
        });
    }
}

    When I run it, I get an exception:

java.lang.NullPointerException
    at org.eclipse.jdt.core.JavaCore.getOptions(JavaCore.java:1224)
    at
org.eclipse.jdt.core.dom.CompilationUnitResolver.parse(CompilationUnitResolv
er.java:180)
    at org.eclipse.jdt.core.dom.AST.parseCompilationUnit(AST.java:359)
    at yann.test.ast.ASTTest.main(ASTTest.java:39)
Exception in thread "main"

    It seems to me that the AST class is tightly coupled with the plug-in
management mechanism. Indeed, the exception arises in class JavaCore at the
following line:

    public static Hashtable getOptions() {

        Hashtable options = new Hashtable(10);

        // see #initializeDefaultPluginPreferences() for changing default
settings
        Preferences preferences = getPlugin().getPluginPreferences();
        ///////////////////////////////////////////////////////////// Here
        HashSet optionNames =
JavaModelManager.getJavaModelManager().OptionNames;

        // get preferences set to their default
        String[] defaultPropertyNames = preferences.defaultPropertyNames();
        for (int i = 0; i < defaultPropertyNames.length; i++){
            String propertyName = defaultPropertyNames[i];
            if (optionNames.contains(propertyName)){
                options.put(propertyName,
preferences.getString(propertyName));
            }
        }
        // get preferences not set to their default
        String[] propertyNames = preferences.propertyNames();
        for (int i = 0; i < propertyNames.length; i++){
            String propertyName = propertyNames[i];
            if (optionNames.contains(propertyName)){
                options.put(propertyName,
preferences.getString(propertyName));
            }
        }
        // get encoding through resource plugin
        options.put(CORE_ENCODING, ResourcesPlugin.getEncoding());

        return options;
    }

    Because the JavaCode class is never initialized, the private static
field (!) JAVA_CORE_PLUGIN is set to null and thus the getPlugin() method
returns null.

    Yann-Gaël Guéhéneuc
Comment 1 Philipe Mulet CLA 2002-07-10 12:07:06 EDT
If JavaCore plug-in is never started properly, various things won't work, like 
the background indexing which won't get started.

No action planned, the plug-in is to be started.
Comment 2 Philipe Mulet CLA 2002-07-10 12:07:36 EDT
No action planned.
Comment 3 Yann-Gaël Guéhéneuc CLA 2002-07-11 02:52:07 EDT
Sure.
However, I do not understand why parsing through the AST class should be plug-
in dependent. The parser (I believe) does not (should not) care whether 
indexing and other goodies are enabled... I think it must be left to the 
developper to start or not the JavaCore plug-in as long as she knows what she 
will get. (Also, the JAVA_CORE_PLUGIN private static variable seemed to me as a 
quick fix?)