Java Code Examples for org.eclipse.jdt.internal.compiler.lookup.MethodBinding#isStatic()

The following examples show how to use org.eclipse.jdt.internal.compiler.lookup.MethodBinding#isStatic() . 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: PatchExtensionMethod.java    From EasyMPermission with MIT License 6 votes vote down vote up
private static List<MethodBinding> getApplicableExtensionMethodsDefinedInProvider(EclipseNode typeNode, ReferenceBinding extensionMethodProviderBinding,
		TypeBinding receiverType) {
	
	List<MethodBinding> extensionMethods = new ArrayList<MethodBinding>();
	CompilationUnitScope cuScope = ((CompilationUnitDeclaration) typeNode.top().get()).scope;
	for (MethodBinding method : extensionMethodProviderBinding.methods()) {
		if (!method.isStatic()) continue;
		if (!method.isPublic()) continue;
		if (method.parameters == null || method.parameters.length == 0) continue;
		TypeBinding firstArgType = method.parameters[0];
		if (receiverType.isProvablyDistinct(firstArgType) && !receiverType.isCompatibleWith(firstArgType.erasure())) continue;
		TypeBinding[] argumentTypes = Arrays.copyOfRange(method.parameters, 1, method.parameters.length);
		if ((receiverType instanceof ReferenceBinding) && ((ReferenceBinding) receiverType).getExactMethod(method.selector, argumentTypes, cuScope) != null) continue;
		extensionMethods.add(method);
	}
	return extensionMethods;
}
 
Example 2
Source File: Factory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public TypeMirror getReceiverType(MethodBinding binding) {
	if (binding != null) {
		if (binding.receiver != null) {
			return _env.getFactory().newTypeMirror(binding.receiver);
		}
		if (binding.declaringClass != null) {
			if (!binding.isStatic() && (!binding.isConstructor() || binding.declaringClass.isMemberType())) {
				return _env.getFactory().newTypeMirror(binding.declaringClass);	
			}
		}
	}
	return NoTypeImpl.NO_TYPE_NONE;
}
 
Example 3
Source File: ExecutableElementImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean hides(Element hidden)
{
	if (!(hidden instanceof ExecutableElementImpl)) {
		return false;
	}
	MethodBinding hiderBinding = (MethodBinding)_binding;
	MethodBinding hiddenBinding = (MethodBinding)((ExecutableElementImpl)hidden)._binding;
	if (hiderBinding == hiddenBinding) {
		return false;
	}
	if (hiddenBinding.isPrivate()) {
		return false;
	}
	// See JLS 8.4.8: hiding only applies to static methods
	if (!hiderBinding.isStatic() || !hiddenBinding.isStatic()) {
		return false;
	}
	// check names
	if (!CharOperation.equals(hiddenBinding.selector, hiderBinding.selector)) {
		return false;
	}
	// check parameters
	if (!_env.getLookupEnvironment().methodVerifier().isMethodSubsignature(hiderBinding, hiddenBinding)) {
		return false;
	}
	return null != hiderBinding.declaringClass.findSuperTypeOriginatingFrom(hiddenBinding.declaringClass); 
}
 
Example 4
Source File: OrLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void matchLevelAndReportImportRef(ImportReference importRef, Binding binding, MatchLocator locator) throws CoreException {

	// for static import, binding can be a field binding or a member type binding
	// verify that in this case binding is static and use declaring class for fields
	Binding refBinding = binding;
	if (importRef.isStatic()) {
		if (binding instanceof FieldBinding) {
			FieldBinding fieldBinding = (FieldBinding) binding;
			if (!fieldBinding.isStatic()) return;
			refBinding = fieldBinding.declaringClass;
		} else if (binding instanceof MethodBinding) {
			MethodBinding methodBinding = (MethodBinding) binding;
			if (!methodBinding.isStatic()) return;
			refBinding = methodBinding.declaringClass;
		} else if (binding instanceof MemberTypeBinding) {
			MemberTypeBinding memberBinding = (MemberTypeBinding) binding;
			if (!memberBinding.isStatic()) return;
		}
	}

	// Look for closest pattern
	PatternLocator closestPattern = null;
	int level = IMPOSSIBLE_MATCH;
	for (int i = 0, length = this.patternLocators.length; i < length; i++) {
		PatternLocator patternLocator = this.patternLocators[i];
		int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH : patternLocator.resolveLevel(refBinding);
		if (newLevel > level) {
			closestPattern = patternLocator;
			if (newLevel == ACCURATE_MATCH) break;
			level = newLevel;
		}
	}
	if (closestPattern != null) {
		closestPattern.matchLevelAndReportImportRef(importRef, binding, locator);
	}
}
 
Example 5
Source File: ConstantPool.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int literalIndexForMethodHandle(MethodBinding binding) {
	boolean isInterface = binding.declaringClass.isInterface();
	int referenceKind =
		isInterface ? binding.isStatic() ? MethodHandleRefKindInvokeStatic : binding.isPrivate() ? MethodHandleRefKindInvokeSpecial : MethodHandleRefKindInvokeInterface
		: binding.isConstructor() ? MethodHandleRefKindNewInvokeSpecial
		: binding.isStatic() ? MethodHandleRefKindInvokeStatic
		: MethodHandleRefKindInvokeVirtual;
	
	return literalIndexForMethodHandle(referenceKind, binding.declaringClass, binding.selector, binding.signature(), isInterface);
}
 
Example 6
Source File: PatchDelegate.java    From EasyMPermission with MIT License 4 votes vote down vote up
private static void addAllMethodBindings0(List<BindingTuple> list, TypeBinding binding, Set<String> banList, char[] fieldName, ASTNode responsible) throws DelegateRecursion {
	if (binding instanceof SourceTypeBinding) ((SourceTypeBinding) binding).scope.environment().globalOptions.storeAnnotations = true;
	if (binding == null) return;
	
	TypeBinding inner;
	
	if (binding instanceof ParameterizedTypeBinding) {
		inner = ((ParameterizedTypeBinding) binding).genericType();
	} else {
		inner = binding;
	}
	
	if (inner instanceof SourceTypeBinding) {
		ClassScope cs = ((SourceTypeBinding)inner).scope;
		if (cs != null) {
			try {
				Reflection.classScopeBuildFieldsAndMethodsMethod.invoke(cs);
			} catch (Exception e) {
				// See 'Reflection' class for why we ignore this exception.
			}
		}
	}
	
	if (binding instanceof ReferenceBinding) {
		ReferenceBinding rb = (ReferenceBinding) binding;
		MethodBinding[] availableMethods = rb.availableMethods();
		FieldBinding[] availableFields = rb.availableFields();
		failIfContainsAnnotation(binding, availableMethods); 
		failIfContainsAnnotation(binding, availableFields); 
		
		MethodBinding[] parameterizedSigs = availableMethods;
		MethodBinding[] baseSigs = parameterizedSigs;
		if (binding instanceof ParameterizedTypeBinding) {
			baseSigs = ((ParameterizedTypeBinding)binding).genericType().availableMethods();
			if (baseSigs.length != parameterizedSigs.length) {
				// The last known state of eclipse source says this can't happen, so we rely on it,
				// but if this invariant is broken, better to go with 'arg0' naming instead of crashing.
				baseSigs = parameterizedSigs;
			}
		}
		for (int i = 0; i < parameterizedSigs.length; i++) {
			MethodBinding mb = parameterizedSigs[i];
			String sig = printSig(mb);
			if (mb.isStatic()) continue;
			if (mb.isBridge()) continue;
			if (mb.isConstructor()) continue;
			if (mb.isDefaultAbstract()) continue;
			if (!mb.isPublic()) continue;
			if (mb.isSynthetic()) continue;
			if (!banList.add(sig)) continue; // If add returns false, it was already in there.
			BindingTuple pair = new BindingTuple(mb, baseSigs[i], fieldName, responsible);
			list.add(pair);
		}
		addAllMethodBindings0(list, rb.superclass(), banList, fieldName, responsible);
		ReferenceBinding[] interfaces = rb.superInterfaces();
		if (interfaces != null) {
			for (ReferenceBinding iface : interfaces) addAllMethodBindings0(list, iface, banList, fieldName, responsible);
		}
	}
}
 
Example 7
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 8
Source File: InternalExtendedCompletionContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void searchVisibleLocalMethods(
		MethodBinding[] methods,
		ReferenceBinding receiverType,
		Scope scope,
		InvocationSite invocationSite,
		Scope invocationScope,
		boolean onlyStaticMethods,
		ObjectVector methodsFound) {
	ObjectVector newMethodsFound =  new ObjectVector();
	// Inherited methods which are hidden by subclasses are filtered out
	// No visibility checks can be performed without the scope & invocationSite

	next : for (int f = methods.length; --f >= 0;) {
		MethodBinding method = methods[f];

		if (method.isSynthetic()) continue next;

		if (method.isDefaultAbstract())	continue next;

		if (method.isConstructor()) continue next;

		if (onlyStaticMethods && !method.isStatic()) continue next;

		if (!method.canBeSeenBy(receiverType, invocationSite, scope)) continue next;

		for (int i = methodsFound.size; --i >= 0;) {
			MethodBinding otherMethod = (MethodBinding) methodsFound.elementAt(i);
			if (method == otherMethod)
				continue next;

			if (CharOperation.equals(method.selector, otherMethod.selector, true)) {
				if (this.lookupEnvironment.methodVerifier().isMethodSubsignature(otherMethod, method)) {
					continue next;
				}
			}
		}

		newMethodsFound.add(method);
	}

	methodsFound.addAll(newMethodsFound);
}
 
Example 9
Source File: CodeSnippetScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public final boolean canBeSeenByForCodeSnippet(MethodBinding methodBinding, TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
	if (methodBinding.isPublic()) return true;

	ReferenceBinding invocationType = (ReferenceBinding) receiverType;
	if (TypeBinding.equalsEquals(invocationType, methodBinding.declaringClass)) return true;

	if (methodBinding.isProtected()) {
		// answer true if the invocationType is the declaringClass or they are in the same package
		// OR the invocationType is a subclass of the declaringClass
		//    AND the receiverType is the invocationType or its subclass
		//    OR the method is a static method accessed directly through a type
		if (TypeBinding.equalsEquals(invocationType, methodBinding.declaringClass)) return true;
		if (invocationType.fPackage == methodBinding.declaringClass.fPackage) return true;
		if (methodBinding.declaringClass.isSuperclassOf(invocationType)) {
			if (invocationSite.isSuperAccess()) return true;
			// receiverType can be an array binding in one case... see if you can change it
			if (receiverType instanceof ArrayBinding)
				return false;
			if (invocationType.isSuperclassOf((ReferenceBinding) receiverType))
				return true;
			if (methodBinding.isStatic())
				return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
		}
		return false;
	}

	if (methodBinding.isPrivate()) {
		// answer true if the receiverType is the declaringClass
		// AND the invocationType and the declaringClass have a common enclosingType
		if (TypeBinding.notEquals(receiverType, methodBinding.declaringClass)) return false;

		if (TypeBinding.notEquals(invocationType, methodBinding.declaringClass)) {
			ReferenceBinding outerInvocationType = invocationType;
			ReferenceBinding temp = outerInvocationType.enclosingType();
			while (temp != null) {
				outerInvocationType = temp;
				temp = temp.enclosingType();
			}

			ReferenceBinding outerDeclaringClass = methodBinding.declaringClass;
			temp = outerDeclaringClass.enclosingType();
			while (temp != null) {
				outerDeclaringClass = temp;
				temp = temp.enclosingType();
			}
			if (TypeBinding.notEquals(outerInvocationType, outerDeclaringClass)) return false;
		}
		return true;
	}

	// isDefault()
	if (invocationType.fPackage != methodBinding.declaringClass.fPackage) return false;

	// receiverType can be an array binding in one case... see if you can change it
	if (receiverType instanceof ArrayBinding)
		return false;
	ReferenceBinding type = (ReferenceBinding) receiverType;
	PackageBinding declaringPackage = methodBinding.declaringClass.fPackage;
	TypeBinding originalDeclaringClass = methodBinding.declaringClass .original();
	do {
		if (type.isCapture()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=285002
			if (TypeBinding.equalsEquals(originalDeclaringClass, type.erasure().original())) return true;
		} else {
			if (TypeBinding.equalsEquals(originalDeclaringClass, type.original())) return true;
		}
		if (declaringPackage != type.fPackage) return false;
	} while ((type = type.superclass()) != null);
	return false;
}
 
Example 10
Source File: MessageSend.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * MessageSend code generation
 *
 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
 * @param valueRequired boolean
 */
public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
	int pc = codeStream.position;
	// generate receiver/enclosing instance access
	MethodBinding codegenBinding = this.binding instanceof PolymorphicMethodBinding ? this.binding : this.binding.original();
	boolean isStatic = codegenBinding.isStatic();
	if (isStatic) {
		this.receiver.generateCode(currentScope, codeStream, false);
	} else if ((this.bits & ASTNode.DepthMASK) != 0 && this.receiver.isImplicitThis()) { // outer access ?
		// outer method can be reached through emulation if implicit access
		ReferenceBinding targetType = currentScope.enclosingSourceType().enclosingTypeAt((this.bits & ASTNode.DepthMASK) >> ASTNode.DepthSHIFT);
		Object[] path = currentScope.getEmulationPath(targetType, true /*only exact match*/, false/*consider enclosing arg*/);
		codeStream.generateOuterAccess(path, this, targetType, currentScope);
	} else {
		this.receiver.generateCode(currentScope, codeStream, true);
		if ((this.bits & NeedReceiverGenericCast) != 0) {
			codeStream.checkcast(this.actualReceiverType);
		}
	}
	codeStream.recordPositionsFrom(pc, this.sourceStart);
	// generate arguments
	generateArguments(this.binding, this.arguments, currentScope, codeStream);
	pc = codeStream.position;
	// actual message invocation
	if (this.syntheticAccessor == null){
		TypeBinding constantPoolDeclaringClass = CodeStream.getConstantPoolDeclaringClass(currentScope, codegenBinding, this.actualReceiverType, this.receiver.isImplicitThis());
		if (isStatic){
			codeStream.invoke(Opcodes.OPC_invokestatic, codegenBinding, constantPoolDeclaringClass, this.typeArguments);
		} else if((this.receiver.isSuper()) || codegenBinding.isPrivate()){
			codeStream.invoke(Opcodes.OPC_invokespecial, codegenBinding, constantPoolDeclaringClass, this.typeArguments);
		} else if (constantPoolDeclaringClass.isInterface()) { // interface or annotation type
			codeStream.invoke(Opcodes.OPC_invokeinterface, codegenBinding, constantPoolDeclaringClass, this.typeArguments);
		} else {
			codeStream.invoke(Opcodes.OPC_invokevirtual, codegenBinding, constantPoolDeclaringClass, this.typeArguments);
		}
	} else {
		codeStream.invoke(Opcodes.OPC_invokestatic, this.syntheticAccessor, null /* default declaringClass */, this.typeArguments);
	}
	// required cast must occur even if no value is required
	if (this.valueCast != null) codeStream.checkcast(this.valueCast);
	if (valueRequired){
		// implicit conversion if necessary
		codeStream.generateImplicitConversion(this.implicitConversion);
	} else {
		boolean isUnboxing = (this.implicitConversion & TypeIds.UNBOXING) != 0;
		// conversion only generated if unboxing
		if (isUnboxing) codeStream.generateImplicitConversion(this.implicitConversion);
		switch (isUnboxing ? postConversionType(currentScope).id : codegenBinding.returnType.id) {
			case T_long :
			case T_double :
				codeStream.pop2();
				break;
			case T_void :
				break;
			default :
				codeStream.pop();
		}
	}
	codeStream.recordPositionsFrom(pc, (int)(this.nameSourcePosition >>> 32)); // highlight selector
}