Jump to: navigation, search

Difference between revisions of "FAQ What is the Java model?"

 
m
 
Line 1: Line 1:
The Java model is a hierarchical representation of the Java
+
The Java model is a hierarchical representation of the Java projects in your workspace. Figure 20.1 is a sample Spider graph of the Java model, showing the <tt>java.lang.VerifyError</tt> class and its two constructors.
projects in your workspace. Figure 20.1 is a sample Spider graph of the Java model,
+
showing the <tt>java.lang.VerifyError</tt> class and its two constructors.
+
  
 
&nbsp;&nbsp;&nbsp;&nbsp;<img src="../images/javamodel.png">
 
&nbsp;&nbsp;&nbsp;&nbsp;<img src="../images/javamodel.png">
 +
&nbsp;&nbsp;&nbsp;&nbsp;'''Figure 20.1'''&nbsp;&nbsp; Spider diagram of the Java model
  
&nbsp;&nbsp;&nbsp;&nbsp;'''Figure 20.1'''&nbsp;&nbsp;
+
Using a utility method provided by <tt>JavaCore</tt>, a Java model is obtained for the workspace. Using the <tt>IJavaModel</tt>, the Java projects can be inspected. Each project contains a classpath, consisting of multiple package fragment roots, such as source directories in the project or referenced JARs from required plug-ins. Each fragment contains a number of class files. An example of its use is given in the following piece of code:
Spider diagram of the Java model
+
  
 
 
Using a utility method provided by <tt>JavaCore</tt>, a Java model is obtained
 
for the workspace. Using the <tt>IJavaModel</tt>, the Java projects
 
can be inspected. Each project contains a classpath, consisting of
 
multiple package fragment roots, such as source directories in the project
 
or referenced JARs from required plug-ins. Each fragment contains a number
 
of class files. An example of its use is given in the following piece of code:
 
 
<pre>
 
<pre>
 
   IWorkspace workspace = ResourcesPlugin.getWorkspace();
 
   IWorkspace workspace = ResourcesPlugin.getWorkspace();
Line 21: Line 12:
 
   for (int n = 0; n &lt; projects.length; n++) {
 
   for (int n = 0; n &lt; projects.length; n++) {
 
       IJavaProject project = projects[n];
 
       IJavaProject project = projects[n];
       IPackageFragmentRoot[] roots =  
+
       IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
        project.getAllPackageFragmentRoots();
+
 
       int nClasses = 0;
 
       int nClasses = 0;
 
       for (int k = 0; k &lt; roots.length; k++) {
 
       for (int k = 0; k &lt; roots.length; k++) {
Line 29: Line 19:
 
         for (int i = 0; i &lt; elements.length; i++) {
 
         for (int i = 0; i &lt; elements.length; i++) {
 
             IJavaElement element = elements[i];
 
             IJavaElement element = elements[i];
             PackageFragment fragment = (PackageFragment)
+
             PackageFragment fragment = (PackageFragment) element.getAdapter(PackageFragment.class);
              element.getAdapter(PackageFragment.class);
+
 
             if (fragment == null) continue;
 
             if (fragment == null) continue;
 
             IJavaElement fes[] = fragment.getChildren();
 
             IJavaElement fes[] = fragment.getChildren();
Line 40: Line 29:
 
       }
 
       }
 
       String projectName = projects[n].getElementName();
 
       String projectName = projects[n].getElementName();
       System.out.println("Classpath for project "+  
+
       System.out.println("Classpath for project "+ projectName +" contains "+nClasses+" classes.");
      projectName +" contains "+nClasses+" classes.");
+
 
   }
 
   }
 
</pre>
 
</pre>
  
The output of this code for a workspace with a
+
The output of this code for a workspace with a single empty Java project is
single empty Java project is
+
 
<pre>
 
<pre>
 
   Classpath for project P contains 12187 classes.
 
   Classpath for project P contains 12187 classes.
 
</pre>
 
</pre>
In other words, before you start adding your own classes or  
+
In other words, before you start adding your own classes or start referring to any other Eclipse plug-ins, you already have access to more than 12,000 classes in the Java 2, Standard Edition (J2SE) libraries.
start referring to any other Eclipse plug-ins, you already have access
+
to more than 12,000 classes in the Java 2, Standard Edition (J2SE) libraries.
+
  
The Java model is declared in package <tt>org.eclipse.jdt.core</tt>,
+
The Java model is declared in package <tt>org.eclipse.jdt.core</tt>, which consists of 55 types providing access to anything you would ever need to analyze and manipulate your Java programs.
which consists of 55 types providing access to anything you
+
would ever need to analyze and manipulate your Java programs.
+
  
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+
{{Template:FAQ_Tagline}}

Latest revision as of 20:25, 19 June 2006

The Java model is a hierarchical representation of the Java projects in your workspace. Figure 20.1 is a sample Spider graph of the Java model, showing the java.lang.VerifyError class and its two constructors.

    <img src="../images/javamodel.png">     Figure 20.1   Spider diagram of the Java model

Using a utility method provided by JavaCore, a Java model is obtained for the workspace. Using the IJavaModel, the Java projects can be inspected. Each project contains a classpath, consisting of multiple package fragment roots, such as source directories in the project or referenced JARs from required plug-ins. Each fragment contains a number of class files. An example of its use is given in the following piece of code:

   IWorkspace workspace = ResourcesPlugin.getWorkspace();
   IJavaModel javaModel = JavaCore.create(workspace.getRoot());
   IJavaProject projects[] = javaModel.getJavaProjects();
   for (int n = 0; n < projects.length; n++) {
      IJavaProject project = projects[n];
      IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
      int nClasses = 0;
      for (int k = 0; k < roots.length; k++) {
         IPackageFragmentRoot root = roots[k];
         IJavaElement[] elements = root.getChildren();
         for (int i = 0; i < elements.length; i++) {
            IJavaElement element = elements[i];
            PackageFragment fragment = (PackageFragment) element.getAdapter(PackageFragment.class);
            if (fragment == null) continue;
            IJavaElement fes[] = fragment.getChildren();
            for (int j = 0; j < fes.length; j++) {
               String className = fes[j].getElementName();
               nClasses++;
            }
         }
      }
      String projectName = projects[n].getElementName();
      System.out.println("Classpath for project "+ projectName +" contains "+nClasses+" classes.");
   }

The output of this code for a workspace with a single empty Java project is

   Classpath for project P contains 12187 classes.

In other words, before you start adding your own classes or start referring to any other Eclipse plug-ins, you already have access to more than 12,000 classes in the Java 2, Standard Edition (J2SE) libraries.

The Java model is declared in package org.eclipse.jdt.core, which consists of 55 types providing access to anything you would ever need to analyze and manipulate your Java programs.


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.