Java 类com.intellij.psi.controlFlow.DefUseUtil 实例源码

项目:intellij-ce-playground    文件:InlineLocalHandler.java   
@Nullable
static PsiExpression getDefToInline(final PsiVariable local,
                                    final PsiElement refExpr,
                                    final PsiCodeBlock block) {
  if (refExpr != null) {
    PsiElement def;
    if (refExpr instanceof PsiReferenceExpression && PsiUtil.isAccessedForWriting((PsiExpression) refExpr)) {
      def = refExpr;
    }
    else {
      final PsiElement[] defs = DefUseUtil.getDefs(block, local, refExpr);
      if (defs.length == 1) {
        def = defs[0];
      }
      else {
        return null;
      }
    }

    if (def instanceof PsiReferenceExpression && def.getParent() instanceof PsiAssignmentExpression) {
      final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)def.getParent();
      if (assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) return null;
      final PsiExpression rExpr = assignmentExpression.getRExpression();
      if (rExpr != null) return rExpr;
    }
  }
  return local.getInitializer();
}
项目:intellij-ce-playground    文件:ListenerNavigateButton.java   
private static void addListenerRef(final DefaultActionGroup actionGroup, final String eventName, final PsiExpression listenerArg) {
  final PsiType type = listenerArg.getType();
  if (type instanceof PsiClassType) {
    PsiClass listenerClass = ((PsiClassType) type).resolve();
    if (listenerClass != null) {
      if (!isAbstractOrInterface(listenerClass)) {
        actionGroup.add(new MyNavigateAction(eventName + ": " + ClassPresentationUtil.getNameForClass(listenerClass, false),
                                             listenerClass));
        return;
      }
      else if (listenerArg instanceof PsiReferenceExpression) {
        final PsiElement psiElement = ((PsiReferenceExpression)listenerArg).resolve();
        if (psiElement instanceof PsiVariable) {
          PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(listenerArg, PsiCodeBlock.class);
          final PsiElement[] defs = DefUseUtil.getDefs(codeBlock, (PsiVariable)psiElement, listenerArg);
          if (defs.length == 1) {
            final PsiElement def = defs[0];
            if (def instanceof PsiVariable) {
              PsiVariable var = (PsiVariable) def;
              if (var.getInitializer() != listenerArg) {
                addListenerRef(actionGroup, eventName, var.getInitializer());
                return;
              }
            }
            else if (def.getParent() instanceof PsiAssignmentExpression) {
              final PsiAssignmentExpression assignmentExpr = (PsiAssignmentExpression)def.getParent();
              if (def.equals(assignmentExpr.getLExpression())) {
                addListenerRef(actionGroup, eventName, assignmentExpr.getRExpression());
                return;
              }
            }
          }
        }
      }
    }
  }
  actionGroup.add(new MyNavigateAction(eventName + ": " + listenerArg.getText(), listenerArg));
}
项目:tools-idea    文件:InlineLocalHandler.java   
@Nullable
private static PsiExpression getDefToInline(final PsiLocalVariable local,
                                            final PsiElement refExpr,
                                            final PsiCodeBlock block) {
  if (refExpr != null) {
    PsiElement def;
    if (refExpr instanceof PsiReferenceExpression && PsiUtil.isAccessedForWriting((PsiExpression) refExpr)) {
      def = refExpr;
    }
    else {
      final PsiElement[] defs = DefUseUtil.getDefs(block, local, refExpr);
      if (defs.length == 1) {
        def = defs[0];
      }
      else {
        return null;
      }
    }

    if (def instanceof PsiReferenceExpression && def.getParent() instanceof PsiAssignmentExpression) {
      final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)def.getParent();
      if (assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) return null;
      final PsiExpression rExpr = assignmentExpression.getRExpression();
      if (rExpr != null) return rExpr;
    }
  }
  return local.getInitializer();
}
项目:tools-idea    文件:ListenerNavigateButton.java   
private static void addListenerRef(final DefaultActionGroup actionGroup, final String eventName, final PsiExpression listenerArg) {
  final PsiType type = listenerArg.getType();
  if (type instanceof PsiClassType) {
    PsiClass listenerClass = ((PsiClassType) type).resolve();
    if (listenerClass != null) {
      if (!isAbstractOrInterface(listenerClass)) {
        actionGroup.add(new MyNavigateAction(eventName + ": " + ClassPresentationUtil.getNameForClass(listenerClass, false),
                                             listenerClass));
        return;
      }
      else if (listenerArg instanceof PsiReferenceExpression) {
        final PsiElement psiElement = ((PsiReferenceExpression)listenerArg).resolve();
        if (psiElement instanceof PsiVariable) {
          PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(listenerArg, PsiCodeBlock.class);
          final PsiElement[] defs = DefUseUtil.getDefs(codeBlock, (PsiVariable)psiElement, listenerArg);
          if (defs.length == 1) {
            final PsiElement def = defs[0];
            if (def instanceof PsiVariable) {
              PsiVariable var = (PsiVariable) def;
              if (var.getInitializer() != listenerArg) {
                addListenerRef(actionGroup, eventName, var.getInitializer());
                return;
              }
            }
            else if (def.getParent() instanceof PsiAssignmentExpression) {
              final PsiAssignmentExpression assignmentExpr = (PsiAssignmentExpression)def.getParent();
              if (def.equals(assignmentExpr.getLExpression())) {
                addListenerRef(actionGroup, eventName, assignmentExpr.getRExpression());
                return;
              }
            }
          }
        }
      }
    }
  }
  actionGroup.add(new MyNavigateAction(eventName + ": " + listenerArg.getText(), listenerArg));
}
项目:consulo-ui-designer    文件:ListenerNavigateButton.java   
private static void addListenerRef(final DefaultActionGroup actionGroup, final String eventName, final PsiExpression listenerArg) {
  final PsiType type = listenerArg.getType();
  if (type instanceof PsiClassType) {
    PsiClass listenerClass = ((PsiClassType) type).resolve();
    if (listenerClass != null) {
      if (!isAbstractOrInterface(listenerClass)) {
        actionGroup.add(new MyNavigateAction(eventName + ": " + ClassPresentationUtil.getNameForClass(listenerClass, false),
                                             listenerClass));
        return;
      }
      else if (listenerArg instanceof PsiReferenceExpression) {
        final PsiElement psiElement = ((PsiReferenceExpression)listenerArg).resolve();
        if (psiElement instanceof PsiVariable) {
          PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(listenerArg, PsiCodeBlock.class);
          final PsiElement[] defs = DefUseUtil.getDefs(codeBlock, (PsiVariable)psiElement, listenerArg);
          if (defs.length == 1) {
            final PsiElement def = defs[0];
            if (def instanceof PsiVariable) {
              PsiVariable var = (PsiVariable) def;
              if (var.getInitializer() != listenerArg) {
                addListenerRef(actionGroup, eventName, var.getInitializer());
                return;
              }
            }
            else if (def.getParent() instanceof PsiAssignmentExpression) {
              final PsiAssignmentExpression assignmentExpr = (PsiAssignmentExpression)def.getParent();
              if (def.equals(assignmentExpr.getLExpression())) {
                addListenerRef(actionGroup, eventName, assignmentExpr.getRExpression());
                return;
              }
            }
          }
        }
      }
    }
  }
  actionGroup.add(new MyNavigateAction(eventName + ": " + listenerArg.getText(), listenerArg));
}
项目:consulo-java    文件:InlineLocalHandler.java   
@Nullable
private static PsiExpression getDefToInline(final PsiLocalVariable local,
                                            final PsiElement refExpr,
                                            final PsiCodeBlock block) {
  if (refExpr != null) {
    PsiElement def;
    if (refExpr instanceof PsiReferenceExpression && PsiUtil.isAccessedForWriting((PsiExpression) refExpr)) {
      def = refExpr;
    }
    else {
      final PsiElement[] defs = DefUseUtil.getDefs(block, local, refExpr);
      if (defs.length == 1) {
        def = defs[0];
      }
      else {
        return null;
      }
    }

    if (def instanceof PsiReferenceExpression && def.getParent() instanceof PsiAssignmentExpression) {
      final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)def.getParent();
      if (assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) return null;
      final PsiExpression rExpr = assignmentExpression.getRExpression();
      if (rExpr != null) return rExpr;
    }
  }
  return local.getInitializer();
}