Search in sources :

Example 1 with PrimitiveType

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;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 2 with PrimitiveType

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;
}
Also used : IType(org.eclipse.jdt.core.IType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Type(org.eclipse.jdt.core.dom.Type) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Example 3 with PrimitiveType

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;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Example 4 with PrimitiveType

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;
}
Also used : NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Example 5 with PrimitiveType

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();
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Aggregations

PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)13 Type (org.eclipse.jdt.core.dom.Type)10 ArrayType (org.eclipse.jdt.core.dom.ArrayType)7 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)5 IType (org.eclipse.jdt.core.IType)4 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)4 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 SimpleType (org.eclipse.jdt.core.dom.SimpleType)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 AST (org.eclipse.jdt.core.dom.AST)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 Block (org.eclipse.jdt.core.dom.Block)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 Javadoc (org.eclipse.jdt.core.dom.Javadoc)3 Name (org.eclipse.jdt.core.dom.Name)3 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 ArrayList (java.util.ArrayList)2