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

The following examples show how to use org.eclipse.jdt.core.dom.IMethodBinding#getExceptionTypes() . 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: ExceptionAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean handleExceptions(IMethodBinding binding, ASTNode node) {
	if (binding == null) {
		return true;
	}
	ITypeBinding[] exceptions = binding.getExceptionTypes();
	for (int i = 0; i < exceptions.length; i++) {
		addException(exceptions[i], node.getAST());
	}
	return true;
}
 
Example 2
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addExceptionsToNewConstructor(MethodDeclaration newConstructor, ImportRewrite importRewrite) {
     IMethodBinding constructorBinding= getSuperConstructorBinding();
     if (constructorBinding == null)
         return;
     ITypeBinding[] exceptions= constructorBinding.getExceptionTypes();
     for (int i= 0; i < exceptions.length; i++) {
Type exceptionType= importRewrite.addImport(exceptions[i], fAnonymousInnerClassNode.getAST());
newConstructor.thrownExceptionTypes().add(exceptionType);
     }
 }
 
Example 3
Source File: ExceptionAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean handleExceptions(IMethodBinding binding, ASTNode node) {
	if (binding == null)
		return true;
	ITypeBinding[] exceptions= binding.getExceptionTypes();
	for (int i= 0; i < exceptions.length; i++) {
		addException(exceptions[i], node.getAST());
	}
	return true;
}
 
Example 4
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean areSubTypeCompatible(IMethodBinding overridden, IMethodBinding overridable) {

		if (overridden.getParameterTypes().length != overridable.getParameterTypes().length)
			return false;

		ITypeBinding overriddenReturn= overridden.getReturnType();
		ITypeBinding overridableReturn= overridable.getReturnType();
		if (overriddenReturn == null || overridableReturn == null)
			return false;

		if (!overriddenReturn.getErasure().isSubTypeCompatible(overridableReturn.getErasure()))
			return false;

		ITypeBinding[] overriddenTypes= overridden.getParameterTypes();
		ITypeBinding[] overridableTypes= overridable.getParameterTypes();
		Assert.isTrue(overriddenTypes.length == overridableTypes.length);
		for (int index= 0; index < overriddenTypes.length; index++) {
			final ITypeBinding overridableErasure= overridableTypes[index].getErasure();
			final ITypeBinding overriddenErasure= overriddenTypes[index].getErasure();
			if (!overridableErasure.isSubTypeCompatible(overriddenErasure) || !overridableErasure.getKey().equals(overriddenErasure.getKey()))
				return false;
		}
		ITypeBinding[] overriddenExceptions= overridden.getExceptionTypes();
		ITypeBinding[] overridableExceptions= overridable.getExceptionTypes();
		boolean checked= false;
		for (int index= 0; index < overriddenExceptions.length; index++) {
			checked= false;
			for (int offset= 0; offset < overridableExceptions.length; offset++) {
				if (overriddenExceptions[index].isSubTypeCompatible(overridableExceptions[offset]))
					checked= true;
			}
			if (!checked)
				return false;
		}
		return true;
	}
 
Example 5
Source File: ExceptionOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void handleResourceDeclarations(TryStatement tryStatement) {
	List<VariableDeclarationExpression> resources= tryStatement.resources();
	for (Iterator<VariableDeclarationExpression> iterator= resources.iterator(); iterator.hasNext();) {
		iterator.next().accept(this);
	}

	//check if the exception is thrown as a result of resource#close()
	boolean exitMarked= false;
	for (VariableDeclarationExpression variable : resources) {
		Type type= variable.getType();
		IMethodBinding methodBinding= Bindings.findMethodInHierarchy(type.resolveBinding(), "close", new ITypeBinding[0]); //$NON-NLS-1$
		if (methodBinding != null) {
			ITypeBinding[] exceptionTypes= methodBinding.getExceptionTypes();
			for (int j= 0; j < exceptionTypes.length; j++) {
				if (matches(exceptionTypes[j])) { // a close() throws the caught exception
					// mark name of resource
					for (VariableDeclarationFragment fragment : (List<VariableDeclarationFragment>) variable.fragments()) {
						SimpleName name= fragment.getName();
						fResult.add(new OccurrenceLocation(name.getStartPosition(), name.getLength(), 0, fDescription));
					}
					if (!exitMarked) {
						// mark exit position
						exitMarked= true;
						Block body= tryStatement.getBody();
						int offset= body.getStartPosition() + body.getLength() - 1; // closing bracket of try block
						fResult.add(new OccurrenceLocation(offset, 1, 0, Messages.format(SearchMessages.ExceptionOccurrencesFinder_occurrence_implicit_close_description,
								BasicElementLabels.getJavaElementName(fException.getName()))));
					}
				}
			}
		}
	}
}
 
Example 6
Source File: ExceptionOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean matches(IMethodBinding binding) {
	if (binding == null)
		return false;
	ITypeBinding[] exceptions= binding.getExceptionTypes();
	for (int i = 0; i < exceptions.length; i++) {
		ITypeBinding exception= exceptions[i];
		if(matches(exception))
			return true;
	}
	return false;
}
 
Example 7
Source File: MethodExitsFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isExitPoint(IMethodBinding binding) {
	if (binding == null)
		return false;
	ITypeBinding[] exceptions= binding.getExceptionTypes();
	for (int i= 0; i < exceptions.length; i++) {
		if (!isCaught(exceptions[i]))
			return true;
	}
	return false;
}
 
Example 8
Source File: ThrownExceptionVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(MethodInvocation node) {
	IMethodBinding methodBinding = node.resolveMethodBinding();
	for(ITypeBinding exceptionType : methodBinding.getExceptionTypes()) {
		typeBindings.add(exceptionType);
	}
	return super.visit(node);
}
 
Example 9
Source File: ThrownExceptionVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(SuperMethodInvocation node) {
	IMethodBinding methodBinding = node.resolveMethodBinding();
	for(ITypeBinding exceptionType : methodBinding.getExceptionTypes()) {
		typeBindings.add(exceptionType);
	}
	return super.visit(node);
}
 
Example 10
Source File: ThrownExceptionVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(ClassInstanceCreation node) {
	IMethodBinding methodBinding = node.resolveConstructorBinding();
	for(ITypeBinding exceptionType : methodBinding.getExceptionTypes()) {
		typeBindings.add(exceptionType);
	}
	return super.visit(node);
}
 
Example 11
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 12
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.4
 */
protected void enhanceExecutable(StringBuilder fqn, String handleIdentifier, String[] path, JvmExecutable result, IMethodBinding method) {
	String name = method.getName();
	fqn.append(name);
	fqn.append('(');
	ITypeBinding[] parameterTypes = method.getParameterTypes();
	for (int i = 0; i < parameterTypes.length; i++) {
		if (i != 0)
			fqn.append(',');
		fqn.append(getQualifiedName(parameterTypes[i]));
	}
	fqn.append(')');
	result.internalSetIdentifier(fqn.toString());
	result.setSimpleName(name);
	setVisibility(result, method.getModifiers());
	result.setDeprecated(method.isDeprecated());
	if (parameterTypes.length > 0) {
		result.setVarArgs(method.isVarargs());
		String[] parameterNames = null;

		// If the method is derived from source, we can efficiently determine the parameter names now.
		//
		ITypeBinding declaringClass = method.getDeclaringClass();
		if (declaringClass.isFromSource()) {
			parameterNames = getParameterNamesFromSource(fqn, method);
		} else {
			// Use the key to determine the signature for the method.
			//
			SegmentSequence signaturex = getSignatureAsSegmentSequence(method);
			ParameterNameInitializer initializer = jdtCompliance.createParameterNameInitializer(method, workingCopyOwner, result, handleIdentifier, path, name, signaturex);
			((JvmExecutableImplCustom)result).setParameterNameInitializer(initializer);
		}

		setParameterNamesAndAnnotations(method, parameterTypes, parameterNames, result);
	}

	ITypeBinding[] exceptionTypes = method.getExceptionTypes();
	if (exceptionTypes.length > 0) {
		InternalEList<JvmTypeReference> exceptions = (InternalEList<JvmTypeReference>)result.getExceptions();
		for (ITypeBinding exceptionType : exceptionTypes) {
			exceptions.addUnique(createTypeReference(exceptionType));
		}
	}
}
 
Example 13
Source File: ConstructorFromSuperclassProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException {
	String name= fTypeNode.getName().getIdentifier();
	MethodDeclaration decl= ast.newMethodDeclaration();
	decl.setConstructor(true);
	decl.setName(ast.newSimpleName(name));
	Block body= ast.newBlock();
	decl.setBody(body);

	SuperConstructorInvocation invocation= null;

	List<SingleVariableDeclaration> parameters= decl.parameters();
	String[] paramNames= getArgumentNames(binding);

	ITypeBinding enclosingInstance= getEnclosingInstance();
	if (enclosingInstance != null) {
		invocation= addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance);
	}

	if (binding == null) {
		decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
	} else {
		decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers()));

		ITypeBinding[] params= binding.getParameterTypes();
		for (int i= 0; i < params.length; i++) {
			SingleVariableDeclaration var= ast.newSingleVariableDeclaration();
			var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext, TypeLocation.LOCAL_VARIABLE));
			var.setName(ast.newSimpleName(paramNames[i]));
			parameters.add(var);
		}

		List<Type> thrownExceptions= decl.thrownExceptionTypes();
		ITypeBinding[] excTypes= binding.getExceptionTypes();
		for (int i= 0; i < excTypes.length; i++) {
			Type excType= getImportRewrite().addImport(excTypes[i], ast, importRewriteContext, TypeLocation.EXCEPTION);
			thrownExceptions.add(excType);
		}

		if (invocation == null) {
			invocation= ast.newSuperConstructorInvocation();
		}

		List<Expression> arguments= invocation.arguments();
		for (int i= 0; i < paramNames.length; i++) {
			Name argument= ast.newSimpleName(paramNames[i]);
			arguments.add(argument);
			addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]); //$NON-NLS-1$
		}
	}

	String bodyStatement = (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true)); //$NON-NLS-1$
	String placeHolder= CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n'));
	if (placeHolder != null) {
		ASTNode todoNode= rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
		body.statements().add(todoNode);
	}
	if (commentSettings != null) {
		String string= CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n'));
		if (string != null) {
			Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
			decl.setJavadoc(javadoc);
		}
	}
	return decl;
}
 
Example 14
Source File: ConstructorFromSuperclassProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException {
	String name= fTypeNode.getName().getIdentifier();
	MethodDeclaration decl= ast.newMethodDeclaration();
	decl.setConstructor(true);
	decl.setName(ast.newSimpleName(name));
	Block body= ast.newBlock();
	decl.setBody(body);

	SuperConstructorInvocation invocation= null;

	List<SingleVariableDeclaration> parameters= decl.parameters();
	String[] paramNames= getArgumentNames(binding);

	ITypeBinding enclosingInstance= getEnclosingInstance();
	if (enclosingInstance != null) {
		invocation= addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance);
	}

	if (binding == null) {
		decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
	} else {
		decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers()));

		ITypeBinding[] params= binding.getParameterTypes();
		for (int i= 0; i < params.length; i++) {
			SingleVariableDeclaration var= ast.newSingleVariableDeclaration();
			var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext));
			var.setName(ast.newSimpleName(paramNames[i]));
			parameters.add(var);
		}

		List<Type> thrownExceptions= decl.thrownExceptionTypes();
		ITypeBinding[] excTypes= binding.getExceptionTypes();
		for (int i= 0; i < excTypes.length; i++) {
			Type excType= getImportRewrite().addImport(excTypes[i], ast, importRewriteContext);
			thrownExceptions.add(excType);
		}

		if (invocation == null) {
			invocation= ast.newSuperConstructorInvocation();
		}

		List<Expression> arguments= invocation.arguments();
		for (int i= 0; i < paramNames.length; i++) {
			Name argument= ast.newSimpleName(paramNames[i]);
			arguments.add(argument);
			addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]); //$NON-NLS-1$
		}
	}

	String bodyStatement= (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true)); //$NON-NLS-1$
	String placeHolder= CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n'));
	if (placeHolder != null) {
		ASTNode todoNode= rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
		body.statements().add(todoNode);
	}
	if (commentSettings != null) {
		String string= CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n'));
		if (string != null) {
			Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
			decl.setJavadoc(javadoc);
		}
	}
	return decl;
}