@NotNull public static PsiPrimitiveType getPrimitiveTypeByText(String typeText) { for (final PsiPrimitiveType primitive : PRIMITIVES) { if (PsiType.VOID.equals(primitive)) { return primitive; } if (primitive.getCanonicalText().equals(typeText)) { return primitive; } } assert false : "Unknown primitive type"; return null; }
public static String[] getParameterString(ExtractInfoHelper helper, boolean useCanonicalText) { int i = 0; ParameterInfo[] infos = helper.getParameterInfos(); int number = 0; for (ParameterInfo info : infos) { if (info.passAsParameter()) number++; } ArrayList<String> params = new ArrayList<String>(); for (ParameterInfo info : infos) { if (info.passAsParameter()) { PsiType paramType = info.getType(); final PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(paramType); if (unboxed != null) paramType = unboxed; String paramTypeText; if (paramType == null || paramType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) { paramTypeText = ""; } else { paramTypeText = (useCanonicalText ? paramType.getCanonicalText() : paramType.getPresentableText()) + " "; } params.add(paramTypeText + info.getName() + (i < number - 1 ? ", " : "")); i++; } } return ArrayUtil.toStringArray(params); }
public static GrMethod createMethod(ExtractMethodInfoHelper helper) { StringBuilder buffer = new StringBuilder(); // Add signature PsiType type = helper.getOutputType(); final PsiPrimitiveType outUnboxed = PsiPrimitiveType.getUnboxedType(type); if (outUnboxed != null) type = outUnboxed; String modifier = getModifierString(helper); String typeText = getTypeString(helper, false, modifier); buffer.append(modifier); buffer.append(typeText); buffer.append(helper.getName()); buffer.append("("); for (String param : getParameterString(helper, true)) { buffer.append(param); } buffer.append(") { \n"); GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject()); generateBody(helper, type == PsiType.VOID, buffer, helper.isForceReturn()); buffer.append("\n}"); String methodText = buffer.toString(); GrMethod method = factory.createMethodFromText(methodText); LOG.assertTrue(method != null); return method; }
public static boolean isAssociativeOperation(PsiPolyadicExpression expression) { final IElementType tokenType = expression.getOperationTokenType(); final PsiType type = expression.getType(); final PsiPrimitiveType primitiveType; if (type instanceof PsiClassType) { primitiveType = PsiPrimitiveType.getUnboxedType(type); if (primitiveType == null) { return false; } } else if (type instanceof PsiPrimitiveType) { primitiveType = (PsiPrimitiveType) type; } else { return false; } if (JavaTokenType.PLUS == tokenType || JavaTokenType.ASTERISK == tokenType) { return !PsiType.FLOAT.equals(primitiveType) && !PsiType.DOUBLE.equals(primitiveType); } else if (JavaTokenType.EQEQ == tokenType || JavaTokenType.NE == tokenType) { return PsiType.BOOLEAN.equals(primitiveType); } else if (JavaTokenType.AND == tokenType || JavaTokenType.OR == tokenType || JavaTokenType.XOR == tokenType) { return true; } else if (JavaTokenType.OROR == tokenType || JavaTokenType.ANDAND == tokenType) { return true; } return false; }
public static PsiType boxPrimitiveType( @Nullable PsiType result, @NotNull PsiManager manager, @NotNull GlobalSearchScope resolveScope, boolean boxVoid) { if (result instanceof PsiPrimitiveType && (boxVoid || result != PsiType.VOID)) { PsiPrimitiveType primitive = (PsiPrimitiveType) result; String boxedTypeName = primitive.getBoxedTypeName(); if (boxedTypeName != null) { return GroovyPsiManager.getInstance(manager.getProject()) .createTypeByFQClassName(boxedTypeName, resolveScope); } } return result; }
private static Map<String, PsiType> getCompatibleTypeNames( @NotNull PsiType type, @Nullable PsiType min, PsiManager manager, GlobalSearchScope scope) { if (type instanceof PsiDisjunctionType) type = ((PsiDisjunctionType) type).getLeastUpperBound(); // if initial type is not assignable to min type we don't take into consideration min type. if (min != null && !TypesUtil.isAssignable(min, type, manager, scope)) { min = null; } Map<String, PsiType> map = new LinkedHashMap<String, PsiType>(); final PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(type); if (unboxed != null) type = unboxed; final Set<PsiType> set = new LinkedHashSet<PsiType>(); set.add(type); while (!set.isEmpty()) { PsiType cur = set.iterator().next(); set.remove(cur); if (!map.containsValue(cur) && (min == null || TypesUtil.isAssignable(min, cur, manager, scope))) { if (isPartiallySubstituted(cur)) { LOG.assertTrue(cur instanceof PsiClassType); PsiClassType rawType = ((PsiClassType) cur).rawType(); map.put(rawType.getPresentableText(), rawType); } else { map.put(cur.getPresentableText(), cur); } for (PsiType superType : cur.getSuperTypes()) { if (!map.containsValue(superType)) { set.add(superType); } } } } return map; }
private void checkExpression(PsiExpression expression) { if (expression.getParent() instanceof PsiParenthesizedExpression) { return; } final PsiType expressionType = expression.getType(); if (expressionType == null) { return; } if (expressionType.getArrayDimensions() > 0) { // a horrible hack to get around what happens when you pass // an array to a vararg expression return; } if (TypeConversionUtil.isPrimitiveAndNotNull(expressionType)) { return; } final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(expressionType); if (unboxedType == null) { return; } final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false); if (expectedType == null) { return; } if (!TypeConversionUtil.isPrimitiveAndNotNull(expectedType)) { return; } if (!expectedType.isAssignableFrom(unboxedType)) { return; } registerError(expression, expression); }
public Object evaluate(EvaluationContextImpl context) throws EvaluateException { final Object result = myOperand.evaluate(context); if (result == null || result instanceof ObjectReference) { return result; } if (result instanceof PrimitiveValue) { PrimitiveValue primitiveValue = (PrimitiveValue) result; PsiPrimitiveType primitiveType = PsiJavaParserFacadeImpl.getPrimitiveType(primitiveValue.type().name()); if (primitiveType != null) { return convertToWrapper(context, primitiveValue, primitiveType.getBoxedTypeName()); } } throw new EvaluateException( "Cannot perform boxing conversion for a value of type " + ((Value) result).type().name()); }
private void checkExpression(@NotNull PsiExpression expression) { if (expression.getParent() instanceof PsiParenthesizedExpression) { return; } final PsiType expressionType = expression.getType(); if (expressionType == null || expressionType.equals(PsiType.VOID) || !TypeConversionUtil.isPrimitiveAndNotNull(expressionType)) { return; } final PsiPrimitiveType primitiveType = (PsiPrimitiveType) expressionType; final PsiClassType boxedType = primitiveType.getBoxedType(expression); if (boxedType == null) { return; } final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false); if (expectedType == null || ClassUtils.isPrimitive(expectedType)) { return; } if (!expectedType.isAssignableFrom(boxedType)) { // JLS 5.2 Assignment Conversion // check if a narrowing primitive conversion is applicable if (!(expectedType instanceof PsiClassType) || !PsiUtil.isConstantExpression(expression)) { return; } final PsiClassType classType = (PsiClassType) expectedType; final String className = classType.getCanonicalText(); if (!convertableBoxedClassNames.contains(className)) { return; } if (!PsiType.BYTE.equals(expressionType) && !PsiType.CHAR.equals(expressionType) && !PsiType.SHORT.equals(expressionType) && !PsiType.INT.equals(expressionType)) { return; } } if (ignoreAddedToCollection && isAddedToCollection(expression)) { return; } registerError(expression); }
public static String getTypeString( ExtractMethodInfoHelper helper, boolean forPresentation, String modifier) { PsiType type = helper.getOutputType(); final PsiPrimitiveType outUnboxed = PsiPrimitiveType.getUnboxedType(type); if (outUnboxed != null) type = outUnboxed; String typeText = forPresentation ? type.getPresentableText() : type.getCanonicalText(); String returnType = typeText == null || !helper.specifyType() ? "" : typeText; if (StringUtil.isEmptyOrSpaces(returnType) || "null".equals(returnType)) { if (modifier.length() == 0) { return "def "; } else { return ""; } } else { return returnType + " "; } }
@NotNull public static String getTypeString( @NotNull ExtractMethodInfoHelper helper, boolean forPresentation, @NotNull String modifier) { if (!helper.specifyType()) { return modifier.isEmpty() ? "def " : ""; } PsiType type = helper.getOutputType(); final PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(type); if (unboxed != null) type = unboxed; final String returnType = StringUtil.notNullize( forPresentation ? type.getPresentableText() : type.getCanonicalText()); if (StringUtil.isEmptyOrSpaces(returnType) || "null".equals(returnType)) { return modifier.isEmpty() ? "def " : ""; } else { return returnType + " "; } }
@Override public void visitTypeCastExpression(@NotNull PsiTypeCastExpression expression) { super.visitTypeCastExpression(expression); final PsiExpression operand = expression.getOperand(); if (operand == null) { return; } final PsiType operandType = operand.getType(); if (operandType == null) { return; } final PsiType type = expression.getType(); if (type == null) { return; } final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, true); if (expectedType == null) { return; } if (expectedType.equals(type)) { return; } final PsiClass resolved = PsiUtil.resolveClassInType(expectedType); if (resolved != null && !resolved.isPhysical()) { return; } if (expectedType.isAssignableFrom(operandType)) { // then it's redundant, and caught by the built-in exception return; } if (isTypeParameter(expectedType)) { return; } if (expectedType instanceof PsiArrayType) { final PsiArrayType arrayType = (PsiArrayType) expectedType; final PsiType componentType = arrayType.getDeepComponentType(); if (isTypeParameter(componentType)) { return; } } if (type instanceof PsiPrimitiveType || expectedType instanceof PsiPrimitiveType) { return; } if (PsiPrimitiveType.getUnboxedType(type) != null || PsiPrimitiveType.getUnboxedType(expectedType) != null) { return; } if (expectedType instanceof PsiClassType) { final PsiClassType expectedClassType = (PsiClassType) expectedType; final PsiClassType expectedRawType = expectedClassType.rawType(); if (type.equals(expectedRawType)) { return; } if (type instanceof PsiClassType) { final PsiClassType classType = (PsiClassType) type; final PsiClassType rawType = classType.rawType(); if (rawType.equals(expectedRawType)) { return; } } if (type instanceof PsiArrayType) { return; } } if (ignoreInMatchingInstanceof && InstanceOfUtils.hasAgreeingInstanceof(expression)) { return; } final PsiTypeElement castTypeElement = expression.getCastType(); if (castTypeElement == null) { return; } registerError(castTypeElement, expectedType); }
@Override public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiExpression expression = (PsiExpression) descriptor.getPsiElement(); final PsiType type = expression.getType(); if (type == null) { return; } final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(type); if (unboxedType == null) { return; } final String newExpressionText = buildNewExpressionText(expression, unboxedType); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiElementFactory factory = psiFacade.getElementFactory(); final PsiElement parent = expression.getParent(); final String expressionText = expression.getText(); if (parent instanceof PsiPrefixExpression && !unboxedType.equalsToText("boolean")) { final PsiPrefixExpression prefixExpression = (PsiPrefixExpression) parent; final IElementType tokenType = prefixExpression.getOperationTokenType(); if (JavaTokenType.PLUSPLUS.equals(tokenType)) { replaceExpression(prefixExpression, expressionText + '=' + newExpressionText + "+1"); } else if (JavaTokenType.MINUSMINUS.equals(tokenType)) { replaceExpression(prefixExpression, expressionText + '=' + newExpressionText + "-1"); } else { replaceExpression( prefixExpression, prefixExpression.getOperationSign().getText() + newExpressionText); } } else if (parent instanceof PsiPostfixExpression) { final PsiPostfixExpression postfixExpression = (PsiPostfixExpression) parent; final IElementType tokenType = postfixExpression.getOperationTokenType(); final PsiElement grandParent = postfixExpression.getParent(); if (grandParent instanceof PsiExpressionStatement) { if (JavaTokenType.PLUSPLUS.equals(tokenType)) { replaceExpression(postfixExpression, expressionText + '=' + newExpressionText + "+1"); } else if (JavaTokenType.MINUSMINUS.equals(tokenType)) { replaceExpression(postfixExpression, expressionText + '=' + newExpressionText + "-1"); } } else { final PsiElement element = postfixExpression.replace(postfixExpression.getOperand()); final PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class); if (statement == null) { return; } final PsiStatement newStatement; if (JavaTokenType.PLUSPLUS.equals(tokenType)) { newStatement = factory.createStatementFromText( expressionText + '=' + newExpressionText + "+1;", statement); } else { newStatement = factory.createStatementFromText( expressionText + '=' + newExpressionText + "-1;", statement); } final PsiElement greatGrandParent = statement.getParent(); greatGrandParent.addAfter(newStatement, statement); } } else if (parent instanceof PsiAssignmentExpression) { final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) parent; final PsiExpression lExpression = assignmentExpression.getLExpression(); if (expression.equals(lExpression)) { final PsiJavaToken operationSign = assignmentExpression.getOperationSign(); final String operationSignText = operationSign.getText(); final char sign = operationSignText.charAt(0); final PsiExpression rExpression = assignmentExpression.getRExpression(); if (rExpression == null) { return; } final String text = lExpression.getText() + '=' + newExpressionText + sign + rExpression.getText(); final PsiExpression newExpression = factory.createExpressionFromText(text, assignmentExpression); assignmentExpression.replace(newExpression); } else { replaceExpression(expression, newExpressionText); } } else { replaceExpression(expression, newExpressionText); } }