001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.source.tasklist;
043:
044: import java.io.File;
045: import java.net.URL;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.EnumSet;
050: import java.util.HashMap;
051: import java.util.HashSet;
052: import java.util.Iterator;
053: import java.util.LinkedList;
054: import java.util.List;
055: import java.util.Map;
056: import java.util.Queue;
057: import java.util.Set;
058: import java.util.WeakHashMap;
059: import java.util.logging.Level;
060: import java.util.logging.Logger;
061: import java.util.regex.Pattern;
062: import javax.lang.model.element.Element;
063: import javax.lang.model.element.ElementKind;
064: import javax.lang.model.element.Modifier;
065: import javax.lang.model.element.TypeElement;
066: import javax.lang.model.util.Elements;
067: import org.netbeans.api.java.source.ClassIndex;
068: import org.netbeans.api.java.source.ClasspathInfo;
069: import org.netbeans.api.java.source.ElementHandle;
070: import org.netbeans.api.java.source.JavaSource;
071: import org.netbeans.modules.java.source.ElementHandleAccessor;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074:
075: /**
076: *
077: * @author Jan Lahoda
078: */
079: public class RebuildOraculum {
080:
081: private static final String DEPRECATED = "DEPRECATED"; //NOI18N
082:
083: private static Map<URL, Map<ElementHandle, Collection<String>>> url2Members = new HashMap<URL, Map<ElementHandle, Collection<String>>>();
084:
085: static synchronized void putMembers(URL file,
086: Map<ElementHandle, Collection<String>> members) {
087: url2Members.clear();
088: url2Members.put(file, members);
089: }
090:
091: private static synchronized Map<ElementHandle, Collection<String>> getMembers(
092: URL file) {
093: Map<ElementHandle, Collection<String>> result = url2Members
094: .get(file);
095:
096: return result != null ? result
097: : new HashMap<ElementHandle, Collection<String>>();
098: }
099:
100: private static RebuildOraculum INSTANCE = new RebuildOraculum();
101:
102: public static RebuildOraculum get() {
103: return INSTANCE;
104: }
105:
106: private RebuildOraculum() {
107: }
108:
109: private static String convertToSourceName(String binaryName) {
110: binaryName = binaryName.replace('.', '/'); //NOI18N
111: int index = binaryName.lastIndexOf('/'); //NOI18N
112: if (index < 0) {
113: index = 0;
114: }
115: index = binaryName.indexOf(index, '$'); //NOI18N
116: if (index > 0) {
117: binaryName = binaryName.substring(0, index);
118: }
119: return binaryName + ".java"; //NOI18N
120: }
121:
122: private static final Pattern ANONYMOUS = Pattern
123: .compile("\\$[0-9]"); //NOI18N
124:
125: public List<File> findFilesToRebuild(File root, URL file,
126: ClasspathInfo cpInfo,
127: Map<ElementHandle, Collection<String>> currentMembers) {
128: long startTime = System.currentTimeMillis();
129: long endTime = -1;
130:
131: try {
132: Logger.getLogger(RebuildOraculum.class.getName()).log(
133: Level.FINE, "members={0}", getMembers(file));
134: Logger.getLogger(RebuildOraculum.class.getName()).log(
135: Level.FINE, "currentMembers={0}", currentMembers);
136:
137: Map<ElementHandle, Collection<String>> added = new HashMap<ElementHandle, Collection<String>>(
138: currentMembers);
139:
140: for (ElementHandle h : getMembers(file).keySet()) {
141: added.remove(h);
142: }
143:
144: Map<ElementHandle, Collection<String>> removed = new HashMap<ElementHandle, Collection<String>>(
145: getMembers(file));
146:
147: for (ElementHandle h : currentMembers.keySet()) {
148: removed.remove(h);
149: }
150:
151: Map<ElementHandle, Collection<String>> changedElements = new HashMap<ElementHandle, Collection<String>>(
152: getMembers(file));
153:
154: for (Iterator<ElementHandle> it = changedElements.keySet()
155: .iterator(); it.hasNext();) {
156: ElementHandle h = it.next();
157:
158: Collection<String> original = changedElements.get(h);
159: Collection<String> current = currentMembers.get(h);
160:
161: if (original == null || current == null
162: || original.equals(current)) {
163: it.remove();
164: }
165: }
166:
167: synchronized (RebuildOraculum.class) {
168: if (url2Members.containsKey(file)) {
169: putMembers(file, currentMembers);
170: }
171: }
172:
173: Collection<ElementHandle<TypeElement>> classes = new ArrayList<ElementHandle<TypeElement>>();
174:
175: //a really simple heuristics:
176: if (!added.isEmpty() || !removed.isEmpty()
177: || !changedElements.isEmpty()) {
178: for (ElementHandle h : currentMembers.keySet()) {
179: if (h.getKind().isClass()
180: || h.getKind().isInterface()) {
181: classes.add(h);
182: }
183: }
184: }
185:
186: if (classes.isEmpty()) {
187: return Collections.<File> emptyList();
188: }
189:
190: endTime = System.currentTimeMillis();
191:
192: ClassIndex ci = cpInfo.getClassIndex();
193:
194: return findAllDependent(root, file, ci, classes);
195: } finally {
196: if (endTime == (-1)) {
197: endTime = System.currentTimeMillis();
198: }
199:
200: if (file != null) {
201: Logger
202: .getLogger("TIMER")
203: .log(
204: Level.FINE,
205: "RebuildOraculum: findFilesToRebuild total",
206: new Object[] {
207: file,
208: System.currentTimeMillis()
209: - startTime });
210: Logger.getLogger("TIMER").log(Level.FINE,
211: "RebuildOraculum: quick heuristics",
212: new Object[] { file, endTime - startTime });
213: }
214: }
215: }
216:
217: public static List<File> findAllDependent(File root, URL file,
218: ClassIndex ci,
219: Collection<ElementHandle<TypeElement>> classes) {
220: //performance: filter out anonymous innerclasses:
221: for (Iterator<ElementHandle<TypeElement>> i = classes
222: .iterator(); i.hasNext();) {
223: if (ANONYMOUS.matcher(i.next().getBinaryName()).find()) {
224: i.remove();
225: }
226: }
227:
228: Set<ElementHandle<TypeElement>> toParse = new HashSet<ElementHandle<TypeElement>>(
229: classes);
230:
231: long start = System.currentTimeMillis();
232:
233: boolean changed = true;
234:
235: while (changed) {
236: Set<ElementHandle<TypeElement>> orig = new HashSet<ElementHandle<TypeElement>>(
237: toParse);
238:
239: for (ElementHandle<TypeElement> e : orig) {
240: toParse.addAll(ci.getElements(e, EnumSet
241: .of(ClassIndex.SearchKind.IMPLEMENTORS),
242: EnumSet.of(ClassIndex.SearchScope.SOURCE)));
243: }
244:
245: changed = !orig.equals(toParse);
246: }
247:
248: Set<ElementHandle<TypeElement>> orig = new HashSet<ElementHandle<TypeElement>>(
249: toParse);
250:
251: for (ElementHandle<TypeElement> e : orig) {
252: toParse.addAll(ci.getElements(e, EnumSet
253: .complementOf(EnumSet
254: .of(ClassIndex.SearchKind.IMPLEMENTORS)),
255: EnumSet.of(ClassIndex.SearchScope.SOURCE)));
256: }
257:
258: toParse.removeAll(classes);
259:
260: if (file != null) {
261: Logger.getLogger("TIMER").log(
262: Level.FINE,
263: "Deps - Handles",
264: new Object[] { file,
265: System.currentTimeMillis() - start });
266: Logger.getLogger("TIMER").log(Level.FINE,
267: "Deps - Handles #",
268: new Object[] { file, toParse.size() });
269: }
270:
271: long cur = System.currentTimeMillis();
272:
273: Set<File> files = new HashSet<File>();
274:
275: for (ElementHandle<TypeElement> e : toParse) {
276: String sourceName = convertToSourceName(e.getBinaryName());
277: File source = new File(root, sourceName);
278:
279: if (source.canRead())
280: files.add(FileUtil.normalizeFile(source));
281:
282: // FileObject f = SourceUtils.getFile(e, cpInfo);
283: //
284: // if (f == null/* || !FileUtil.isParentOf(rootFO, f)*/)
285: // continue;
286: //
287: // files.add(FileUtil.toFile(f));
288: }
289:
290: files.remove(null);
291:
292: if (file != null) {
293: Logger.getLogger("TIMER").log(
294: Level.FINE,
295: "Deps - Files",
296: new Object[] { file,
297: System.currentTimeMillis() - cur });
298: Logger.getLogger("TIMER").log(Level.FINE, "Deps - Files #",
299: new Object[] { file, files.size() });
300: }
301:
302: return new ArrayList<File>(files);
303: }
304:
305: static synchronized boolean isInitialized(URL file) {
306: return url2Members.containsKey(file);
307: }
308:
309: private static Collection<String> getExtendedModifiers(
310: Elements elements, Element el) {
311: Set<String> result = new HashSet<String>();
312:
313: for (Modifier m : el.getModifiers()) {
314: result.add(m.name());
315: }
316:
317: if (elements.isDeprecated(el)) {
318: result.add(DEPRECATED);
319: }
320:
321: return result;
322: }
323:
324: public static Map<ElementHandle, Collection<String>> sortOut(
325: Elements elements,
326: Iterable<? extends TypeElement> topLevelElements) {
327: Map<ElementHandle, Collection<String>> types = new HashMap<ElementHandle, Collection<String>>();
328: Queue<TypeElement> toHandle = new LinkedList<TypeElement>();
329: for (TypeElement te : topLevelElements) {
330: toHandle.offer(te);
331: }
332:
333: while (!toHandle.isEmpty()) {
334: TypeElement te = toHandle.poll();
335:
336: types.put(ElementHandle.create(te), getExtendedModifiers(
337: elements, te));
338:
339: for (Element e : te.getEnclosedElements()) {
340: switch (e.getKind()) {
341: case CLASS:
342: case INTERFACE:
343: case ENUM:
344: case ANNOTATION_TYPE:
345: toHandle.offer((TypeElement) e);
346: break;
347: case METHOD:
348: case FIELD:
349: case ENUM_CONSTANT:
350: types.put(ElementHandle.create(e),
351: getExtendedModifiers(elements, e));
352: }
353: }
354: }
355:
356: return types;
357: }
358:
359: }
|