Java Code Examples for org.eclipse.jdt.core.dom.AST#newNullLiteral()

The following examples show how to use org.eclipse.jdt.core.dom.AST#newNullLiteral() . 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: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Expression getAssignedValue(ParameterObjectFactory pof, String parameterName, IJavaProject javaProject, RefactoringStatus status, ASTRewrite rewrite, ParameterInfo pi, boolean useSuper, ITypeBinding typeBinding, Expression qualifier, ASTNode replaceNode, ITypeRoot typeRoot) {
	AST ast= rewrite.getAST();
	boolean is50OrHigher= JavaModelUtil.is50OrHigher(javaProject);
	Expression assignedValue= handleSimpleNameAssignment(replaceNode, pof, parameterName, ast, javaProject, useSuper);
	if (assignedValue == null) {
		NullLiteral marker= qualifier == null ? null : ast.newNullLiteral();
		Expression fieldReadAccess= pof.createFieldReadAccess(pi, parameterName, ast, javaProject, useSuper, marker);
		assignedValue= GetterSetterUtil.getAssignedValue(replaceNode, rewrite, fieldReadAccess, typeBinding, is50OrHigher);
		boolean markerReplaced= replaceMarker(rewrite, qualifier, assignedValue, marker);
		if (markerReplaced) {
			switch (qualifier.getNodeType()) {
				case ASTNode.METHOD_INVOCATION:
				case ASTNode.CLASS_INSTANCE_CREATION:
				case ASTNode.SUPER_METHOD_INVOCATION:
				case ASTNode.PARENTHESIZED_EXPRESSION:
					status.addWarning(RefactoringCoreMessages.ExtractClassRefactoring_warning_semantic_change, JavaStatusContext.create(typeRoot, replaceNode));
					break;
			}
		}
	}
	return assignedValue;
}
 
Example 2
Source File: MissingAnnotationAttributesProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Expression newDefaultExpression(AST ast, ITypeBinding type, ImportRewriteContext context) {
	if (type.isPrimitive()) {
		String name= type.getName();
		if ("boolean".equals(name)) { //$NON-NLS-1$
			return ast.newBooleanLiteral(false);
		} else {
			return ast.newNumberLiteral("0"); //$NON-NLS-1$
		}
	}
	if (type == ast.resolveWellKnownType("java.lang.String")) { //$NON-NLS-1$
		return ast.newStringLiteral();
	}
	if (type.isArray()) {
		ArrayInitializer initializer= ast.newArrayInitializer();
		initializer.expressions().add(newDefaultExpression(ast, type.getElementType(), context));
		return initializer;
	}
	if (type.isAnnotation()) {
		MarkerAnnotation annotation= ast.newMarkerAnnotation();
		annotation.setTypeName(ast.newName(getImportRewrite().addImport(type, context)));
		return annotation;
	}
	return ast.newNullLiteral();
}
 
Example 3
Source File: ASTNodeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns an expression that is assignable to the given type. <code>null</code> is
 * returned if the type is the 'void' type.
 *
 * @param ast The AST to create the expression for
 * @param type The type of the returned expression
 * @param extraDimensions Extra dimensions to the type
 * @return the Null-literal for reference types, a boolean-literal for a boolean type, a number
 * literal for primitive types or <code>null</code> if the type is void.
 */
public static Expression newDefaultExpression(AST ast, Type type, int extraDimensions) {
	if (extraDimensions == 0 && type.isPrimitiveType()) {
		PrimitiveType primitiveType= (PrimitiveType) type;
		if (primitiveType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) {
			return ast.newBooleanLiteral(false);
		} else if (primitiveType.getPrimitiveTypeCode() == PrimitiveType.VOID) {
			return null;
		} else {
			return ast.newNumberLiteral("0"); //$NON-NLS-1$
		}
	}
	return ast.newNullLiteral();
}
 
Example 4
Source File: ASTNodeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns an expression that is assignable to the given type binding. <code>null</code> is
 * returned if the type is the 'void' type.
 *
 * @param ast The AST to create the expression for
 * @param type The type binding to which the returned expression is compatible to
 * @return the Null-literal for reference types, a boolean-literal for a boolean type, a number
 * literal for primitive types or <code>null</code> if the type is void.
 */
public static Expression newDefaultExpression(AST ast, ITypeBinding type) {
	if (type.isPrimitive()) {
		String name= type.getName();
		if ("boolean".equals(name)) { //$NON-NLS-1$
			return ast.newBooleanLiteral(false);
		} else if ("void".equals(name)) { //$NON-NLS-1$
			return null;
		} else {
			return ast.newNumberLiteral("0"); //$NON-NLS-1$
		}
	}
	return ast.newNullLiteral();
}
 
Example 5
Source File: PolymorphismRefactoring.java    From JDeodorant with MIT License 5 votes vote down vote up
protected Expression generateDefaultValue(ASTRewrite sourceRewriter, AST ast, ITypeBinding returnTypeBinding) {
	Expression returnedExpression = null;
	if(returnTypeBinding.isPrimitive()) {
		if(returnTypeBinding.getQualifiedName().equals("boolean")) {
			returnedExpression = ast.newBooleanLiteral(false);
		}
		else if(returnTypeBinding.getQualifiedName().equals("char")) {
			CharacterLiteral characterLiteral = ast.newCharacterLiteral();
			sourceRewriter.set(characterLiteral, CharacterLiteral.ESCAPED_VALUE_PROPERTY, "\u0000", null);
			returnedExpression = characterLiteral;
		}
		else if(returnTypeBinding.getQualifiedName().equals("int") ||
				returnTypeBinding.getQualifiedName().equals("short") ||
				returnTypeBinding.getQualifiedName().equals("byte")) {
			returnedExpression = ast.newNumberLiteral("0");
		}
		else if(returnTypeBinding.getQualifiedName().equals("long")) {
			returnedExpression = ast.newNumberLiteral("0L");
		}
		else if(returnTypeBinding.getQualifiedName().equals("float")) {
			returnedExpression = ast.newNumberLiteral("0.0f");
		}
		else if(returnTypeBinding.getQualifiedName().equals("double")) {
			returnedExpression = ast.newNumberLiteral("0.0d");
		}
		else if(returnTypeBinding.getQualifiedName().equals("void")) {
			returnedExpression = null;
		}
	}
	else {
		returnedExpression = ast.newNullLiteral();
	}
	return returnedExpression;
}
 
Example 6
Source File: ExtractMethodFragmentRefactoring.java    From JDeodorant with MIT License 5 votes vote down vote up
protected Expression generateDefaultValue(ASTRewrite sourceRewriter, AST ast, ITypeBinding returnTypeBinding) {
	Expression returnedExpression = null;
	if(returnTypeBinding.isPrimitive()) {
		if(returnTypeBinding.getQualifiedName().equals("boolean")) {
			returnedExpression = ast.newBooleanLiteral(false);
		}
		else if(returnTypeBinding.getQualifiedName().equals("char")) {
			CharacterLiteral characterLiteral = ast.newCharacterLiteral();
			sourceRewriter.set(characterLiteral, CharacterLiteral.ESCAPED_VALUE_PROPERTY, "\u0000", null);
			returnedExpression = characterLiteral;
		}
		else if(returnTypeBinding.getQualifiedName().equals("int") ||
				returnTypeBinding.getQualifiedName().equals("short") ||
				returnTypeBinding.getQualifiedName().equals("byte")) {
			returnedExpression = ast.newNumberLiteral("0");
		}
		else if(returnTypeBinding.getQualifiedName().equals("long")) {
			returnedExpression = ast.newNumberLiteral("0L");
		}
		else if(returnTypeBinding.getQualifiedName().equals("float")) {
			returnedExpression = ast.newNumberLiteral("0.0f");
		}
		else if(returnTypeBinding.getQualifiedName().equals("double")) {
			returnedExpression = ast.newNumberLiteral("0.0d");
		}
		else if(returnTypeBinding.getQualifiedName().equals("void")) {
			returnedExpression = null;
		}
	}
	else {
		returnedExpression = ast.newNullLiteral();
	}
	return returnedExpression;
}
 
Example 7
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private RefactoringStatus replaceReferences(ParameterObjectFactory pof, SearchResultGroup group, CompilationUnitRewrite cuRewrite) {
	TextEditGroup writeGroup= cuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_replace_write);
	TextEditGroup readGroup= cuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_replace_read);
	ITypeRoot typeRoot= cuRewrite.getCu();
	IJavaProject javaProject= typeRoot.getJavaProject();
	AST ast= cuRewrite.getAST();

	RefactoringStatus status= new RefactoringStatus();
	String parameterName= fDescriptor.getFieldName();

	SearchMatch[] searchResults= group.getSearchResults();
	for (int j= 0; j < searchResults.length; j++) {
		SearchMatch searchMatch= searchResults[j];
		ASTNode node= NodeFinder.perform(cuRewrite.getRoot(), searchMatch.getOffset(), searchMatch.getLength());
		ASTNode parent= node.getParent();
		boolean isDeclaration= parent instanceof VariableDeclaration && ((VariableDeclaration)parent).getInitializer() != node;
		if (!isDeclaration && node instanceof SimpleName) {
			ASTRewrite rewrite= cuRewrite.getASTRewrite();
			if (parent.getNodeType() == ASTNode.SWITCH_CASE)
				status.addError(RefactoringCoreMessages.ExtractClassRefactoring_error_switch, JavaStatusContext.create(typeRoot, node));

			SimpleName name= (SimpleName) node;
			ParameterInfo pi= getFieldInfo(name.getIdentifier()).pi;
			boolean writeAccess= ASTResolving.isWriteAccess(name);
			if (writeAccess && fDescriptor.isCreateGetterSetter()) {
				boolean useSuper= parent.getNodeType() == ASTNode.SUPER_FIELD_ACCESS;
				Expression qualifier= getQualifier(parent);
				ASTNode replaceNode= getReplacementNode(parent, useSuper, qualifier);
				Expression assignedValue= getAssignedValue(pof, parameterName, javaProject, status, rewrite, pi, useSuper, name.resolveTypeBinding(), qualifier, replaceNode, typeRoot);
				if (assignedValue == null) {
					status.addError(RefactoringCoreMessages.ExtractClassRefactoring_error_unable_to_convert_node, JavaStatusContext.create(typeRoot, replaceNode));
				} else {
					NullLiteral marker= qualifier == null ? null : ast.newNullLiteral();
					Expression access= pof.createFieldWriteAccess(pi, parameterName, ast, javaProject, assignedValue, useSuper, marker);
					replaceMarker(rewrite, qualifier, access, marker);
					rewrite.replace(replaceNode, access, writeGroup);
				}
			} else {
				Expression fieldReadAccess= pof.createFieldReadAccess(pi, parameterName, ast, javaProject, false, null); //qualifier is already there
				rewrite.replace(name, fieldReadAccess, readGroup);
			}
		}
	}
	return status;
}