comparison graal/com.oracle.max.base/src/com/sun/max/ide/JavaProject.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.sun.max.ide;
24
25 import java.io.*;
26 import java.util.*;
27
28 import com.sun.max.program.*;
29 import com.sun.max.program.Classpath.Entry;
30
31 /**
32 * Software project-dependent configuration derived from the
33 * {@linkplain Classpath#fromSystem() system class path}.
34 */
35 public final class JavaProject {
36
37 /**
38 * System property name specifying the Maxine workspace directory.
39 */
40 public static final String MAX_WORKSPACE_PROPERTY = "max.workspace";
41
42 /**
43 * Determines if a given directory is Maxine workspace directory.
44 *
45 * @param dir a directory to test
46 * @return {@code true} if {@code dir} is a directory containing a file named "projects.properties"
47 */
48 public static boolean isWorkspace(File dir) {
49 return new File(dir, "projects.properties").exists();
50 }
51
52 private JavaProject() {
53 }
54
55 public static final String SOURCE_DIRECTORY_NAME = "src";
56
57 public static final String TEST_SOURCE_DIRECTORY_NAME = "test";
58
59 /**
60 * Gets the paths on which all the class files referenced by a Java project can be found.
61 *
62 * @param projClass a class denoting a project (i.e. any class in the project)
63 * @param includeDependencies if true, the returned path includes the location of the
64 * class files produced by each of the projects that the current
65 * project depends upon
66 */
67 public static Classpath getClassPath(Class projClass, boolean includeDependencies) {
68 String classfile = projClass.getName().replace('.', '/') + ".class";
69 ArrayList<Entry> classPathEntries = new ArrayList<Entry>();
70 Entry projEntry = null;
71 for (Entry entry : Classpath.fromSystem().entries()) {
72 if (entry.contains(classfile)) {
73 projEntry = entry;
74 classPathEntries.add(entry);
75 break;
76 }
77 }
78 if (classPathEntries.isEmpty()) {
79 throw new JavaProjectNotFoundException("Could not find path to Java project classes");
80 }
81 if (includeDependencies) {
82 for (Entry entry : Classpath.fromSystem().entries()) {
83 if (entry != projEntry) {
84 classPathEntries.add(entry);
85 }
86 }
87 }
88 return new Classpath(classPathEntries);
89 }
90
91 static class WorkspaceFinder extends ClasspathTraversal {
92
93 File workspace;
94 File project;
95
96
97 boolean deriveWorkspace(File start) {
98 File dir = start;
99 File child = null;
100 while (dir != null) {
101 if (isWorkspace(dir)) {
102 workspace = dir;
103 project = child;
104 return true;
105 }
106 child = dir;
107 dir = dir.getParentFile();
108 }
109 return false;
110 }
111
112 @Override
113 protected boolean visitFile(File parent, String resource) {
114 String classFile = JavaProject.class.getName().replace('.', File.separatorChar) + ".class";
115 if (resource.equals(classFile)) {
116 if (deriveWorkspace(parent)) {
117 return false;
118 }
119 }
120 return true;
121 }
122 @Override
123 protected boolean visitArchiveEntry(java.util.zip.ZipFile archive, java.util.zip.ZipEntry resource) {
124 String classFile = JavaProject.class.getName().replace('.', File.separatorChar) + ".class";
125 if (resource.equals(classFile)) {
126 File archiveFile = new File(archive.getName());
127 if (deriveWorkspace(archiveFile)) {
128 return false;
129 }
130 }
131 return true;
132 }
133 }
134
135 /**
136 * Gets Maxine workspace directory (i.e. the parent of all the {@linkplain #WORKSPACE_PROJECTS representative project directories}).
137 * This can be specified explicitly with the {@value JavaProject#MAX_WORKSPACE_PROPERTY}
138 * or is derived from the {@linkplain Classpath#fromSystem() system class path}.
139 *
140 * @return the Maxine workspace directory
141 */
142 public static File findWorkspaceDirectory() {
143 final String prop = System.getProperty(JavaProject.MAX_WORKSPACE_PROPERTY);
144 if (prop != null) {
145 File dir = new File(prop);
146 ProgramError.check(isWorkspace(dir), prop + " is not a Maxine workspace directory");
147 return dir;
148 }
149 WorkspaceFinder finder = new WorkspaceFinder();
150 finder.run(Classpath.fromSystem());
151 ProgramError.check(finder.workspace != null, "failed to find the Maxine workspace directory");
152 return finder.workspace;
153 }
154
155 /**
156 * Gets the paths on which all the Java source files for a Java project can be found.
157 *
158 * @param projClass a class denoting a project (i.e. any class in the project)
159 * @param includeDependencies if true, the returned path includes the location of the
160 * Java source files for each of the projects that the current
161 * project depends upon
162 */
163 public static Classpath getSourcePath(Class projClass, boolean includeDependencies) {
164 final Classpath classPath = getClassPath(projClass, includeDependencies);
165 final List<String> sourcePath = new LinkedList<String>();
166 for (Entry entry : classPath.entries()) {
167 WorkspaceFinder finder = new WorkspaceFinder();
168 finder.deriveWorkspace(entry.file());
169 final File projectDirectory = finder.project;
170 if (projectDirectory != null) {
171 final File srcDirectory = new File(projectDirectory, SOURCE_DIRECTORY_NAME);
172 if (srcDirectory.exists() && srcDirectory.isDirectory()) {
173 sourcePath.add(srcDirectory.getPath());
174 }
175
176 final File testDirectory = new File(projectDirectory, TEST_SOURCE_DIRECTORY_NAME);
177 if (testDirectory.exists() && testDirectory.isDirectory()) {
178 sourcePath.add(testDirectory.getPath());
179 }
180 if (!includeDependencies) {
181 break;
182 }
183 }
184 }
185 if (sourcePath.isEmpty()) {
186 throw new JavaProjectNotFoundException("Could not find path to Java project sources");
187 }
188 return new Classpath(sourcePath.toArray(new String[sourcePath.size()]));
189 }
190
191 /**
192 * Find the primary source directory for a project.
193 *
194 * @param projClass a class denoting a project (i.e. any class in the project)
195 */
196 public static File findSourceDirectory(Class projClass) {
197 return getSourcePath(projClass, false).entries().get(0).file();
198 }
199 }