Java Code Examples for org.eclipse.jdt.core.dom.IMethodBinding#getModifiers()

The following examples show how to use org.eclipse.jdt.core.dom.IMethodBinding#getModifiers() . 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: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmOperation createOperation(StringBuilder qualifiedName, String handleIdentifier, String[] path, IMethodBinding method) {
	JvmOperation result = TypesFactory.eINSTANCE.createJvmOperation();
	enhanceGenericDeclaration(result, method.getTypeParameters());
	enhanceExecutable(qualifiedName, handleIdentifier, path, result, method);
	int modifiers = method.getModifiers();
	result.setAbstract(Modifier.isAbstract(modifiers));
	result.setFinal(Modifier.isFinal(modifiers));
	result.setStatic(Modifier.isStatic(modifiers));
	result.setStrictFloatingPoint(Modifier.isStrictfp(modifiers));
	result.setSynchronized(Modifier.isSynchronized(modifiers));
	result.setNative(Modifier.isNative(modifiers));
	result.setReturnType(createTypeReference(method.getReturnType()));
	createAnnotationValues(method, result);
	return result;
}
 
Example 2
Source File: VarargsWarningsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static void addAddSafeVarargsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode coveringNode= problem.getCoveringNode(context.getASTRoot());

	MethodDeclaration methodDeclaration= ASTResolving.findParentMethodDeclaration(coveringNode);
	if (methodDeclaration == null)
		return;
	
	IMethodBinding methodBinding= methodDeclaration.resolveBinding();
	if (methodBinding == null)
		return;
	
	int modifiers= methodBinding.getModifiers();
	if (!Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers) && ! methodBinding.isConstructor())
		return; 
	
	String label= CorrectionMessages.VarargsWarningsSubProcessor_add_safevarargs_label;
	AddSafeVarargsProposal proposal= new AddSafeVarargsProposal(label, context.getCompilationUnit(), methodDeclaration, null, IProposalRelevance.ADD_SAFEVARARGS);
	proposals.add(proposal);
}
 
Example 3
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void modifySourceStaticMethodInvocationsInTargetClass(MethodDeclaration newMethodDeclaration, ASTRewrite targetRewriter) {
	ExpressionExtractor extractor = new ExpressionExtractor();	
	List<Expression> sourceMethodInvocations = extractor.getMethodInvocations(sourceMethod.getBody());
	List<Expression> newMethodInvocations = extractor.getMethodInvocations(newMethodDeclaration.getBody());
	int i = 0;
	for(Expression expression : sourceMethodInvocations) {
		if(expression instanceof MethodInvocation) {
			MethodInvocation methodInvocation = (MethodInvocation)expression;
			IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
			if((methodBinding.getModifiers() & Modifier.STATIC) != 0 &&
					methodBinding.getDeclaringClass().isEqualTo(sourceTypeDeclaration.resolveBinding()) &&
					!additionalMethodsToBeMoved.containsKey(methodInvocation)) {
				AST ast = newMethodDeclaration.getAST();
				SimpleName qualifier = ast.newSimpleName(sourceTypeDeclaration.getName().getIdentifier());
				targetRewriter.set(newMethodInvocations.get(i), MethodInvocation.EXPRESSION_PROPERTY, qualifier, null);
				this.additionalTypeBindingsToBeImportedInTargetClass.add(sourceTypeDeclaration.resolveBinding());
			}
		}
		i++;
	}
}
 
Example 4
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isVisibleInHierarchy(IMethodBinding member, IPackageBinding pack) {
	int otherflags= member.getModifiers();
	ITypeBinding declaringType= member.getDeclaringClass();
	if (Modifier.isPublic(otherflags) || Modifier.isProtected(otherflags) || (declaringType != null && declaringType.isInterface())) {
		return true;
	} else if (Modifier.isPrivate(otherflags)) {
		return false;
	}
	return declaringType != null && pack == declaringType.getPackage();
}
 
Example 5
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isDefaultMethod(IMethodBinding method) {
	int modifiers= method.getModifiers();
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=405517#c7
	ITypeBinding declaringClass= method.getDeclaringClass();
	if (declaringClass.isInterface()) {
		return !Modifier.isAbstract(modifiers) && !Modifier.isStatic(modifiers);
	}
	return false;
}
 
Example 6
Source File: MethodObject.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean overridesMethod(Set<ITypeBinding> typeBindings) {
	IMethodBinding methodBinding = getMethodDeclaration().resolveBinding();
	Set<ITypeBinding> superTypeBindings = new LinkedHashSet<ITypeBinding>();
	for(ITypeBinding typeBinding : typeBindings) {
		ITypeBinding superClassTypeBinding = typeBinding.getSuperclass();
    	if(superClassTypeBinding != null)
    		superTypeBindings.add(superClassTypeBinding);
    	ITypeBinding[] interfaceTypeBindings = typeBinding.getInterfaces();
    	for(ITypeBinding interfaceTypeBinding : interfaceTypeBindings)
    		superTypeBindings.add(interfaceTypeBinding);
		if(typeBinding.isInterface()) {
			IMethodBinding[] interfaceMethodBindings = typeBinding.getDeclaredMethods();
    		for(IMethodBinding interfaceMethodBinding : interfaceMethodBindings) {
    			if(methodBinding.overrides(interfaceMethodBinding) || methodBinding.toString().equals(interfaceMethodBinding.toString()))
    				return true;
    		}
		}
		else {
			IMethodBinding[] superClassMethodBindings = typeBinding.getDeclaredMethods();
	    	for(IMethodBinding superClassMethodBinding : superClassMethodBindings) {
	    		if(methodBinding.overrides(superClassMethodBinding) || (methodBinding.toString().equals(superClassMethodBinding.toString())
	    				&& (superClassMethodBinding.getModifiers() & Modifier.PRIVATE) == 0) )
	    			return true;
	    	}
		}
	}
	if(!superTypeBindings.isEmpty()) {
		return overridesMethod(superTypeBindings);
	}
	else
		return false;
}
 
Example 7
Source File: AbstractMethodFragment.java    From JDeodorant with MIT License 5 votes vote down vote up
protected void processConstructorInvocation(ConstructorInvocation constructorInvocation) {
	IMethodBinding methodBinding = constructorInvocation.resolveConstructorBinding();
	String originClassName = methodBinding.getDeclaringClass().getQualifiedName();
	TypeObject originClassTypeObject = TypeObject.extractTypeObject(originClassName);
	String methodInvocationName = methodBinding.getName();
	String qualifiedName = methodBinding.getReturnType().getQualifiedName();
	TypeObject returnType = TypeObject.extractTypeObject(qualifiedName);
	ConstructorInvocationObject constructorInvocationObject = new ConstructorInvocationObject(originClassTypeObject, methodInvocationName, returnType);
	constructorInvocationObject.setConstructorInvocation(constructorInvocation);
	ITypeBinding[] parameterTypes = methodBinding.getParameterTypes();
	for(ITypeBinding parameterType : parameterTypes) {
		String qualifiedParameterName = parameterType.getQualifiedName();
		TypeObject typeObject = TypeObject.extractTypeObject(qualifiedParameterName);
		constructorInvocationObject.addParameter(typeObject);
	}
	ITypeBinding[] thrownExceptionTypes = methodBinding.getExceptionTypes();
	for(ITypeBinding thrownExceptionType : thrownExceptionTypes) {
		constructorInvocationObject.addThrownException(thrownExceptionType.getQualifiedName());
	}
	if((methodBinding.getModifiers() & Modifier.STATIC) != 0)
		constructorInvocationObject.setStatic(true);
	addConstructorInvocation(constructorInvocationObject);
	List<Expression> arguments = constructorInvocation.arguments();
	for(Expression argument : arguments) {
		if(argument instanceof SimpleName) {
			SimpleName argumentName = (SimpleName)argument;
			IBinding binding = argumentName.resolveBinding();
			if(binding != null && binding.getKind() == IBinding.VARIABLE) {
				IVariableBinding variableBinding = (IVariableBinding)binding;
				if(variableBinding.isParameter()) {
					PlainVariable variable = new PlainVariable(variableBinding);
					addParameterPassedAsArgumentInConstructorInvocation(variable, constructorInvocationObject);
				}
			}
		}
	}
}
 
Example 8
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isStaticMethodCall(ASTNode node) {
	if (node instanceof SimpleName){
		SimpleName simpleName = (SimpleName) node;
		IBinding binding = simpleName.resolveBinding();
		if(binding != null && binding.getKind() == IBinding.METHOD) {
			IMethodBinding methodBinding = (IMethodBinding)binding;
			if((methodBinding.getModifiers() & Modifier.STATIC) != 0) 
				return true;
		}
	}
	return false;
}
 
Example 9
Source File: JsInteropUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private static boolean isDebugger(IMethodBinding methodBinding) {
  int modifiers = methodBinding.getModifiers();
  return methodBinding.getName().equals("debugger")
      && Modifier.isNative(modifiers)
      && JdtUtils.isStatic(methodBinding);
}
 
Example 10
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean visit(ITypeBinding type) {
	IMethodBinding[] methods= type.getDeclaredMethods();
	for (int i= 0; i < methods.length; i++) {
		IMethodBinding candidate= methods[i];
		if (candidate.getMethodDeclaration() == fOriginalMethod.getMethodDeclaration()) {
			continue;
		}
		ITypeBinding candidateDeclaringType= candidate.getDeclaringClass();
		if (fDeclaringType != candidateDeclaringType) {
			int modifiers= candidate.getModifiers();
			if (candidateDeclaringType.isInterface() && Modifier.isStatic(modifiers)) {
				continue;
			}
			if (Modifier.isPrivate(modifiers)) {
				continue;
			}
		}
		if (fOriginalMethod.getName().equals(candidate.getName()) && !fOriginalMethod.overrides(candidate)) {
			ITypeBinding[] originalParameterTypes= fOriginalMethod.getParameterTypes();
			ITypeBinding[] candidateParameterTypes= candidate.getParameterTypes();
			
			boolean couldBeAmbiguous;
			if (originalParameterTypes.length == candidateParameterTypes.length) {
				couldBeAmbiguous= true;
			} else if (fOriginalMethod.isVarargs() || candidate.isVarargs() ) {
				int candidateMinArgumentCount= candidateParameterTypes.length;
				if (candidate.isVarargs())
					candidateMinArgumentCount--;
				couldBeAmbiguous= fArgumentCount >= candidateMinArgumentCount;
			} else {
				couldBeAmbiguous= false;
			}
			if (couldBeAmbiguous) {
				ITypeBinding parameterType= ASTResolving.getParameterTypeBinding(candidate, fArgIndex);
				if (parameterType != null && parameterType.getFunctionalInterfaceMethod() != null) {
					if (!fExpressionIsExplicitlyTyped) {
						/* According to JLS8 15.12.2.2, implicitly typed lambda expressions are not "pertinent to applicability"
						 * and hence potentially applicable methods are always "applicable by strict invocation",
						 * regardless of whether argument expressions are compatible with the method's parameter types or not.
						 * If there are multiple such methods, 15.12.2.5 results in an ambiguous method invocation.
						 */
						return false;
					}
					/* Explicitly typed lambda expressions are pertinent to applicability, and hence
					 * compatibility with the corresponding method parameter type is checked. And since this check
					 * separates functional interface methods by their void-compatibility state, functional interfaces
					 * with a different void compatibility are not applicable any more and hence can't cause
					 * an ambiguous method invocation.
					 */
					ITypeBinding origParamType= ASTResolving.getParameterTypeBinding(fOriginalMethod, fArgIndex);
					boolean originalIsVoidCompatible=  Bindings.isVoidType(origParamType.getFunctionalInterfaceMethod().getReturnType());
					boolean candidateIsVoidCompatible= Bindings.isVoidType(parameterType.getFunctionalInterfaceMethod().getReturnType());
					if (originalIsVoidCompatible == candidateIsVoidCompatible) {
						return false;
					}
				}
			}
		}
	}
	return true;
}