org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InternalExtendedCompletionContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public InternalExtendedCompletionContext(
		InternalCompletionContext completionContext,
		ITypeRoot typeRoot,
		CompilationUnitDeclaration compilationUnitDeclaration,
		LookupEnvironment lookupEnvironment,
		Scope assistScope,
		ASTNode assistNode,
		ASTNode assistNodeParent,
		WorkingCopyOwner owner,
		CompletionParser parser) {
	this.completionContext = completionContext;
	this.typeRoot = typeRoot;
	this.compilationUnitDeclaration = compilationUnitDeclaration;
	this.lookupEnvironment = lookupEnvironment;
	this.assistScope = assistScope;
	this.assistNode = assistNode;
	this.assistNodeParent = assistNodeParent;
	this.owner = owner;
	this.parser = parser;
}
 
Example #2
Source File: Main.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ReferenceBinding[] processClassNames(LookupEnvironment environment) {
	// check for .class file presence in case of apt processing
	int length = this.classNames.length;
	ReferenceBinding[] referenceBindings = new ReferenceBinding[length];
	for (int i = 0; i < length; i++) {
		String currentName = this.classNames[i];
		char[][] compoundName = null;
		if (currentName.indexOf('.') != -1) {
			// consider names with '.' as fully qualified names
			char[] typeName = currentName.toCharArray();
			compoundName = CharOperation.splitOn('.', typeName);
		} else {
			compoundName = new char[][] { currentName.toCharArray() };
		}
		ReferenceBinding type = environment.getType(compoundName);
		if (type != null && type.isValidBinding()) {
			if (type.isBinaryBinding()) {
				referenceBindings[i] = type;
			}
		} else {
			throw new IllegalArgumentException(
					this.bind("configure.invalidClassName", currentName));//$NON-NLS-1$
		}
	}
	return referenceBindings;
}
 
Example #3
Source File: InternalCompletionContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void setExtendedData(
		ITypeRoot typeRoot,
		CompilationUnitDeclaration compilationUnitDeclaration,
		LookupEnvironment lookupEnvironment,
		Scope scope,
		ASTNode astNode,
		ASTNode astNodeParent,
		WorkingCopyOwner owner,
		CompletionParser parser) {
	this.isExtended = true;
	this.extendedContext =
		new InternalExtendedCompletionContext(
				this,
				typeRoot,
				compilationUnitDeclaration,
				lookupEnvironment,
				scope,
				astNode,
				astNodeParent,
				owner,
				parser);
}
 
Example #4
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.8
 */
protected void setMayTolerateMissingType(ITypeBinding typeBinding) {
	try {
		Field field = typeBinding.getClass().getDeclaredField("binding");
		field.setAccessible(true);
		Object object = field.get(typeBinding);
		if (object instanceof BinaryTypeBinding) {
			BinaryTypeBinding binaryTypeBinding = (BinaryTypeBinding) object;
			Field declaredField = binaryTypeBinding.getClass().getDeclaredField("environment");
			declaredField.setAccessible(true);
			LookupEnvironment env = (LookupEnvironment) declaredField.get(binaryTypeBinding);
			env.mayTolerateMissingType = true;
		}
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
}
 
Example #5
Source File: PackageElementImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public List<? extends Element> getEnclosedElements() {
	PackageBinding binding = (PackageBinding)_binding;
	LookupEnvironment environment = binding.environment;
	char[][][] typeNames = null;
	INameEnvironment nameEnvironment = binding.environment.nameEnvironment;
	if (nameEnvironment instanceof FileSystem) {
		typeNames = ((FileSystem) nameEnvironment).findTypeNames(binding.compoundName);
	}
	HashSet<Element> set = new HashSet<Element>(); 
	if (typeNames != null) {
		for (char[][] typeName : typeNames) {
			ReferenceBinding type = environment.getType(typeName);
			if (type != null && type.isValidBinding()) {
				set.add(_env.getFactory().newElement(type));
			}
		}
	}
	ArrayList<Element> list = new ArrayList<Element>(set.size());
	list.addAll(set);
	return Collections.unmodifiableList(list);
}
 
Example #6
Source File: ElementsImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TypeElement getTypeElement(CharSequence name) {
	LookupEnvironment le = _env.getLookupEnvironment();
	final char[][] compoundName = CharOperation.splitOn('.', name.toString().toCharArray());
	ReferenceBinding binding = le.getType(compoundName);
	// If we didn't find the binding, maybe it's a nested type;
	// try finding the top-level type and then working downwards.
	if (null == binding) {
		ReferenceBinding topLevelBinding = null;
		int topLevelSegments = compoundName.length;
		while (--topLevelSegments > 0) {
			char[][] topLevelName = new char[topLevelSegments][];
			for (int i = 0; i < topLevelSegments; ++i) {
				topLevelName[i] = compoundName[i];
			}
			topLevelBinding = le.getType(topLevelName);
			if (null != topLevelBinding) {
				break;
			}
		}
		if (null == topLevelBinding) {
			return null;
		}
		binding = topLevelBinding;
		for (int i = topLevelSegments; null != binding && i < compoundName.length; ++i) {
			binding = binding.getMemberType(compoundName[i]);
		}
	}
	if (null == binding) {
		return null;
	}
	return new TypeElementImpl(_env, binding, null);
}
 
Example #7
Source File: ElementsImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public PackageElement getPackageElement(CharSequence name) {
	LookupEnvironment le = _env.getLookupEnvironment();
	if (name.length() == 0) {
		return new PackageElementImpl(_env, le.defaultPackage);
	}
	char[] packageName = name.toString().toCharArray();
	PackageBinding packageBinding = le.createPackage(CharOperation.splitOn('.', packageName));
	if (packageBinding == null) {
		return null;
	}
	return new PackageElementImpl(_env, packageBinding);
}
 
Example #8
Source File: LambdaExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void mergeParameterNullAnnotations(BlockScope currentScope) {
	LookupEnvironment env = currentScope.environment();
	TypeBinding[] ourParameters = this.binding.parameters;
	TypeBinding[] descParameters = this.descriptor.parameters;
	int len = Math.min(ourParameters.length, descParameters.length);
	for (int i = 0; i < len; i++) {
		long ourTagBits = ourParameters[i].tagBits & TagBits.AnnotationNullMASK;
		long descTagBits = descParameters[i].tagBits & TagBits.AnnotationNullMASK;
		if (ourTagBits == 0L) {
			if (descTagBits != 0L && !ourParameters[i].isBaseType()) {
				AnnotationBinding [] annotations = descParameters[i].getTypeAnnotations();
				for (int j = 0, length = annotations.length; j < length; j++) {
					AnnotationBinding annotation = annotations[j];
					if (annotation != null) {
						switch (annotation.getAnnotationType().id) {
							case TypeIds.T_ConfiguredAnnotationNullable :
							case TypeIds.T_ConfiguredAnnotationNonNull :
								ourParameters[i] = env.createAnnotatedType(ourParameters[i], new AnnotationBinding [] { annotation });
								break;
						}
					}
				}
			}
		} else if (ourTagBits != descTagBits) {
			if (ourTagBits == TagBits.AnnotationNonNull) { // requested @NonNull not provided
				char[][] inheritedAnnotationName = null;
				if (descTagBits == TagBits.AnnotationNullable)
					inheritedAnnotationName = env.getNullableAnnotationName();
				currentScope.problemReporter().illegalRedefinitionToNonNullParameter(this.arguments[i], this.descriptor.declaringClass, inheritedAnnotationName);
			}
		}			
	}
}
 
Example #9
Source File: SourceIndexer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void resolveDocument() {
	try {
		IPath path = new Path(this.document.getPath());
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0));
		JavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
		JavaProject javaProject = (JavaProject) model.getJavaProject(project);

		this.options = new CompilerOptions(javaProject.getOptions(true));
		ProblemReporter problemReporter =
				new ProblemReporter(
						DefaultErrorHandlingPolicies.proceedWithAllProblems(),
						this.options,
						new DefaultProblemFactory());

		// Re-parse using normal parser, IndexingParser swallows several nodes, see comment above class.
		this.basicParser = new Parser(problemReporter, false);
		this.basicParser.reportOnlyOneSyntaxError = true;
		this.basicParser.scanner.taskTags = null;
		this.cud = this.basicParser.parse(this.compilationUnit, new CompilationResult(this.compilationUnit, 0, 0, this.options.maxProblemsPerUnit));

		// Use a non model name environment to avoid locks, monitors and such.
		INameEnvironment nameEnvironment = new JavaSearchNameEnvironment(javaProject, JavaModelManager.getJavaModelManager().getWorkingCopies(DefaultWorkingCopyOwner.PRIMARY, true/*add primary WCs*/));
		this.lookupEnvironment = new LookupEnvironment(this, this.options, problemReporter, nameEnvironment);
		reduceParseTree(this.cud);
		this.lookupEnvironment.buildTypeBindings(this.cud, null);
		this.lookupEnvironment.completeTypeBindings();
		this.cud.scope.faultInTypes();
		this.cud.resolve();
	} catch (Exception e) {
		if (JobManager.VERBOSE) {
			e.printStackTrace();
		}
	}
}
 
Example #10
Source File: BindingKeyResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private BindingKeyResolver(BindingKeyParser parser, Compiler compiler, LookupEnvironment environment, CompilationUnitDeclaration outerMostParsedUnit, HashtableOfObject parsedUnits) {
	super(parser);
	this.compiler = compiler;
	this.environment = environment;
	this.outerMostParsedUnit = outerMostParsedUnit;
	this.resolvedUnits = parsedUnits;
}
 
Example #11
Source File: NonNullDefaultAwareTypeAnnotationWalker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IBinaryAnnotation getNonNullAnnotation(LookupEnvironment environment) {
	final char[] nonNullAnnotationName = CharOperation.concat(
					'L', CharOperation.concatWith(environment.getNonNullAnnotationName(), '/'), ';');
	// create the synthetic annotation:
	return new IBinaryAnnotation() {
		@Override
		public char[] getTypeName() {
			return nonNullAnnotationName;
		}
		@Override
		public IBinaryElementValuePair[] getElementValuePairs() {
			return null;
		}
	};
}
 
Example #12
Source File: MethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean isSubsignature(IMethodBinding otherMethod) {
	try {
		LookupEnvironment lookupEnvironment = this.resolver.lookupEnvironment();
		return lookupEnvironment != null
			&& lookupEnvironment.methodVerifier().isMethodSubsignature(this.binding, ((MethodBinding) otherMethod).binding);
	} catch (AbortCompilation e) {
		// don't surface internal exception to clients
		// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=143013
		return false;
	}
}
 
Example #13
Source File: NonNullDefaultAwareTypeAnnotationWalker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/** Create initial walker with non-empty type annotations. */
public NonNullDefaultAwareTypeAnnotationWalker(IBinaryTypeAnnotation[] typeAnnotations,
					int defaultNullness, LookupEnvironment environment) {
	super(typeAnnotations);
	this.nonNullAnnotation = getNonNullAnnotation(environment);
	this.defaultNullness = defaultNullness;
}
 
Example #14
Source File: DefaultBindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
DefaultBindingResolver(LookupEnvironment lookupEnvironment, WorkingCopyOwner workingCopyOwner, BindingTables bindingTables, boolean isRecoveringBindings, boolean fromJavaProject) {
	this.newAstToOldAst = new HashMap();
	this.astNodesToBlockScope = new HashMap();
	this.bindingsToAstNodes = new HashMap();
	this.bindingTables = bindingTables;
	this.scope = new CompilationUnitScope(new CompilationUnitDeclaration(null, null, -1), lookupEnvironment);
	this.workingCopyOwner = workingCopyOwner;
	this.isRecoveringBindings = isRecoveringBindings;
	this.fromJavaProject = fromJavaProject;
}
 
Example #15
Source File: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 5 votes vote down vote up
private static void copyNameLookupAndCompletionEngine(CompletionProposalCollector completionProposalCollector, IJavaCompletionProposal proposal,
		InternalCompletionProposal newProposal) {
	
	try {
		InternalCompletionContext context = (InternalCompletionContext) Reflection.contextField.get(completionProposalCollector);
		InternalExtendedCompletionContext extendedContext = (InternalExtendedCompletionContext) Reflection.extendedContextField.get(context);
		LookupEnvironment lookupEnvironment = (LookupEnvironment) Reflection.lookupEnvironmentField.get(extendedContext);
		Reflection.nameLookupField.set(newProposal, ((SearchableEnvironment) lookupEnvironment.nameEnvironment).nameLookup);
		Reflection.completionEngineField.set(newProposal, lookupEnvironment.typeRequestor);
	} catch (IllegalAccessException ignore) {
		// ignore
	}
}
 
Example #16
Source File: ProcessingEnvImpl.java    From takari-lifecycle with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public LookupEnvironment getLookupEnvironment() {
  LookupEnvironment _le = super.getLookupEnvironment();
  return _le;
}
 
Example #17
Source File: NonNullDefaultAwareTypeAnnotationWalker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/** Create an initial walker without 'real' type annotations, but with a nonnull default. */
public NonNullDefaultAwareTypeAnnotationWalker(int defaultNullness, LookupEnvironment environment) {
	this(defaultNullness, getNonNullAnnotation(environment), false);
}
 
Example #18
Source File: DefaultBindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
LookupEnvironment lookupEnvironment() {
	return this.scope.environment();
}
 
Example #19
Source File: MethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @see IMethodBinding#overrides(IMethodBinding)
 */
public boolean overrides(IMethodBinding otherMethod) {
		LookupEnvironment lookupEnvironment = this.resolver.lookupEnvironment();
		return lookupEnvironment != null
			&& lookupEnvironment.methodVerifier().doesMethodOverride(this.binding, ((MethodBinding) otherMethod).binding);
}
 
Example #20
Source File: BindingKeyResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public BindingKeyResolver(String key, Compiler compiler, LookupEnvironment environment) {
	super(key);
	this.compiler = compiler;
	this.environment = environment;
	this.resolvedUnits = new HashtableOfObject();
}
 
Example #21
Source File: BaseProcessingEnvImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public LookupEnvironment getLookupEnvironment() {
	return _compiler.lookupEnvironment;
}
 
Example #22
Source File: ExecutableElementImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Return true if this method overrides {@code overridden} in the context of {@code type}.  For
 * instance, consider 
 * <pre>
 *   interface A { void f(); }
 *   class B { void f() {} }
 *   class C extends B implements I { }
 * </pre> 
 * In the context of B, B.f() does not override A.f(); they are unrelated.  But in the context of
 * C, B.f() does override A.f().  That is, the copy of B.f() that C inherits overrides A.f().
 * This is equivalent to considering two questions: first, does C inherit B.f(); if so, does
 * the inherited C.f() override A.f().  If B.f() were private, for instance, then in the context
 * of C it would still not override A.f().  
 * 
 * @see javax.lang.model.util.Elements#overrides(ExecutableElement, ExecutableElement, TypeElement)
    * @jls3 8.4.8 Inheritance, Overriding, and Hiding
    * @jls3 9.4.1 Inheritance and Overriding
 */
public boolean overrides(ExecutableElement overridden, TypeElement type)
{
	MethodBinding overriddenBinding = (MethodBinding)((ExecutableElementImpl) overridden)._binding;
	ReferenceBinding overriderContext = (ReferenceBinding)((TypeElementImpl)type)._binding;
	if ((MethodBinding)_binding == overriddenBinding
			|| overriddenBinding.isStatic()
			|| overriddenBinding.isPrivate()
			|| ((MethodBinding)_binding).isStatic()) {
		return false;
	}
	char[] selector = ((MethodBinding)_binding).selector;
	if (!CharOperation.equals(selector, overriddenBinding.selector))
		return false;
	
	// Construct a binding to the equivalent of this (the overrider) as it would be inherited by 'type'.
	// Can only do this if 'type' is descended from the overrider.
	// Second clause of the AND is required to match a peculiar javac behavior.
	if (null == overriderContext.findSuperTypeOriginatingFrom(((MethodBinding)_binding).declaringClass) &&
			null == ((MethodBinding)_binding).declaringClass.findSuperTypeOriginatingFrom(overriderContext)) {
		return false;
	}
	MethodBinding overriderBinding = new MethodBinding((MethodBinding)_binding, overriderContext);
	if (overriderBinding.isPrivate()) {
		// a private method can never override another method.  The other method would either be
		// private itself, in which case it would not be visible; or this would be a restriction 
		// of access, which is a compile-time error.
		return false;
	}
	
	TypeBinding match = overriderBinding.declaringClass.findSuperTypeOriginatingFrom(overriddenBinding.declaringClass);
	if (!(match instanceof ReferenceBinding)) return false;

	org.eclipse.jdt.internal.compiler.lookup.MethodBinding[] superMethods = ((ReferenceBinding)match).getMethods(selector);
	LookupEnvironment lookupEnvironment = _env.getLookupEnvironment();
	if (lookupEnvironment == null) return false;
	MethodVerifier methodVerifier = lookupEnvironment.methodVerifier();
	for (int i = 0, length = superMethods.length; i < length; i++) {
		if (superMethods[i].original() == overriddenBinding) {
			return methodVerifier.doesMethodOverride(overriderBinding, superMethods[i]);
		}
	}
	return false;
}
 
Example #23
Source File: BindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns the compiler lookup environment used by this binding resolver.
 * Returns <code>null</code> if none.
 *
 * @return the lookup environment used by this resolver, or <code>null</code> if none.
 */
LookupEnvironment lookupEnvironment() {
	return null;
}