本文整理汇总了Java中com.siyeh.ig.psiutils.SideEffectChecker类的典型用法代码示例。如果您正苦于以下问题:Java SideEffectChecker类的具体用法?Java SideEffectChecker怎么用?Java SideEffectChecker使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SideEffectChecker类属于com.siyeh.ig.psiutils包,在下文中一共展示了SideEffectChecker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkSideEffects
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
public static boolean checkSideEffects(PsiExpression element, PsiVariable variableToIgnore, List<PsiElement> sideEffects) {
if (sideEffects == null || element == null) return false;
List<PsiElement> writes = new ArrayList<PsiElement>();
SideEffectChecker.checkSideEffects(element, writes);
if (variableToIgnore != null) {
for (int i = writes.size() - 1; i >= 0; i--) {
PsiElement write = writes.get(i);
if (!(write instanceof PsiAssignmentExpression)) continue;
PsiExpression lExpression = ((PsiAssignmentExpression)write).getLExpression();
if (lExpression instanceof PsiReference && ((PsiReference)lExpression).resolve() == variableToIgnore) {
writes.remove(i);
}
}
}
sideEffects.addAll(writes);
return !writes.isEmpty();
}
示例2: visitMethodCallExpression
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
if (!MethodCallUtils.isEqualsCall(expression)) {
return;
}
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (qualifier == null) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length != 1) {
return;
}
final PsiExpression argument = arguments[0];
if (!EquivalenceChecker.expressionsAreEquivalent(qualifier, argument) ||
SideEffectChecker.mayHaveSideEffects(qualifier)) {
return;
}
registerMethodCallError(expression);
}
示例3: invoke
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
PsiExpressionStatement statement = myPointer.getElement();
if(statement == null)
{
return;
}
PsiExpression expression = statement.getExpression();
List<PsiExpression> sideEffects = SideEffectChecker.extractSideEffectExpressions(expression);
PsiStatement[] statements = StatementExtractor.generateStatements(sideEffects, expression);
if(statements.length > 0)
{
BlockUtils.addBefore(statement, statements);
}
statement.delete();
}
示例4: canConvertToLambdaWithoutSideEffects
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
/**
* Checks whether method reference can be converted to lambda without significant semantics change
* (i.e. method reference qualifier has no side effects)
*
* @param methodReferenceExpression method reference to check
* @return true if method reference can be converted to lambda
*/
public static boolean canConvertToLambdaWithoutSideEffects(PsiMethodReferenceExpression methodReferenceExpression)
{
final PsiExpression qualifierExpression = methodReferenceExpression.getQualifierExpression();
if(qualifierExpression == null)
{
PsiElement resolved = methodReferenceExpression.resolve();
if(resolved == null)
{
return false;
}
PsiClass arrayClass = JavaPsiFacade.getInstance(methodReferenceExpression.getProject()).getElementFactory().getArrayClass(PsiUtil.getLanguageLevel(methodReferenceExpression));
return resolved == arrayClass;
}
return !SideEffectChecker.mayHaveSideEffects(qualifierExpression);
}
示例5: canBeSwitchExpression
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
private static boolean canBeSwitchExpression(PsiExpression expression, LanguageLevel languageLevel) {
if (expression == null || SideEffectChecker.mayHaveSideEffects(expression)) {
return false;
}
final PsiType type = expression.getType();
if (PsiType.CHAR.equals(type) || PsiType.BYTE.equals(type) || PsiType.SHORT.equals(type) || PsiType.INT.equals(type)) {
return true;
}
else if (type instanceof PsiClassType) {
if (type.equalsToText(CommonClassNames.JAVA_LANG_CHARACTER) || type.equalsToText(CommonClassNames.JAVA_LANG_BYTE) ||
type.equalsToText(CommonClassNames.JAVA_LANG_SHORT) || type.equalsToText(CommonClassNames.JAVA_LANG_INTEGER)) {
return true;
}
if (languageLevel.isAtLeast(LanguageLevel.JDK_1_5)) {
final PsiClassType classType = (PsiClassType)type;
final PsiClass aClass = classType.resolve();
if (aClass != null && aClass.isEnum()) {
return true;
}
}
if (languageLevel.isAtLeast(LanguageLevel.JDK_1_7) && type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
return true;
}
}
return false;
}
示例6: mayHaveSideEffects
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
private static boolean mayHaveSideEffects(PsiDeclarationStatement statement) {
for (PsiElement element : statement.getDeclaredElements()) {
if (element instanceof PsiVariable) {
PsiExpression initializer = ((PsiVariable)element).getInitializer();
if (initializer != null && SideEffectChecker.mayHaveSideEffects(initializer)) {
return true;
}
}
}
return false;
}
示例7: visitField
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
@Override
public void visitField(@NotNull PsiField field) {
if (field.hasModifierProperty(PsiModifier.STATIC)) {
return;
}
if (!field.hasModifierProperty(PsiModifier.FINAL)) {
return;
}
final PsiExpression initializer = field.getInitializer();
if (initializer == null) {
return;
}
if (SideEffectChecker.mayHaveSideEffects(initializer)) {
return;
}
final PsiType type = field.getType();
if (!ClassUtils.isImmutable(type)) {
return;
}
final PsiClass containingClass = field.getContainingClass();
if (containingClass != null
&& !containingClass.hasModifierProperty(PsiModifier.STATIC)
&& containingClass.getContainingClass() != null
&& !PsiUtil.isCompileTimeConstant(field)) {
// inner class cannot have static declarations
return;
}
if (containingClass instanceof PsiAnonymousClass && !PsiUtil.isCompileTimeConstant(field)) {
return;
}
if (!canBeStatic(initializer)) {
return;
}
registerFieldError(field);
}
示例8: visitBinaryExpression
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
@Override
public void visitBinaryExpression(PsiBinaryExpression expression) {
super.visitBinaryExpression(expression);
final IElementType tokenType = expression.getOperationTokenType();
if (!JavaTokenType.OROR.equals(tokenType)) {
return;
}
final PsiExpression lhs = ParenthesesUtils.stripParentheses(expression.getLOperand());
final PsiExpression rhs = ParenthesesUtils.stripParentheses(expression.getROperand());
if (!(lhs instanceof PsiBinaryExpression) || !(rhs instanceof PsiBinaryExpression)) {
return;
}
final PsiBinaryExpression lBinaryExpression = (PsiBinaryExpression)lhs;
final PsiBinaryExpression rBinaryExpression = (PsiBinaryExpression)rhs;
final IElementType lTokenType = lBinaryExpression.getOperationTokenType();
final IElementType rTokenType = rBinaryExpression.getOperationTokenType();
if (!JavaTokenType.ANDAND.equals(lTokenType) || !JavaTokenType.ANDAND.equals(rTokenType)) {
return;
}
final PsiExpression expression1 = ParenthesesUtils.stripParentheses(lBinaryExpression.getLOperand());
final PsiExpression expression2 = ParenthesesUtils.stripParentheses(rBinaryExpression.getLOperand());
if (expression1 == null || expression2 == null ||
ParenthesesUtils.stripParentheses(lBinaryExpression.getROperand()) == null ||
ParenthesesUtils.stripParentheses(rBinaryExpression.getROperand()) == null) {
return;
}
if (EquivalenceChecker.expressionsAreEquivalent(BoolUtils.getNegated(expression1), expression2) &&
!SideEffectChecker.mayHaveSideEffects(expression2)) {
registerError(expression);
}
else if (EquivalenceChecker.expressionsAreEquivalent(expression1, BoolUtils.getNegated(expression2)) &&
!SideEffectChecker.mayHaveSideEffects(expression1)) {
registerError(expression);
}
}
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:36,代码来源:BooleanExpressionMayBeConditionalInspection.java
示例9: satisfiedBy
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof PsiAssignmentExpression)) {
return false;
}
final PsiAssignmentExpression assignment = (PsiAssignmentExpression)element;
final IElementType tokenType = assignment.getOperationTokenType();
if (!JavaTokenType.EQ.equals(tokenType)) {
return false;
}
final PsiExpression rhs = assignment.getRExpression();
final PsiExpression expression = PsiUtil.deparenthesizeExpression(rhs);
if (!(expression instanceof PsiPolyadicExpression)) {
return false;
}
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
final PsiExpression[] operands = polyadicExpression.getOperands();
if (operands.length < 2) {
return false;
}
final IElementType rhsTokenType = polyadicExpression.getOperationTokenType();
if (JavaTokenType.OROR.equals(rhsTokenType) || JavaTokenType.ANDAND.equals(rhsTokenType) ||
JavaTokenType.EQEQ.equals(rhsTokenType) || JavaTokenType.NE.equals(rhsTokenType)) {
return false;
}
final PsiExpression lhs = assignment.getLExpression();
if (SideEffectChecker.mayHaveSideEffects(lhs)) {
return false;
}
if (!EquivalenceChecker.expressionsAreEquivalent(lhs, operands[0])) {
return false;
}
return !ErrorUtil.containsError(element);
}
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:34,代码来源:ReplaceableWithOperatorAssignmentPredicate.java
示例10: introduceQualifierAsLocalVariable
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
private static void introduceQualifierAsLocalVariable(Editor editor, PsiLambdaExpression lambdaExpression) {
if (lambdaExpression != null && lambdaExpression.isValid()) {
final PsiElement body = lambdaExpression.getBody();
PsiExpression methodCall = null;
if (body instanceof PsiCodeBlock) {
final PsiStatement[] statements = ((PsiCodeBlock)body).getStatements();
LOG.assertTrue(statements.length == 1);
final PsiStatement statement = statements[0];
if (statement instanceof PsiReturnStatement) {
methodCall = ((PsiReturnStatement)statement).getReturnValue();
}
else if (statement instanceof PsiExpressionStatement) {
methodCall = ((PsiExpressionStatement)statement).getExpression();
}
} else {
methodCall = (PsiExpression)body;
}
PsiExpression qualifierExpression = null;
if (methodCall instanceof PsiMethodCallExpression) {
qualifierExpression = ((PsiMethodCallExpression)methodCall).getMethodExpression().getQualifierExpression();
}
else if (methodCall instanceof PsiNewExpression) {
qualifierExpression = ((PsiNewExpression)methodCall).getQualifier();
}
if (qualifierExpression != null) {
final List<PsiElement> sideEffects = new ArrayList<PsiElement>();
SideEffectChecker.checkSideEffects(qualifierExpression, sideEffects);
if (!sideEffects.isEmpty()) {
//ensure introduced before lambda
qualifierExpression.putUserData(ElementToWorkOn.PARENT, lambdaExpression);
new IntroduceVariableHandler().invoke(qualifierExpression.getProject(), editor, qualifierExpression);
}
}
}
}
示例11: visitField
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
@Override
public void visitField(@NotNull PsiField field) {
if (field.hasModifierProperty(PsiModifier.STATIC)) {
return;
}
if (!field.hasModifierProperty(PsiModifier.FINAL)) {
return;
}
final PsiExpression initializer = field.getInitializer();
if (initializer == null) {
return;
}
if (SideEffectChecker.mayHaveSideEffects(initializer)) {
return;
}
if (!canBeStatic(initializer)) {
return;
}
final PsiType type = field.getType();
if (!ClassUtils.isImmutable(type)) {
return;
}
final PsiClass containingClass = field.getContainingClass();
if (containingClass != null
&& !containingClass.hasModifierProperty(PsiModifier.STATIC)
&& containingClass.getContainingClass() != null
&& !PsiUtil.isCompileTimeConstant(field)) {
// inner class cannot have static declarations
return;
}
registerFieldError(field);
}
示例12: DeleteSideEffectsAwareFix
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
public DeleteSideEffectsAwareFix(PsiExpressionStatement statement)
{
myPointer = SmartPointerManager.getInstance(statement.getProject()).createSmartPsiElementPointer(statement);
PsiExpression expression = statement.getExpression();
List<PsiExpression> sideEffects = SideEffectChecker.extractSideEffectExpressions(expression);
if(sideEffects.isEmpty())
{
myMessage = JavaQuickFixBundle.message("delete.element.fix.text");
}
else if(sideEffects.size() == 1 && sideEffects.get(0) == PsiUtil.skipParenthesizedExprDown(expression))
{
// "Remove unnecessary parentheses" action is already present which will do the same
myMessage = "";
}
else
{
PsiStatement[] statements = StatementExtractor.generateStatements(sideEffects, expression);
if(statements.length == 1)
{
if(statements[0] instanceof PsiIfStatement)
{
myMessage = JavaQuickFixBundle.message("extract.side.effects.convert.to.if");
}
else
{
myMessage = JavaQuickFixBundle.message("extract.side.effects.single");
}
}
else
{
myMessage = JavaQuickFixBundle.message("extract.side.effects.multiple");
}
}
}
示例13: removeSideEffectsFromLambdaBody
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
/**
* Works for expression lambdas/one statement code block lambdas to ensures equivalent method ref -> lambda transformation.
*/
public static void removeSideEffectsFromLambdaBody(Editor editor, PsiLambdaExpression lambdaExpression)
{
if(lambdaExpression != null && lambdaExpression.isValid())
{
final PsiElement body = lambdaExpression.getBody();
PsiExpression methodCall = LambdaUtil.extractSingleExpressionFromBody(body);
PsiExpression qualifierExpression = null;
if(methodCall instanceof PsiMethodCallExpression)
{
qualifierExpression = ((PsiMethodCallExpression) methodCall).getMethodExpression().getQualifierExpression();
}
else if(methodCall instanceof PsiNewExpression)
{
qualifierExpression = ((PsiNewExpression) methodCall).getQualifier();
}
if(qualifierExpression != null)
{
final List<PsiElement> sideEffects = new ArrayList<>();
SideEffectChecker.checkSideEffects(qualifierExpression, sideEffects);
if(!sideEffects.isEmpty())
{
if(ApplicationManager.getApplication().isUnitTestMode() || Messages.showYesNoDialog(lambdaExpression.getProject(), "There are possible side effects found in method reference " +
"qualifier." + "\nIntroduce local variable?", "Side Effects Detected", Messages.getQuestionIcon()) == Messages.YES)
{
//ensure introduced before lambda
qualifierExpression.putUserData(ElementToWorkOn.PARENT, lambdaExpression);
new IntroduceVariableHandler().invoke(qualifierExpression.getProject(), editor, qualifierExpression);
}
}
}
}
}
示例14: mayHaveSideEffects
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
public static boolean mayHaveSideEffects(@Nullable final PsiElement element) {
return element instanceof PsiExpression && SideEffectChecker.mayHaveSideEffects((PsiExpression)element);
}
示例15: expressionIsArrayCopy
import com.siyeh.ig.psiutils.SideEffectChecker; //导入依赖的package包/类
private static boolean expressionIsArrayCopy(
@Nullable PsiExpression expression,
@NotNull PsiVariable variable,
@Nullable PsiVariable variable2) {
final PsiExpression strippedExpression =
ParenthesesUtils.stripParentheses(expression);
if (strippedExpression == null) {
return false;
}
if (!(strippedExpression instanceof PsiAssignmentExpression)) {
return false;
}
final PsiAssignmentExpression assignment =
(PsiAssignmentExpression)strippedExpression;
final IElementType tokenType = assignment.getOperationTokenType();
if (!tokenType.equals(JavaTokenType.EQ)) {
return false;
}
final PsiExpression lhs = assignment.getLExpression();
if (SideEffectChecker.mayHaveSideEffects(lhs)) {
return false;
}
if (!ExpressionUtils.isOffsetArrayAccess(lhs, variable)) {
return false;
}
final PsiExpression rhs = assignment.getRExpression();
if (rhs == null) {
return false;
}
if (SideEffectChecker.mayHaveSideEffects(rhs)) {
return false;
}
if (!areExpressionsCopyable(lhs, rhs)) {
return false;
}
final PsiType type = lhs.getType();
if (type instanceof PsiPrimitiveType) {
final PsiExpression strippedLhs =
ParenthesesUtils.stripParentheses(lhs);
final PsiExpression strippedRhs =
ParenthesesUtils.stripParentheses(rhs);
if (!areExpressionsCopyable(strippedLhs, strippedRhs)) {
return false;
}
}
if (variable2 == null) {
return ExpressionUtils.isOffsetArrayAccess(rhs, variable);
}
else {
return VariableAccessUtils.evaluatesToVariable(rhs, variable2);
}
}