当前位置: 首页>>代码示例>>Java>>正文


Java HighlightControlFlowUtil类代码示例

本文整理汇总了Java中com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil的典型用法代码示例。如果您正苦于以下问题:Java HighlightControlFlowUtil类的具体用法?Java HighlightControlFlowUtil怎么用?Java HighlightControlFlowUtil使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


HighlightControlFlowUtil类属于com.intellij.codeInsight.daemon.impl.analysis包,在下文中一共展示了HighlightControlFlowUtil类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: checkNotNullFieldsInitialized

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
private static void checkNotNullFieldsInitialized(PsiField field,
                                                  Annotated annotated,
                                                  NullableNotNullManager manager, @NotNull ProblemsHolder holder) {
  if (annotated.isDeclaredNotNull && !HighlightControlFlowUtil.isFieldInitializedAfterObjectConstruction(field)) {
    final PsiAnnotation annotation = AnnotationUtil.findAnnotation(field, manager.getNotNulls());
    if (annotation != null) {
      holder.registerProblem(annotation.isPhysical() ? annotation : field.getNameIdentifier(),
                             "Not-null fields must be initialized",
                             ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
    }
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:13,代码来源:NullableStuffInspectionBase.java

示例2: getQuickFixType

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
private static int getQuickFixType(@NotNull PsiVariable variable) {
  PsiElement outerCodeBlock = PsiUtil.getVariableCodeBlock(variable, null);
  if (outerCodeBlock == null) return -1;
  List<PsiReferenceExpression> outerReferences = new ArrayList<PsiReferenceExpression>();
  collectReferences(outerCodeBlock, variable, outerReferences);

  int type = MAKE_FINAL;
  for (PsiReferenceExpression expression : outerReferences) {
    // if it happens that variable referenced from another inner class, make sure it can be make final from there
    PsiElement innerScope = HighlightControlFlowUtil.getInnerClassVariableReferencedFrom(variable, expression);

    if (innerScope != null) {
      int thisType = MAKE_FINAL;
      if (writtenInside(variable, innerScope)) {
        // cannot make parameter array
        if (variable instanceof PsiParameter) return -1;
        thisType = MAKE_ARRAY;
      }
      if (thisType == MAKE_FINAL
          && !canBeFinal(variable, outerReferences)) {
        thisType = COPY_TO_FINAL;
      }
      type = Math.max(type, thisType);
    }
  }
  return type;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:28,代码来源:VariableAccessFromInnerClassFix.java

示例3: canBeFinal

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
private static boolean canBeFinal(@NotNull PsiVariable variable, @NotNull List<PsiReferenceExpression> references) {
  // if there is at least one assignment to this variable, it cannot be final
  Map<PsiElement, Collection<PsiReferenceExpression>> uninitializedVarProblems = new THashMap<PsiElement, Collection<PsiReferenceExpression>>();
  Map<PsiElement, Collection<ControlFlowUtil.VariableInfo>> finalVarProblems = new THashMap<PsiElement, Collection<ControlFlowUtil.VariableInfo>>();
  for (PsiReferenceExpression expression : references) {
    if (ControlFlowUtil.isVariableAssignedInLoop(expression, variable)) return false;
    HighlightInfo highlightInfo = HighlightControlFlowUtil.checkVariableInitializedBeforeUsage(expression, variable, uninitializedVarProblems,
                                                                                               variable.getContainingFile());
    if (highlightInfo != null) return false;
    highlightInfo = HighlightControlFlowUtil.checkFinalVariableMightAlreadyHaveBeenAssignedTo(variable, expression, finalVarProblems);
    if (highlightInfo != null) return false;
    if (variable instanceof PsiParameter && PsiUtil.isAccessedForWriting(expression)) return false;
  }
  return true;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:16,代码来源:VariableAccessFromInnerClassFix.java

示例4: makeFinalIfNeeded

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
public static void makeFinalIfNeeded(@NotNull InsertionContext context, @NotNull PsiVariable variable) {
  PsiElement place = context.getFile().findElementAt(context.getTailOffset() - 1);
  if (!Registry.is("java.completion.make.outer.variables.final") ||
      place == null || PsiUtil.isLanguageLevel8OrHigher(place) || JspPsiUtil.isInJspFile(place)) {
    return;
  }

  if (HighlightControlFlowUtil.getInnerClassVariableReferencedFrom(variable, place) != null &&
      !HighlightControlFlowUtil.isReassigned(variable, new HashMap<PsiElement, Collection<ControlFlowUtil.VariableInfo>>())) {
    PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, true);
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:13,代码来源:VariableLookupItem.java

示例5: visitReferenceExpression

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
  super.visitReferenceExpression(expression);
  if (expression.getQualifierExpression() != null) {
    return;
  }
  final PsiType type = expression.getType();
  if (SerializationUtils.isProbablySerializable(type)) {
    return;
  }
  final PsiElement target = expression.resolve();
  if (!(target instanceof PsiLocalVariable) && !(target instanceof PsiParameter)) {
    return;
  }
  final PsiVariable variable = (PsiVariable)target;
  if (!variable.hasModifierProperty(PsiModifier.FINAL)) {
    if (!PsiUtil.isLanguageLevel8OrHigher(variable) ||
        !HighlightControlFlowUtil.isEffectivelyFinal(variable, myClassOrLambda, expression)) {
      // don't warn on uncompilable code.
      return;
    }
  }
  if (PsiTreeUtil.isAncestor(myClassOrLambda, variable, true)) {
    return;
  }
  registerError(expression, myClassOrLambda, type);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:28,代码来源:SerializableStoresNonSerializableInspection.java

示例6: getQuickFixType

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
private static int getQuickFixType(PsiVariable variable) {
  PsiElement outerCodeBlock = PsiUtil.getVariableCodeBlock(variable, null);
  if (outerCodeBlock == null) return -1;
  List<PsiReferenceExpression> outerReferences = new ArrayList<PsiReferenceExpression>();
  collectReferences(outerCodeBlock, variable, outerReferences);

  int type = MAKE_FINAL;
  for (PsiReferenceExpression expression : outerReferences) {
    // if it happens that variable referenced from another inner class, make sure it can be make final from there
    PsiClass innerClass = HighlightControlFlowUtil.getInnerClassVariableReferencedFrom(variable, expression);

    if (innerClass != null) {
      int thisType = MAKE_FINAL;
      if (writtenInside(variable, innerClass)) {
        // cannot make parameter array
        if (variable instanceof PsiParameter) return -1;
        thisType = MAKE_ARRAY;
      }
      if (thisType == MAKE_FINAL
          && !canBeFinal(variable, outerReferences)) {
        thisType = COPY_TO_FINAL;
      }
      type = Math.max(type, thisType);
    }
  }
  return type;
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:28,代码来源:VariableAccessFromInnerClassFix.java

示例7: canBeFinal

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
private static boolean canBeFinal(PsiVariable variable, List<PsiReferenceExpression> references) {
  // if there is at least one assignment to this variable, it cannot be final
  Map<PsiElement, Collection<PsiReferenceExpression>> uninitializedVarProblems = new THashMap<PsiElement, Collection<PsiReferenceExpression>>();
  Map<PsiElement, Collection<ControlFlowUtil.VariableInfo>> finalVarProblems = new THashMap<PsiElement, Collection<ControlFlowUtil.VariableInfo>>();
  for (PsiReferenceExpression expression : references) {
    if (ControlFlowUtil.isVariableAssignedInLoop(expression, variable)) return false;
    HighlightInfo highlightInfo = HighlightControlFlowUtil.checkVariableInitializedBeforeUsage(expression, variable, uninitializedVarProblems,
                                                                                               variable.getContainingFile());
    if (highlightInfo != null) return false;
    highlightInfo = HighlightControlFlowUtil.checkFinalVariableMightAlreadyHaveBeenAssignedTo(variable, expression, finalVarProblems);
    if (highlightInfo != null) return false;
    if (variable instanceof PsiParameter && PsiUtil.isAccessedForWriting(expression)) return false;
  }
  return true;
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:16,代码来源:VariableAccessFromInnerClassFix.java

示例8: checkNotNullFieldsInitialized

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
private static void checkNotNullFieldsInitialized(PsiField field, NullableNotNullManager manager, @NotNull ProblemsHolder holder)
{
	PsiAnnotation annotation = manager.getNotNullAnnotation(field, false);
	if(annotation == null || HighlightControlFlowUtil.isFieldInitializedAfterObjectConstruction(field))
	{
		return;
	}

	boolean byDefault = manager.isContainerAnnotation(annotation);
	PsiJavaCodeReferenceElement name = annotation.getNameReferenceElement();
	holder.registerProblem(annotation.isPhysical() && !byDefault ? annotation : field.getNameIdentifier(), (byDefault && name != null ? "@" + name.getReferenceName() : "Not-null") + " fields " +
			"must be initialized");
}
 
开发者ID:consulo,项目名称:consulo-java,代码行数:14,代码来源:NullableStuffInspectionBase.java

示例9: makeFinalIfNeeded

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
public static void makeFinalIfNeeded(@NotNull InsertionContext context, @NotNull PsiVariable variable)
{
	PsiElement place = context.getFile().findElementAt(context.getTailOffset() - 1);
	if(place == null || PsiUtil.isLanguageLevel8OrHigher(place))
	{
		return;
	}

	if(HighlightControlFlowUtil.getInnerClassVariableReferencedFrom(variable, place) != null && !HighlightControlFlowUtil.isReassigned(variable, new HashMap<>()))
	{
		PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, true);
	}
}
 
开发者ID:consulo,项目名称:consulo-java,代码行数:14,代码来源:VariableLookupItem.java

示例10: buildVisitor

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
  return new JavaElementVisitor() {
    @Override
    public void visitForeachStatement(PsiForeachStatement statement) {
      super.visitForeachStatement(statement);
      if (PsiUtil.getLanguageLevel(statement).isAtLeast(LanguageLevel.JDK_1_8)) {
        final PsiExpression iteratedValue = statement.getIteratedValue();
        final PsiStatement body = statement.getBody();
        if (iteratedValue != null && body != null) {
          final PsiType iteratedValueType = iteratedValue.getType();
          if (InheritanceUtil.isInheritor(iteratedValueType, CommonClassNames.JAVA_UTIL_COLLECTION)) {
            final PsiClass iteratorClass = PsiUtil.resolveClassInType(iteratedValueType);
            LOG.assertTrue(iteratorClass != null);
            try {
              final ControlFlow controlFlow = ControlFlowFactory.getInstance(holder.getProject())
                .getControlFlow(body, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance());
              int startOffset = controlFlow.getStartOffset(body);
              int endOffset = controlFlow.getEndOffset(body);
              final Collection<PsiStatement> exitPoints = ControlFlowUtil
                .findExitPointsAndStatements(controlFlow, startOffset, endOffset, new IntArrayList(), PsiContinueStatement.class,
                                             PsiBreakStatement.class, PsiReturnStatement.class, PsiThrowStatement.class);
              if (exitPoints.isEmpty()) {

                final List<PsiVariable> usedVariables = ControlFlowUtil.getUsedVariables(controlFlow, startOffset, endOffset);
                for (PsiVariable variable : usedVariables) {
                  if (!HighlightControlFlowUtil.isEffectivelyFinal(variable, body, null)) {
                    return;
                  }
                }

                if (ExceptionUtil.getThrownCheckedExceptions(new PsiElement[] {body}).isEmpty()) {
                  if (!(iteratedValueType instanceof PsiClassType && ((PsiClassType)iteratedValueType).isRaw()) && 
                      isCollectCall(body, statement.getIterationParameter())) {
                    holder.registerProblem(iteratedValue, "Can be replaced with collect call",
                                           ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithCollectCallFix());
                  } else if (!isTrivial(body, statement.getIterationParameter())) {
                    holder.registerProblem(iteratedValue, "Can be replaced with foreach call",
                                           ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithForeachCallFix());
                  }
                }
              }
            }
            catch (AnalysisCanceledException ignored) {
            }
          }
        }
      }
    }
  };
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:53,代码来源:StreamApiMigrationInspection.java

示例11: canBeDeclaredFinal

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
public static boolean canBeDeclaredFinal(@NotNull PsiVariable variable) {
  LOG.assertTrue(variable instanceof PsiLocalVariable || variable instanceof PsiParameter);
  final boolean isReassigned = HighlightControlFlowUtil
    .isReassigned(variable, new THashMap<PsiElement, Collection<ControlFlowUtil.VariableInfo>>());
  return !isReassigned;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:7,代码来源:RefactoringUtil.java

示例12: invoke

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
  LOG.assertTrue(myOutOfScopeVariable != null);
  PsiManager manager = file.getManager();
  myOutOfScopeVariable.normalizeDeclaration();
  PsiUtil.setModifierProperty(myOutOfScopeVariable, PsiModifier.FINAL, false);
  PsiElement commonParent = PsiTreeUtil.findCommonParent(myOutOfScopeVariable, myUnresolvedReference);
  LOG.assertTrue(commonParent != null);
  PsiElement child = myOutOfScopeVariable.getTextRange().getStartOffset() < myUnresolvedReference.getTextRange().getStartOffset() ? myOutOfScopeVariable
                     : myUnresolvedReference;

  while(child.getParent() != commonParent) child = child.getParent();
  PsiDeclarationStatement newDeclaration = (PsiDeclarationStatement)JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createStatementFromText("int i = 0", null);
  PsiVariable variable = (PsiVariable)newDeclaration.getDeclaredElements()[0].replace(myOutOfScopeVariable);
  if (variable.getInitializer() != null) {
    variable.getInitializer().delete();
  }

  while(!(child instanceof PsiStatement) || !(child.getParent() instanceof PsiCodeBlock)) {
    child = child.getParent();
    commonParent = commonParent.getParent();
  }
  LOG.assertTrue(commonParent != null);
  PsiDeclarationStatement added = (PsiDeclarationStatement)commonParent.addBefore(newDeclaration, child);
  PsiLocalVariable addedVar = (PsiLocalVariable)added.getDeclaredElements()[0];
  CodeStyleManager.getInstance(manager.getProject()).reformat(commonParent);

  //Leave initializer assignment
  PsiExpression initializer = myOutOfScopeVariable.getInitializer();
  if (initializer != null) {
    PsiExpressionStatement assignment = (PsiExpressionStatement)JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createStatementFromText(myOutOfScopeVariable
      .getName() + "= e;", null);
    ((PsiAssignmentExpression)assignment.getExpression()).getRExpression().replace(initializer);
    assignment = (PsiExpressionStatement)CodeStyleManager.getInstance(manager.getProject()).reformat(assignment);
    PsiDeclarationStatement declStatement = PsiTreeUtil.getParentOfType(myOutOfScopeVariable, PsiDeclarationStatement.class);
    LOG.assertTrue(declStatement != null);
    PsiElement parent = declStatement.getParent();
    if (parent instanceof PsiForStatement) {
      declStatement.replace(assignment);
    }
    else {
      parent.addAfter(assignment, declStatement);
    }
  }

  if (myOutOfScopeVariable.isValid()) {
    myOutOfScopeVariable.delete();
  }

  if (HighlightControlFlowUtil.checkVariableInitializedBeforeUsage(myUnresolvedReference, addedVar, new THashMap<PsiElement, Collection<PsiReferenceExpression>>(),file) != null) {
    initialize(addedVar);
  }

  DaemonCodeAnalyzer.getInstance(project).updateVisibleHighlighters(editor);
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:56,代码来源:BringVariableIntoScopeFix.java

示例13: canBeDeclaredFinal

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
public static boolean canBeDeclaredFinal(PsiVariable variable) {
  LOG.assertTrue(variable instanceof PsiLocalVariable || variable instanceof PsiParameter);
  final boolean isReassigned = HighlightControlFlowUtil
    .isReassigned(variable, new THashMap<PsiElement, Collection<ControlFlowUtil.VariableInfo>>());
  return !isReassigned;
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:7,代码来源:RefactoringUtil.java

示例14: invoke

import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; //导入依赖的package包/类
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
  LOG.assertTrue(myOutOfScopeVariable != null);
  PsiManager manager = file.getManager();
  myOutOfScopeVariable.normalizeDeclaration();
  PsiUtil.setModifierProperty(myOutOfScopeVariable, PsiModifier.FINAL, false);
  PsiElement commonParent = PsiTreeUtil.findCommonParent(myOutOfScopeVariable, myUnresolvedReference);
  LOG.assertTrue(commonParent != null);
  PsiElement child = myOutOfScopeVariable.getTextRange().getStartOffset() < myUnresolvedReference.getTextRange().getStartOffset() ? myOutOfScopeVariable
                                                                                                                                  : myUnresolvedReference;

  while(child.getParent() != commonParent) child = child.getParent();
  PsiDeclarationStatement newDeclaration = (PsiDeclarationStatement)JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createStatementFromText("int i = 0", null);
  PsiVariable variable = (PsiVariable)newDeclaration.getDeclaredElements()[0].replace(myOutOfScopeVariable);
  if (variable.getInitializer() != null) {
    variable.getInitializer().delete();
  }

  while(!(child instanceof PsiStatement) || !(child.getParent() instanceof PsiCodeBlock)) {
    child = child.getParent();
    commonParent = commonParent.getParent();
  }
  LOG.assertTrue(commonParent != null);
  PsiDeclarationStatement added = (PsiDeclarationStatement)commonParent.addBefore(newDeclaration, child);
  PsiLocalVariable addedVar = (PsiLocalVariable)added.getDeclaredElements()[0];
  CodeStyleManager.getInstance(manager.getProject()).reformat(commonParent);

  //Leave initializer assignment
  PsiExpression initializer = myOutOfScopeVariable.getInitializer();
  if (initializer != null) {
    PsiExpressionStatement assignment = (PsiExpressionStatement)JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createStatementFromText(myOutOfScopeVariable
                                                                                                                                                              .getName() + "= e;", null);
    ((PsiAssignmentExpression)assignment.getExpression()).getRExpression().replace(initializer);
    assignment = (PsiExpressionStatement)CodeStyleManager.getInstance(manager.getProject()).reformat(assignment);
    PsiDeclarationStatement declStatement = PsiTreeUtil.getParentOfType(myOutOfScopeVariable, PsiDeclarationStatement.class);
    LOG.assertTrue(declStatement != null);
    PsiElement parent = declStatement.getParent();
    if (parent instanceof PsiForStatement) {
      declStatement.replace(assignment);
    }
    else {
      parent.addAfter(assignment, declStatement);
    }
  }

  if (myOutOfScopeVariable.isValid()) {
    myOutOfScopeVariable.delete();
  }

  if (HighlightControlFlowUtil.checkVariableInitializedBeforeUsage(myUnresolvedReference, addedVar, new THashMap<PsiElement, Collection<PsiReferenceExpression>>(),file) != null) {
    initialize(addedVar);
  }

  DaemonCodeAnalyzer.getInstance(project).updateVisibleHighlighters(editor);
}
 
开发者ID:consulo,项目名称:consulo-java,代码行数:56,代码来源:BringVariableIntoScopeFix.java


注:本文中的com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。