KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > api > JavaModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javacore.api;
21
22 import java.util.List JavaDoc;
23 import org.netbeans.api.java.classpath.ClassPath;
24 import org.netbeans.api.mdr.MDRManager;
25 import org.netbeans.jmi.javamodel.Resource;
26 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
27 import org.openide.filesystems.FileObject;
28 import org.netbeans.jmi.javamodel.JavaModelPackage;
29 import org.netbeans.api.mdr.MDRepository;
30
31 /** Provides an entry point to the Java metadata.
32  */

33 public final class JavaModel {
34     private JavaModel() {
35     }
36
37     /** Sets active classpath for the current transaction. The real active classpath
38      * is constructed from the passed classpath as follows:
39      * <ul>
40      * <li>For every binary root its source roots are retrieved.</li>
41      * <li>If a given source root is immutable (a zip file) or does not exist,
42      * the binary root is added to the active classpath, otherwise the source root is added.</li>
43      * <li>Some binary roots may point to the same source root (JDK jars -&gt; src.zip), which
44      * could result in duplicate entries on the active classpath - these are filtered out.</li>
45      * </ul>
46      *
47      * @param cp Classpath to be set as active.
48      */

49     public static void setClassPath(ClassPath cp) {
50         JavaMetamodel.getManager().setClassPath(cp);
51     }
52     
53     /** Sets a list of classpaths as being an active classpath. The real active classpath
54      * is constructed by merging all classpath roots of the classpath in the passed list
55      * and applying the same rules as in {@link #setClassPath(ClassPath)}.
56      *
57      * @param cps List of classpaths.
58      */

59     public static void setClassPath(List JavaDoc/*<ClassPath>*/ cps) {
60         JavaMetamodel.getManager().setClassPath(cps);
61     }
62     
63     /** Sets the default classpath for a FileObject corresponding to the given
64      * Resource as the active classpath. The real classpath is obtained from the default
65      * classpath according to the same rules as described in {@link #setClassPath(ClassPath)}.
66      *
67      * @param rsc Resource from which to obtain the default classpath.
68      */

69     public static void setClassPath(Resource rsc) {
70         JavaMetamodel.getManager().setClassPath(rsc);
71     }
72     
73     /** Sets the default classpath for the passed FileObject as the active classpath.
74      * The real classpath is obtained from the default classpath according to the same rules
75      * as described in {@link #setClassPath(ClassPath)}.
76      *
77      * @param fo FileObject from which to obtain the default classpath.
78      */

79     public static void setClassPath(FileObject fo) {
80         JavaMetamodel.getManager().setClassPath(fo);
81     }
82     
83     /** Method that navigates from a FileObject to the corresponding Resource.
84      *
85      * @param fo FileObject for which the Resource should be found.
86      * @return Resource corresponding to the passed FileObject.
87      */

88     public static Resource getResource(FileObject fo) {
89         return JavaMetamodel.getManager().getResource(fo);
90     }
91
92     /** Method that finds a Resource corresponding to a file identified by
93      * a classpath root and the file's relative name in this root.
94      *
95      * @param cpRoot FileObject representing a classpath root the desired file belongs to.
96      * @param name Relative name of the file in the classpath root.
97      * @return Resource corresponding to the file identified by the method parameters.
98      */

99     public static Resource getResource(FileObject cpRoot, String JavaDoc name) {
100         return JavaMetamodel.getManager().getResource(cpRoot, name);
101     }
102     
103     /** Navigates from a Resource to the corresponding FileObject.
104      *
105      * @param r Resource for which the FileObject should be found.
106      * @return FileObject corresponding to the specified resource.
107      */

108     public static FileObject getFileObject(Resource r) {
109         return JavaMetamodel.getManager().getFileObject(r);
110     }
111     
112     /** Tries to find an extent for a given classpath root. If the extent does not exist, returns null.
113      *
114      * @param cpRoot Classpath root.
115      * @return Extent containing metadata for a given classpath root.
116      */

117     public static JavaModelPackage getJavaExtent(FileObject cpRoot) {
118         return JavaMetamodel.getManager().getJavaExtent(cpRoot);
119     }
120
121     /** Returns the default extent. It can be used e.g. for accessing global operations
122      * available from factory objects - e.g. resolving types by their names:<p/>
123      * <pre> JavaModel.getDefaultExtent().getType().resolve("java.lang.String");</pre>
124      *
125      * @return Default extent.
126      */

127     public static JavaModelPackage getDefaultExtent() {
128         return JavaMetamodel.getManager().getDefaultExtent();
129     }
130     
131     /** Returns metadata repository containing all Java metadata managed by JavaCore module.
132      * It can be used for invoking repository-wide operations such as starting/ending transactions:<p/>
133      * <pre> JavaModel.getJavaRepository().beginTrans(false);
134      * try {
135      * // do something
136      * } finally {
137      * JavaModel.getJavaRepository().endTrans();
138      * }
139      * </pre>
140      *
141      * @return Metadata repository.
142      */

143     public static MDRepository getJavaRepository() {
144         return JavaMetamodel.getDefaultRepository();
145     }
146 }
147
Popular Tags