Java源码示例:org.eclipse.jdt.internal.core.NameLookup
示例1
private static int convertSearchFilterToModelFilter(int searchFilter) {
switch (searchFilter) {
case IJavaSearchConstants.CLASS:
return NameLookup.ACCEPT_CLASSES;
case IJavaSearchConstants.INTERFACE:
return NameLookup.ACCEPT_INTERFACES;
case IJavaSearchConstants.ENUM:
return NameLookup.ACCEPT_ENUMS;
case IJavaSearchConstants.ANNOTATION_TYPE:
return NameLookup.ACCEPT_ANNOTATIONS;
case IJavaSearchConstants.CLASS_AND_ENUM:
return NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_ENUMS;
case IJavaSearchConstants.CLASS_AND_INTERFACE:
return NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES;
default:
return NameLookup.ACCEPT_ALL;
}
}
示例2
public IJavaElement getJavaElement() {
INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment; // a package binding always has a LooupEnvironment set
if (!(nameEnvironment instanceof SearchableEnvironment)) return null;
// this is not true in standalone DOM/AST
NameLookup nameLookup = ((SearchableEnvironment) nameEnvironment).nameLookup;
if (nameLookup == null) return null;
IJavaElement[] pkgs = nameLookup.findPackageFragments(getName(), false/*exact match*/);
if (pkgs == null) return null;
if (pkgs.length == 0) {
// add additional tracing as this should not happen
org.eclipse.jdt.internal.core.util.Util.log(
new Status(
IStatus.WARNING,
JavaCore.PLUGIN_ID,
"Searching for package " + getName() + " returns an empty array")); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
return pkgs[0];
}
示例3
/**
* @since 2.9
*/
protected IType findPrimaryType(String packageName, String typeName) throws JavaModelException {
JavaProject casted = (JavaProject) javaProject;
NameLookup nameLookup = getNameLookup(casted);
NameLookup.Answer answer = nameLookup.findType(
typeName,
packageName,
false,
NameLookup.ACCEPT_ALL,
false, // do not consider secondary types
true, // wait for indexes (in case we need to consider secondary types)
false/*don't check restrictions*/,
null);
return answer == null ? null : answer.type;
}
示例4
public CompletionResultRequestor(ISearchRequestor requestor, ICompilationUnit unitToSkip, IJavaProject project, NameLookup nameLookup) {
this.requestor = requestor;
this.unitToSkip= unitToSkip;
this.project= project;
this.nameLookup = nameLookup;
this.checkAccessRestrictions =
!JavaCore.IGNORE.equals(project.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
|| !JavaCore.IGNORE.equals(project.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true));
}
示例5
private void checkCanceled() {
if (this.monitor != null && this.monitor.isCanceled()) {
if (NameLookup.VERBOSE) {
System.out.println(Thread.currentThread() + " CANCELLING LOOKUP "); //$NON-NLS-1$
}
throw new AbortCompilation(true/*silent*/, new OperationCanceledException());
}
}
示例6
private NameLookup getNameLookup(JavaProject casted) throws JavaModelException {
return casted.newNameLookup(getWorkingCopies());
}
示例7
public static void resolve(
ICompilationUnit[] compilationUnits,
String[] bindingKeys,
ASTRequestor requestor,
int apiLevel,
Map options,
IJavaProject javaProject,
WorkingCopyOwner owner,
int flags,
IProgressMonitor monitor) {
CancelableNameEnvironment environment = null;
CancelableProblemFactory problemFactory = null;
try {
if (monitor != null) {
int amountOfWork = (compilationUnits.length + bindingKeys.length) * 2; // 1 for beginToCompile, 1 for resolve
monitor.beginTask("", amountOfWork); //$NON-NLS-1$
}
environment = new CancelableNameEnvironment(((JavaProject) javaProject), owner, monitor);
problemFactory = new CancelableProblemFactory(monitor);
CompilerOptions compilerOptions = getCompilerOptions(options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
compilerOptions.ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
CompilationUnitResolver resolver =
new CompilationUnitResolver(
environment,
getHandlingPolicy(),
compilerOptions,
getRequestor(),
problemFactory,
monitor,
javaProject != null);
resolver.resolve(compilationUnits, bindingKeys, requestor, apiLevel, options, owner, flags);
if (NameLookup.VERBOSE) {
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}
} catch (JavaModelException e) {
// project doesn't exist -> simple parse without resolving
parse(compilationUnits, requestor, apiLevel, options, flags, monitor);
} finally {
if (monitor != null) monitor.done();
if (environment != null) {
environment.setMonitor(null); // don't hold a reference to this external object
}
if (problemFactory != null) {
problemFactory.monitor = null; // don't hold a reference to this external object
}
}
}
示例8
public static void resolve(
String[] sourceUnits,
String[] encodings,
String[] bindingKeys,
FileASTRequestor requestor,
int apiLevel,
Map options,
List classpaths,
int flags,
IProgressMonitor monitor) {
INameEnvironmentWithProgress environment = null;
CancelableProblemFactory problemFactory = null;
try {
if (monitor != null) {
int amountOfWork = (sourceUnits.length + bindingKeys.length) * 2; // 1 for beginToCompile, 1 for resolve
monitor.beginTask("", amountOfWork); //$NON-NLS-1$
}
Classpath[] allEntries = new Classpath[classpaths.size()];
classpaths.toArray(allEntries);
environment = new NameEnvironmentWithProgress(allEntries, null, monitor);
problemFactory = new CancelableProblemFactory(monitor);
CompilerOptions compilerOptions = getCompilerOptions(options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
compilerOptions.ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
CompilationUnitResolver resolver =
new CompilationUnitResolver(
environment,
getHandlingPolicy(),
compilerOptions,
getRequestor(),
problemFactory,
monitor,
false);
resolver.resolve(sourceUnits, encodings, bindingKeys, requestor, apiLevel, options, flags);
if (NameLookup.VERBOSE && (environment instanceof CancelableNameEnvironment)) {
CancelableNameEnvironment cancelableNameEnvironment = (CancelableNameEnvironment) environment;
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}
} finally {
if (monitor != null) monitor.done();
if (environment != null) {
environment.setMonitor(null); // don't hold a reference to this external object
}
if (problemFactory != null) {
problemFactory.monitor = null; // don't hold a reference to this external object
}
}
}
示例9
public static CompilationUnitDeclaration resolve(
org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit,
IJavaProject javaProject,
List classpaths,
NodeSearcher nodeSearcher,
Map options,
WorkingCopyOwner owner,
int flags,
IProgressMonitor monitor) throws JavaModelException {
CompilationUnitDeclaration unit = null;
INameEnvironmentWithProgress environment = null;
CancelableProblemFactory problemFactory = null;
CompilationUnitResolver resolver = null;
try {
if (javaProject == null) {
Classpath[] allEntries = new Classpath[classpaths.size()];
classpaths.toArray(allEntries);
environment = new NameEnvironmentWithProgress(allEntries, null, monitor);
} else {
environment = new CancelableNameEnvironment((JavaProject) javaProject, owner, monitor);
}
problemFactory = new CancelableProblemFactory(monitor);
CompilerOptions compilerOptions = getCompilerOptions(options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
boolean ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
resolver =
new CompilationUnitResolver(
environment,
getHandlingPolicy(),
compilerOptions,
getRequestor(),
problemFactory,
monitor,
javaProject != null);
boolean analyzeAndGenerateCode = !ignoreMethodBodies;
unit =
resolver.resolve(
null, // no existing compilation unit declaration
sourceUnit,
nodeSearcher,
true, // method verification
analyzeAndGenerateCode, // analyze code
analyzeAndGenerateCode); // generate code
if (resolver.hasCompilationAborted) {
// the bindings could not be resolved due to missing types in name environment
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=86541
CompilationUnitDeclaration unitDeclaration = parse(sourceUnit, nodeSearcher, options, flags);
if (unit != null) {
final int problemCount = unit.compilationResult.problemCount;
if (problemCount != 0) {
unitDeclaration.compilationResult.problems = new CategorizedProblem[problemCount];
System.arraycopy(unit.compilationResult.problems, 0, unitDeclaration.compilationResult.problems, 0, problemCount);
unitDeclaration.compilationResult.problemCount = problemCount;
}
} else if (resolver.abortProblem != null) {
unitDeclaration.compilationResult.problemCount = 1;
unitDeclaration.compilationResult.problems = new CategorizedProblem[] { resolver.abortProblem };
}
return unitDeclaration;
}
if (NameLookup.VERBOSE && environment instanceof CancelableNameEnvironment) {
CancelableNameEnvironment cancelableNameEnvironment = (CancelableNameEnvironment) environment;
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}
return unit;
} finally {
if (environment != null) {
// don't hold a reference to this external object
environment.setMonitor(null);
}
if (problemFactory != null) {
problemFactory.monitor = null; // don't hold a reference to this external object
}
}
}
示例10
public IAnnotationBinding[] getAnnotations() {
try {
INameEnvironment nameEnvironment = this.binding.environment.nameEnvironment;
if (!(nameEnvironment instanceof SearchableEnvironment))
return AnnotationBinding.NoAnnotations;
NameLookup nameLookup = ((SearchableEnvironment) nameEnvironment).nameLookup;
if (nameLookup == null)
return AnnotationBinding.NoAnnotations;
final String pkgName = getName();
IPackageFragment[] pkgs = nameLookup.findPackageFragments(pkgName, false/*exact match*/);
if (pkgs == null)
return AnnotationBinding.NoAnnotations;
for (int i = 0, len = pkgs.length; i < len; i++) {
int fragType = pkgs[i].getKind();
switch(fragType) {
case IPackageFragmentRoot.K_SOURCE:
String unitName = "package-info.java"; //$NON-NLS-1$
ICompilationUnit unit = pkgs[i].getCompilationUnit(unitName);
if (unit != null && unit.exists()) {
ASTParser p = ASTParser.newParser(AST.JLS3_INTERNAL);
p.setSource(unit);
p.setResolveBindings(true);
p.setUnitName(unitName);
p.setFocalPosition(0);
p.setKind(ASTParser.K_COMPILATION_UNIT);
CompilationUnit domUnit = (CompilationUnit) p.createAST(null);
PackageDeclaration pkgDecl = domUnit.getPackage();
if (pkgDecl != null) {
List annos = pkgDecl.annotations();
if (annos == null || annos.isEmpty())
return AnnotationBinding.NoAnnotations;
IAnnotationBinding[] result = new IAnnotationBinding[annos.size()];
int index=0;
for (Iterator it = annos.iterator(); it.hasNext(); index++) {
result[index] = ((Annotation) it.next()).resolveAnnotationBinding();
// not resolving bindings
if (result[index] == null)
return AnnotationBinding.NoAnnotations;
}
return result;
}
}
break;
case IPackageFragmentRoot.K_BINARY:
NameEnvironmentAnswer answer =
nameEnvironment.findType(TypeConstants.PACKAGE_INFO_NAME, this.binding.compoundName);
if (answer != null && answer.isBinaryType()) {
IBinaryType type = answer.getBinaryType();
char[][][] missingTypeNames = type.getMissingTypeNames();
IBinaryAnnotation[] binaryAnnotations = type.getAnnotations();
org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] binaryInstances =
BinaryTypeBinding.createAnnotations(binaryAnnotations, this.binding.environment, missingTypeNames);
org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] allInstances =
org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding.addStandardAnnotations(binaryInstances, type.getTagBits(), this.binding.environment);
int total = allInstances.length;
IAnnotationBinding[] domInstances = new AnnotationBinding[total];
for (int a = 0; a < total; a++) {
final IAnnotationBinding annotationInstance = this.resolver.getAnnotationInstance(allInstances[a]);
if (annotationInstance == null) {// not resolving binding
return AnnotationBinding.NoAnnotations;
}
domInstances[a] = annotationInstance;
}
return domInstances;
}
}
}
} catch(JavaModelException e) {
return AnnotationBinding.NoAnnotations;
}
return AnnotationBinding.NoAnnotations;
}