Java Code Examples for org.eclipse.jdt.internal.corext.dom.Bindings#isVoidType()

The following examples show how to use org.eclipse.jdt.internal.corext.dom.Bindings#isVoidType() . 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: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
private Statement createAddArrayHashCode(IVariableBinding binding) {
	MethodInvocation invoc= fAst.newMethodInvocation();
	if (JavaModelUtil.is50OrHigher(fRewrite.getCu().getJavaProject())) {
		invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE));
		invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
		invoc.arguments().add(getThisAccessForHashCode(binding.getName()));
	} else {
		invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE));
		final IJavaElement element= fType.getJavaElement();
		if (element != null && !"".equals(element.getElementName())) //$NON-NLS-1$
			invoc.setExpression(fAst.newSimpleName(element.getElementName()));
		invoc.arguments().add(getThisAccessForHashCode(binding.getName()));
		ITypeBinding type= binding.getType().getElementType();
		if (!Bindings.isVoidType(type)) {
			if (!type.isPrimitive() || binding.getType().getDimensions() >= 2)
				type= fAst.resolveWellKnownType(JAVA_LANG_OBJECT);
			if (!fCustomHashCodeTypes.contains(type))
				fCustomHashCodeTypes.add(type);
		}
	}
	return prepareAssignment(invoc);
}
 
Example 2
Source File: RefactorProposalUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean supportsExtractVariable(IInvocationContext context) {
	ASTNode node = context.getCoveredNode();
	if (!(node instanceof Expression)) {
		if (context.getSelectionLength() != 0) {
			return false;
		}

		node = context.getCoveringNode();
		if (!(node instanceof Expression)) {
			return false;
		}
	}

	final Expression expression = (Expression) node;
	ITypeBinding binding = expression.resolveTypeBinding();
	if (binding == null || Bindings.isVoidType(binding)) {
		return false;
	}

	return true;
}
 
Example 3
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void replaceSelectedExpressionWithTempDeclaration() throws CoreException {
	ASTRewrite rewrite = fCURewrite.getASTRewrite();
	Expression selectedExpression = getSelectedExpression().getAssociatedExpression(); // whole expression selected

	Expression initializer = (Expression) rewrite.createMoveTarget(selectedExpression);
	VariableDeclarationStatement tempDeclaration = createTempDeclaration(initializer);
	ASTNode replacement;

	ASTNode parent = selectedExpression.getParent();
	boolean isParentLambda = parent instanceof LambdaExpression;
	AST ast = rewrite.getAST();
	if (isParentLambda) {
		Block blockBody = ast.newBlock();
		blockBody.statements().add(tempDeclaration);
		if (!Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) {
			List<VariableDeclarationFragment> fragments = tempDeclaration.fragments();
			SimpleName varName = fragments.get(0).getName();
			ReturnStatement returnStatement = ast.newReturnStatement();
			returnStatement.setExpression(ast.newSimpleName(varName.getIdentifier()));
			blockBody.statements().add(returnStatement);
		}
		replacement = blockBody;
	} else if (ASTNodes.isControlStatementBody(parent.getLocationInParent())) {
		Block block = ast.newBlock();
		block.statements().add(tempDeclaration);
		replacement = block;
	} else {
		replacement = tempDeclaration;
	}
	ASTNode replacee = isParentLambda || !ASTNodes.hasSemicolon((ExpressionStatement) parent, fCu) ? selectedExpression : parent;
	rewrite.replace(replacee, replacement, fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable));
}
 
Example 4
Source File: GetterSetterCorrectionSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Proposes a setter for this field.
 *
 * @param context
 *            the proposal parameter
 * @param relevance
 *            relevance of this proposal
 * @return the proposal if available or null
 */
private static ChangeCorrectionProposal addSetterProposal(ProposalParameter context, int relevance) {
	boolean isBoolean = isBoolean(context);
	String setterName = GetterSetterUtil.getSetterName(context.variableBinding, context.compilationUnit.getJavaProject(), null, isBoolean);
	ITypeBinding declaringType = context.variableBinding.getDeclaringClass();
	if (declaringType == null) {
		return null;
	}

	IMethodBinding method = Bindings.findMethodInHierarchy(declaringType, setterName, new ITypeBinding[] { context.variableBinding.getType() });
	if (method != null && Bindings.isVoidType(method.getReturnType()) && (Modifier.isStatic(method.getModifiers()) == Modifier.isStatic(context.variableBinding.getModifiers()))) {
		Expression assignedValue = getAssignedValue(context);
		if (assignedValue == null) {
			return null; //we don't know how to handle those cases.
		}
		Expression mi = createMethodInvocation(context, method, assignedValue);
		context.astRewrite.replace(context.accessNode.getParent(), mi, null);

		String label = Messages.format(CorrectionMessages.GetterSetterCorrectionSubProcessor_replacewithsetter_description, BasicElementLabels.getJavaCodeString(ASTNodes.asString(context.accessNode)));
		ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.compilationUnit, context.astRewrite, relevance);
		return proposal;
	} else {
		IJavaElement element = context.variableBinding.getJavaElement();
		if (element instanceof IField) {
			IField field = (IField) element;
			try {
				if (RefactoringAvailabilityTester.isSelfEncapsulateAvailable(field)) {
					return new SelfEncapsulateFieldProposal(relevance, field);
				}
			} catch (JavaModelException e) {
				JavaLanguageServerPlugin.log(e);
			}
		}
	}
	return null;
}
 
Example 5
Source File: MethodExitsFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void markReferences() {
	fCaughtExceptions= new ArrayList<ITypeBinding>();
	boolean isVoid= true;
	Type returnType= fMethodDeclaration.getReturnType2();
	if (returnType != null) {
		ITypeBinding returnTypeBinding= returnType.resolveBinding();
		isVoid= returnTypeBinding != null && Bindings.isVoidType(returnTypeBinding);
	}
	fMethodDeclaration.accept(this);
	Block block= fMethodDeclaration.getBody();
	if (block != null) {
		List<Statement> statements= block.statements();
		if (statements.size() > 0) {
			Statement last= statements.get(statements.size() - 1);
			int maxVariableId= LocalVariableIndex.perform(fMethodDeclaration);
			FlowContext flowContext= new FlowContext(0, maxVariableId + 1);
			flowContext.setConsiderAccessMode(false);
			flowContext.setComputeMode(FlowContext.ARGUMENTS);
			InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(flowContext);
			FlowInfo info= flowAnalyzer.perform(new ASTNode[] {last});
			if (!info.isNoReturn() && !isVoid) {
				if (!info.isPartialReturn())
					return;
			}
		}
		int offset= fMethodDeclaration.getStartPosition() + fMethodDeclaration.getLength() - 1; // closing bracket
		fResult.add(new OccurrenceLocation(offset, 1, 0, fExitDescription));
	}
}
 
Example 6
Source File: GetterSetterCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Proposes a setter for this field.
 * 
 * @param context the proposal parameter
 * @param relevance relevance of this proposal
 * @return the proposal if available or null
 */
private static ChangeCorrectionProposal addSetterProposal(ProposalParameter context, int relevance) {
	boolean isBoolean= isBoolean(context);
	String setterName= GetterSetterUtil.getSetterName(context.variableBinding, context.compilationUnit.getJavaProject(), null, isBoolean);
	ITypeBinding declaringType= context.variableBinding.getDeclaringClass();
	if (declaringType == null)
		return null;

	IMethodBinding method= Bindings.findMethodInHierarchy(declaringType, setterName, new ITypeBinding[] { context.variableBinding.getType() });
	if (method != null && Bindings.isVoidType(method.getReturnType()) && (Modifier.isStatic(method.getModifiers()) == Modifier.isStatic(context.variableBinding.getModifiers()))) {
		Expression assignedValue= getAssignedValue(context);
		if (assignedValue == null)
			return null; //we don't know how to handle those cases.
		Expression mi= createMethodInvocation(context, method, assignedValue);
		context.astRewrite.replace(context.accessNode.getParent(), mi, null);

		String label= Messages.format(CorrectionMessages.GetterSetterCorrectionSubProcessor_replacewithsetter_description, BasicElementLabels.getJavaCodeString(ASTNodes.asString(context.accessNode)));
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.compilationUnit, context.astRewrite, relevance, image);
		return proposal;
	} else {
		IJavaElement element= context.variableBinding.getJavaElement();
		if (element instanceof IField) {
			IField field= (IField) element;
			try {
				if (RefactoringAvailabilityTester.isSelfEncapsulateAvailable(field))
					return new SelfEncapsulateFieldProposal(relevance, field);
			} catch (JavaModelException e) {
				JavaPlugin.log(e);
			}
		}
	}
	return null;
}