| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2010, 2017 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | * Stephan Herrmann - Contribution for |
| 14 | * Bug 440687 - [compiler][batch][null] improve command line option for external annotations |
| 15 | *******************************************************************************/ |
| 16 | package org.eclipse.jdt.core.dom; |
| 17 | |
| 18 | import org.eclipse.core.runtime.IProgressMonitor; |
| 19 | import org.eclipse.core.runtime.OperationCanceledException; |
| 20 | import org.eclipse.jdt.core.compiler.CharOperation; |
| 21 | import org.eclipse.jdt.internal.compiler.batch.ClasspathDirectory; |
| 22 | import org.eclipse.jdt.internal.compiler.batch.FileSystem; |
| 23 | import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; |
| 24 | import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; |
| 25 | import org.eclipse.jdt.internal.core.INameEnvironmentWithProgress; |
| 26 | import org.eclipse.jdt.internal.core.NameLookup; |
| 27 | |
| 28 | /** |
| 29 | * Batch name environment that can be canceled using a monitor. |
| 30 | * @since 3.6 |
| 31 | */ |
| 32 | class NameEnvironmentWithProgress extends FileSystem implements INameEnvironmentWithProgress { |
| 33 | IProgressMonitor monitor; |
| 34 | |
| 35 | public NameEnvironmentWithProgress(Classpath[] paths, String[] initialFileNames, IProgressMonitor monitor) { |
| 36 | super(paths, initialFileNames, false); |
| 37 | setMonitor(monitor); |
| 38 | } |
| 39 | private void checkCanceled() { |
| 40 | if (this.monitor != null && this.monitor.isCanceled()) { |
| 41 | if (NameLookup.VERBOSE) { |
| 42 | System.out.println(Thread.currentThread() + " CANCELLING LOOKUP "); //$NON-NLS-1$ |
| 43 | } |
| 44 | throw new AbortCompilation(true/*silent*/, new OperationCanceledException()); |
| 45 | } |
| 46 | } |
| 47 | @Override |
| 48 | public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName, char[] moduleName) { |
| 49 | return findType(typeName, packageName, true, moduleName); |
| 50 | } |
| 51 | @Override |
| 52 | public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName, boolean searchWithSecondaryTypes, char[] moduleName) { |
| 53 | checkCanceled(); |
| 54 | NameEnvironmentAnswer answer = super.findType(typeName, packageName, moduleName); |
| 55 | if (answer == null && searchWithSecondaryTypes) { |
| 56 | NameEnvironmentAnswer suggestedAnswer = null; |
| 57 | String qualifiedPackageName = new String(CharOperation.concatWith(packageName, '/')); |
| 58 | String qualifiedTypeName = new String(CharOperation.concatWith(packageName, typeName, '/')); |
| 59 | String qualifiedBinaryFileName = qualifiedTypeName + SUFFIX_STRING_class; |
| 60 | for (int i = 0, length = this.classpaths.length; i < length; i++) { |
| 61 | if (!(this.classpaths[i] instanceof ClasspathDirectory)) continue; |
| 62 | ClasspathDirectory classpathDirectory = (ClasspathDirectory) this.classpaths[i]; |
| 63 | LookupStrategy strategy = LookupStrategy.get(moduleName); |
| 64 | if (!strategy.matchesWithName(classpathDirectory, |
| 65 | loc -> loc.getModule() != null, |
| 66 | loc -> loc.servesModule(moduleName))) { |
| 67 | continue; |
| 68 | } |
| 69 | answer = classpathDirectory.findSecondaryInClass(typeName, qualifiedPackageName, qualifiedBinaryFileName); |
| 70 | if (answer != null) { |
| 71 | if (!answer.ignoreIfBetter()) { |
| 72 | if (answer.isBetter(suggestedAnswer)) |
| 73 | return answer; |
| 74 | } else if (answer.isBetter(suggestedAnswer)) |
| 75 | // remember suggestion and keep looking |
| 76 | suggestedAnswer = answer; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return answer; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public NameEnvironmentAnswer findType(char[][] compoundName) { |
| 85 | checkCanceled(); |
| 86 | return super.findType(compoundName); |
| 87 | } |
| 88 | @Override |
| 89 | public boolean isPackage(char[][] compoundName, char[] packageName) { |
| 90 | checkCanceled(); |
| 91 | return super.isPackage(compoundName, packageName); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void setMonitor(IProgressMonitor monitor) { |
| 96 | this.monitor = monitor; |
| 97 | } |
| 98 | } |
| 99 |
Members