@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> result = new ArrayList<TextRange>(); result.addAll(expandToWholeLine(editorText, e.getTextRange(), false)); PsiTypeCastExpression expression = (PsiTypeCastExpression)e; PsiElement[] children = expression.getChildren(); PsiElement lParen = null; PsiElement rParen = null; for (PsiElement child : children) { if (child instanceof PsiJavaToken) { PsiJavaToken token = (PsiJavaToken)child; if (token.getTokenType() == JavaTokenType.LPARENTH) lParen = token; if (token.getTokenType() == JavaTokenType.RPARENTH) rParen = token; } } if (lParen != null && rParen != null) { result.addAll(expandToWholeLine(editorText, new TextRange(lParen.getTextRange().getStartOffset(), rParen.getTextRange().getEndOffset()), false)); } return result; }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { if(!FileModificationService.getInstance().prepareFileForWrite(file)) { return; } myConjuncts.remove(myConjunct); myConjuncts.add(0, myConjunct); final String intersectionTypeText = StringUtil.join(myConjuncts, new Function<PsiTypeElement, String>() { @Override public String fun(PsiTypeElement element) { return element.getText(); } }, " & "); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); final PsiTypeCastExpression fixedCast = (PsiTypeCastExpression) elementFactory.createExpressionFromText("(" + intersectionTypeText + ") a", myCastTypeElement); final PsiTypeElement fixedCastCastType = fixedCast.getCastType(); LOG.assertTrue(fixedCastCastType != null); final PsiElement flippedTypeElement = myCastTypeElement.replace(fixedCastCastType); CodeStyleManager.getInstance(project).reformat(flippedTypeElement); }
@Override public void doFix(@NotNull Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement element = descriptor.getPsiElement(); if (!(element instanceof PsiAssignmentExpression)) { return; } final PsiAssignmentExpression expression = (PsiAssignmentExpression)element; final PsiExpression lhs = expression.getLExpression(); PsiExpression rhs = ParenthesesUtils.stripParentheses(expression.getRExpression()); if (rhs instanceof PsiTypeCastExpression) { final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)rhs; final PsiType castType = typeCastExpression.getType(); if (castType == null || !castType.equals(lhs.getType())) { return; } rhs = ParenthesesUtils.stripParentheses(typeCastExpression.getOperand()); } if (!(rhs instanceof PsiPolyadicExpression)) { return; } final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)rhs; final String newExpression = calculateReplacementExpression(lhs, polyadicExpression); replaceExpression(expression, newExpression); }
public PsiTypeCastExpressionPattern withOperand(final ElementPattern<? extends PsiExpression> operand) { return with(new PatternCondition<PsiTypeCastExpression>("withOperand") { @Override public boolean accepts(@NotNull PsiTypeCastExpression psiTypeCastExpression, ProcessingContext context) { return operand.accepts(psiTypeCastExpression.getOperand(), context); } }); }
public void testIntersectionTypeWithSameBaseInterfaceInConjuncts() throws Exception { String filePath = BASE_PATH + "/" + getTestName(false) + ".java"; configureByFile(filePath); final PsiTypeCastExpression castExpression = PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiTypeCastExpression.class); assertNotNull(castExpression); final PsiTypeElement castTypeElement = castExpression.getCastType(); assertNotNull(castTypeElement); final PsiType type = castTypeElement.getType(); final String errorMessage = LambdaHighlightingUtil.checkInterfaceFunctional(type); assertEquals(null, errorMessage); }
@Override public boolean tryInlineCall(@NotNull CFGBuilder builder, @NotNull PsiMethodCallExpression call) { PsiMethod method = call.resolveMethod(); if(method == null || method != LambdaUtil.getFunctionalInterfaceMethod(method.getContainingClass())) { return false; } PsiTypeCastExpression typeCastExpression = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(call.getMethodExpression().getQualifierExpression()), PsiTypeCastExpression.class); if(typeCastExpression == null) { return false; } PsiLambdaExpression lambda = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(typeCastExpression.getOperand()), PsiLambdaExpression.class); if(lambda == null || lambda.getBody() == null) { return false; } if(method.isVarArgs()) { return false; // TODO: support varargs } PsiExpression[] args = call.getArgumentList().getExpressions(); PsiParameter[] parameters = lambda.getParameterList().getParameters(); if(args.length != parameters.length) { return false; } EntryStream.zip(args, parameters).forKeyValue((arg, parameter) -> builder.pushVariable(parameter).pushExpression(arg).boxUnbox(arg, parameter.getType()).assign().pop()); builder.inlineLambda(lambda, Nullness.UNKNOWN); return true; }
@Nullable private Boolean acceptInterfaceError( @NotNull HighlightInfo hi, PsiElement firstElem, PsiElement elem ) { if( elem instanceof PsiTypeCastExpression ) { PsiTypeElement castType = ((PsiTypeCastExpression)elem).getCastType(); if( isStructuralType( castType ) ) { // if( TypeUtil.isStructurallyAssignable( castType.getType(), ((PsiTypeCastExpression)elem).getType(), false ) ) // { // ignore incompatible cast type involving structure return false; // } } } else if( firstElem instanceof PsiIdentifier ) { PsiTypeElement lhsType = findTypeElement( firstElem ); if( isStructuralType( lhsType ) ) { PsiType initType = findInitializerType( firstElem ); if( initType != null ) { // if( TypeUtil.isStructurallyAssignable( lhsType.getType(), initType, false ) ) // { // ignore incompatible type in assignment involving structure return false; // } } } } else if( hi.getDescription().contains( "cannot be applied to" ) ) { PsiMethodCallExpression methodCall = findMethodCall( firstElem ); if( methodCall != null ) { PsiMethod psiMethod = methodCall.resolveMethod(); if( psiMethod != null ) { PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); PsiType[] argTypes = methodCall.getArgumentList().getExpressionTypes(); for( int i = 0; i < parameters.length; i++ ) { PsiParameter param = parameters[i]; if( argTypes.length <= i ) { return true; } if( !isStructuralType( param.getTypeElement() ) ) { if( !param.getType().isAssignableFrom( argTypes[i] ) ) { return true; } } // else // { // boolean nominal = false;//typeExtensionNominallyExtends( methodCall.getArgumentList().getExpressionTypes()[i], param.getTypeElement() ); // if( !TypeUtil.isStructurallyAssignable( param.getType(), methodCall.getArgumentList().getExpressionTypes()[i], !nominal ) ) // { // return true; // } // } } return true; } } } return null; }
@Override public void visitTypeCastExpression(PsiTypeCastExpression expression) { mVisitor.report("PsiTypeCastExpression", expression.getText(), expression); super.visitExpression(expression); }
PsiTypeCastExpressionPattern() { super(PsiTypeCastExpression.class); }
public TypeCastInstruction(PsiTypeCastExpression castExpression, PsiExpression casted, PsiType castTo) { myCastExpression = castExpression; myCasted = casted; myCastTo = castTo; }
public PsiTypeCastExpression getCastExpression() { return myCastExpression; }
@Override public boolean canSelect(PsiElement e) { return e instanceof PsiTypeCastExpression; }
@NotNull protected UsageInfo[] findUsages() { final SystemBuilder systemBuilder = new SystemBuilder(myProject, mySettings); final ReductionSystem commonSystem = systemBuilder.build(myElements); myResult = new Result(commonSystem); final ReductionSystem[] systems = commonSystem.isolate(); for (final ReductionSystem system : systems) { if (system != null) { final ResolverTree tree = new ResolverTree(system); tree.resolve(); final Binding solution = tree.getBestSolution(); if (solution != null) { myResult.incorporateSolution(solution); } } } final Set<PsiElement> changedItems = myResult.getCookedElements(); final UsageInfo[] usages = new UsageInfo[changedItems.size()]; int i = 0; for (final PsiElement element : changedItems) { if (!(element instanceof PsiTypeCastExpression)) { usages[i++] = new UsageInfo(element) { public String getTooltipText() { return myResult.getCookedType(element).getCanonicalText(); } }; } else { usages[i++] = new UsageInfo(element); } } return usages; }
@Override protected void createForLoopDeclaration(PsiForeachStatement statement, PsiExpression iteratedValue, boolean isArray, String iteratedValueText, StringBuilder newStatement, final String indexText) { final String lengthText; if (isArray) { lengthText = createVariableName(iteratedValueText + "Length", PsiType.INT, statement); } else { lengthText = createVariableName(iteratedValueText + "Size", PsiType.INT, statement); } newStatement.append("for(int "); newStatement.append(indexText); newStatement.append(" = 0, "); newStatement.append(lengthText); newStatement.append(" = "); if (iteratedValue instanceof PsiTypeCastExpression) { newStatement.append('('); newStatement.append(iteratedValueText); newStatement.append(')'); } else { newStatement.append(iteratedValueText); } if (isArray) { newStatement.append(".length;"); } else { newStatement.append(".size();"); } newStatement.append(indexText); newStatement.append('<'); newStatement.append(lengthText); newStatement.append(';'); newStatement.append(indexText); newStatement.append("++)"); newStatement.append("{ "); }
@Override public Object[] get(PsiElement context, CompletionContext completionContext){ final PsiTypeCastExpression cast = PsiTreeUtil.getContextOfType(context, PsiTypeCastExpression.class, true); return new Object[]{cast.getCastType().getType()}; }
@NotNull protected UsageInfo[] findUsages() { final SystemBuilder systemBuilder = new SystemBuilder(myProject, mySettings); final ReductionSystem commonSystem = systemBuilder.build(myElements); myResult = new Result(commonSystem); final ReductionSystem[] systems = commonSystem.isolate(); for (final ReductionSystem system : systems) { if (system != null) { final ResolverTree tree = new ResolverTree(system); tree.resolve(); final Binding solution = tree.getBestSolution(); if (solution != null) { myResult.incorporateSolution(solution); } } } final HashSet<PsiElement> changedItems = myResult.getCookedElements(); final UsageInfo[] usages = new UsageInfo[changedItems.size()]; int i = 0; for (final PsiElement element : changedItems) { if (!(element instanceof PsiTypeCastExpression)) { usages[i++] = new UsageInfo(element) { public String getTooltipText() { return myResult.getCookedType(element).getCanonicalText(); } }; } else { usages[i++] = new UsageInfo(element); } } return usages; }
@Override public void visitAssignmentExpression(@NotNull PsiAssignmentExpression assignment) { super.visitAssignmentExpression(assignment); final IElementType assignmentTokenType = assignment.getOperationTokenType(); if (!assignmentTokenType.equals(JavaTokenType.EQ)) { return; } final PsiExpression lhs = assignment.getLExpression(); PsiExpression rhs = ParenthesesUtils.stripParentheses(assignment.getRExpression()); if (rhs instanceof PsiTypeCastExpression) { final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)rhs; final PsiType castType = typeCastExpression.getType(); if (castType == null || !castType.equals(lhs.getType())) { return; } rhs = ParenthesesUtils.stripParentheses(typeCastExpression.getOperand()); } if (!(rhs instanceof PsiPolyadicExpression)) { return; } final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)rhs; final PsiExpression[] operands = polyadicExpression.getOperands(); if (operands.length < 2) { return; } if (operands.length > 2 && !ParenthesesUtils.isAssociativeOperation(polyadicExpression)) { return; } for (PsiExpression operand : operands) { if (operand == null) { return; } } final IElementType expressionTokenType = polyadicExpression.getOperationTokenType(); if (JavaTokenType.EQEQ.equals(expressionTokenType) || JavaTokenType.NE.equals(expressionTokenType)) { return; } if (ignoreLazyOperators) { if (JavaTokenType.ANDAND.equals(expressionTokenType) || JavaTokenType.OROR.equals(expressionTokenType)) { return; } } if (ignoreObscureOperators) { if (JavaTokenType.XOR.equals(expressionTokenType) || JavaTokenType.PERC.equals(expressionTokenType)) { return; } } if (!EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(lhs, operands[0])) { return; } registerError(assignment, lhs, polyadicExpression); }