use of org.eclipse.jdt.core.dom.PrimitiveType in project che by eclipse.
the class GetterSetterUtil method createNarrowCastIfNessecary.
/**
* Checks if the assignment needs a downcast and inserts it if necessary
*
* @param expression the right hand-side
* @param expressionType the type of the right hand-side. Can be null
* @param ast the AST
* @param variableType the Type of the variable the expression will be assigned to
* @param is50OrHigher if <code>true</code> java 5.0 code will be assumed
* @return the casted expression if necessary
*/
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
PrimitiveType castTo = null;
if (variableType.isEqualTo(expressionType))
//no cast for same type
return expression;
if (is50OrHigher) {
if (//$NON-NLS-1$
ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
if (//$NON-NLS-1$
ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
if (//$NON-NLS-1$
ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
}
if (//$NON-NLS-1$
ast.resolveWellKnownType("char").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
if (//$NON-NLS-1$
ast.resolveWellKnownType("byte").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
if (//$NON-NLS-1$
ast.resolveWellKnownType("short").isEqualTo(variableType))
castTo = ast.newPrimitiveType(PrimitiveType.SHORT);
if (castTo != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(expression);
cast.setExpression(parenthesized);
} else
cast.setExpression(expression);
cast.setType(castTo);
return cast;
}
return expression;
}
use of org.eclipse.jdt.core.dom.PrimitiveType in project che by eclipse.
the class IntroduceIndirectionRefactoring method encapsulateInvocation.
private Statement encapsulateInvocation(MethodDeclaration declaration, MethodInvocation invocation) {
final Type type = declaration.getReturnType2();
if (type == null || (type instanceof PrimitiveType && PrimitiveType.VOID.equals(((PrimitiveType) type).getPrimitiveTypeCode())))
return invocation.getAST().newExpressionStatement(invocation);
ReturnStatement statement = invocation.getAST().newReturnStatement();
statement.setExpression(invocation);
return statement;
}
use of org.eclipse.jdt.core.dom.PrimitiveType in project che by eclipse.
the class DelegateMethodCreator method createMethodInvocation.
/**
* Creates the corresponding statement for the method invocation, based on
* the return type.
*
* @param declaration the method declaration where the invocation statement
* is inserted
* @param invocation the method invocation being encapsulated by the
* resulting statement
* @return the corresponding statement
*/
protected Statement createMethodInvocation(final MethodDeclaration declaration, final MethodInvocation invocation) {
Assert.isNotNull(declaration);
Assert.isNotNull(invocation);
Statement statement = null;
final Type type = declaration.getReturnType2();
if (type == null)
statement = createExpressionStatement(invocation);
else {
if (type instanceof PrimitiveType) {
final PrimitiveType primitive = (PrimitiveType) type;
if (primitive.getPrimitiveTypeCode().equals(PrimitiveType.VOID))
statement = createExpressionStatement(invocation);
else
statement = createReturnStatement(invocation);
} else
statement = createReturnStatement(invocation);
}
return statement;
}
use of org.eclipse.jdt.core.dom.PrimitiveType in project che by eclipse.
the class TypeContextChecker method checkParameterTypeSyntax.
public static RefactoringStatus checkParameterTypeSyntax(String type, IJavaProject project) {
String newTypeName = ParameterInfo.stripEllipsis(type.trim()).trim();
String typeLabel = BasicElementLabels.getJavaElementName(type);
if ("".equals(newTypeName.trim())) {
//$NON-NLS-1$
String msg = Messages.format(RefactoringCoreMessages.TypeContextChecker_parameter_type, typeLabel);
return RefactoringStatus.createFatalErrorStatus(msg);
}
if (ParameterInfo.isVarargs(type) && !JavaModelUtil.is50OrHigher(project)) {
String msg = Messages.format(RefactoringCoreMessages.TypeContextChecker_no_vararg_below_50, typeLabel);
return RefactoringStatus.createFatalErrorStatus(msg);
}
List<String> problemsCollector = new ArrayList<String>(0);
Type parsedType = parseType(newTypeName, project, problemsCollector);
boolean valid = parsedType != null;
if (valid && parsedType instanceof PrimitiveType)
valid = !PrimitiveType.VOID.equals(((PrimitiveType) parsedType).getPrimitiveTypeCode());
if (!valid) {
String msg = Messages.format(RefactoringCoreMessages.TypeContextChecker_invalid_type_name, BasicElementLabels.getJavaElementName(newTypeName));
return RefactoringStatus.createFatalErrorStatus(msg);
}
if (problemsCollector.size() == 0)
return null;
RefactoringStatus result = new RefactoringStatus();
for (Iterator<String> iter = problemsCollector.iterator(); iter.hasNext(); ) {
String msg = Messages.format(RefactoringCoreMessages.TypeContextChecker_invalid_type_syntax, new String[] { BasicElementLabels.getJavaElementName(newTypeName), BasicElementLabels.getJavaElementName(iter.next()) });
result.addError(msg);
}
return result;
}
use of org.eclipse.jdt.core.dom.PrimitiveType in project flux by eclipse.
the class ASTNodes method getTypeName.
/**
* Returns the simple name of the type, followed by array dimensions.
* Skips qualifiers, type arguments, and type annotations.
* <p>
* Does <b>not</b> work for WildcardTypes, etc.!
*
* @param type a type that has a simple name
* @return the simple name, followed by array dimensions
* @see #getSimpleNameIdentifier(Name)
* @since 3.10
*/
public static String getTypeName(Type type) {
final StringBuffer buffer = new StringBuffer();
ASTVisitor visitor = new ASTVisitor() {
@Override
public boolean visit(PrimitiveType node) {
buffer.append(node.getPrimitiveTypeCode().toString());
return false;
}
@Override
public boolean visit(SimpleType node) {
buffer.append(getSimpleNameIdentifier(node.getName()));
return false;
}
@Override
public boolean visit(QualifiedType node) {
buffer.append(node.getName().getIdentifier());
return false;
}
@Override
public boolean visit(NameQualifiedType node) {
buffer.append(node.getName().getIdentifier());
return false;
}
@Override
public boolean visit(ParameterizedType node) {
node.getType().accept(this);
return false;
}
@Override
public void endVisit(ArrayType node) {
for (int i = 0; i < node.dimensions().size(); i++) {
//$NON-NLS-1$
buffer.append("[]");
}
}
};
type.accept(visitor);
return buffer.toString();
}
Aggregations