类com.intellij.psi.PsiVariable源码实例Demo

下面列出了怎么用com.intellij.psi.PsiVariable的API类实例代码及写法,或者点击链接到github查看源代码。

@Override
public Object getValueAt(Object o, int index) {
    if (o instanceof ASTSlice) {
        ASTSlice entry = (ASTSlice) o;
        PsiVariable variable = entry.getLocalVariableCriterion();
        if (index == 1) {
            return variable == null ? "" : variable.getName();
        }
        return "";
    } else if (o instanceof ExtractMethodCandidateGroup) {
        ExtractMethodCandidateGroup group = (ExtractMethodCandidateGroup) o;
        switch (index) {
            case 0:
                return group.toString();
            case 1:
                PsiVariable firstCandidate = group.getCandidates().iterator().next().getLocalVariableCriterion();
                return firstCandidate == null ? "" : firstCandidate.getName();
            default:
                return "";
        }
    }
    return "";
}
 
源代码2 项目: IntelliJDeodorant   文件: PDGObjectSliceUnion.java
private boolean duplicatedSliceNodeWithClassInstantiationHasDependenceOnRemovableNode() {
    Set<PDGNode> duplicatedNodes = new LinkedHashSet<>(sliceNodes);
    duplicatedNodes.retainAll(indispensableNodes);
    for (PDGNode duplicatedNode : duplicatedNodes) {
        if (duplicatedNode.containsClassInstanceCreation()) {
            Map<PsiVariable, PsiNewExpression> classInstantiations = duplicatedNode.getClassInstantiations();
            for (PsiVariable variableDeclaration : classInstantiations.keySet()) {
                for (GraphEdge edge : duplicatedNode.outgoingEdges) {
                    PDGDependence dependence = (PDGDependence) edge;
                    if (subgraph.edgeBelongsToBlockBasedRegion(dependence) && dependence != null) {
                        PDGNode dstPDGNode = (PDGNode) dependence.dst;
                        if (removableNodes.contains(dstPDGNode)) {
                            if (dstPDGNode.changesStateOfReference(variableDeclaration)
                                    || dstPDGNode.assignsReference(variableDeclaration)
                                    || dstPDGNode.accessesReference(variableDeclaration))
                                return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
源代码3 项目: IntelliJDeodorant   文件: PDGSliceUnion.java
private boolean duplicatedSliceNodeWithClassInstantiationHasDependenceOnRemovableNode() {
    Set<PDGNode> duplicatedNodes = new LinkedHashSet<>(sliceNodes);
    duplicatedNodes.retainAll(indispensableNodes);
    for (PDGNode duplicatedNode : duplicatedNodes) {
        if (duplicatedNode.containsClassInstanceCreation()) {
            Map<PsiVariable, PsiNewExpression> classInstantiations = duplicatedNode.getClassInstantiations();
            for (PsiVariable variableDeclaration : classInstantiations.keySet()) {
                for (GraphEdge edge : duplicatedNode.outgoingEdges) {
                    PDGDependence dependence = (PDGDependence) edge;
                    if (subgraph.edgeBelongsToBlockBasedRegion(dependence) && dependence != null) {
                        PDGNode dstPDGNode = (PDGNode) dependence.dst;
                        if (removableNodes.contains(dstPDGNode)) {
                            if (dstPDGNode.changesStateOfReference(variableDeclaration)
                                    || dstPDGNode.assignsReference(variableDeclaration)
                                    || dstPDGNode.accessesReference(variableDeclaration))
                                return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
源代码4 项目: litho   文件: RequiredPropAnnotator.java
/**
 * If element is a {@link PsiStatement} with {@code Component.create()} call, finds missing
 * required props for it.
 *
 * @param element element to verify
 * @param errorHandler handles a list of missing required props and reference to the {@code
 *     Component.create()} call in the statement
 * @param generatedClassResolver returns generated Litho class, or null if the provided method
 *     doesn't belong to any
 */
static void annotate(
    PsiElement element,
    BiConsumer<Collection<String>, PsiReferenceExpression> errorHandler,
    Function<PsiMethodCallExpression, PsiClass> generatedClassResolver) {
  if (element instanceof PsiDeclarationStatement) {
    Arrays.stream(((PsiDeclarationStatement) element).getDeclaredElements())
        .filter(PsiVariable.class::isInstance)
        .map(declaredVariable -> ((PsiVariable) declaredVariable).getInitializer())
        .forEach(
            expression -> handleIfMethodCall(expression, errorHandler, generatedClassResolver));
  } else if (element instanceof PsiExpressionStatement) {
    handleIfMethodCall(
        ((PsiExpressionStatement) element).getExpression(), errorHandler, generatedClassResolver);
  } else if (element instanceof PsiReturnStatement) {
    handleIfMethodCall(
        ((PsiReturnStatement) element).getReturnValue(), errorHandler, generatedClassResolver);
  }
}
 
@Nullable
protected final ProblemDescriptor[] verifyAnnotationDeclaredCorrectly( @NotNull PsiVariable psiVariable,
                                                                       @NotNull PsiAnnotation structureAnnotation,
                                                                       @NotNull InspectionManager manager )
{
    StructureAnnotationDeclarationValidationResult annotationCheck =
        validateStructureAnnotationDeclaration( psiVariable );
    switch( annotationCheck )
    {
    case invalidInjectionType:
        String message = message(
            "injections.structure.annotation.declared.correctly.error.invalid.injection.type",
            psiVariable.getType().getCanonicalText()
        );
        AbstractFix removeStructureAnnotationFix = createRemoveAnnotationFix( structureAnnotation );
        ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(
            structureAnnotation, message, removeStructureAnnotationFix, GENERIC_ERROR_OR_WARNING
        );
        return new ProblemDescriptor[]{ problemDescriptor };
    }

    return null;
}
 
@Override
public String renderBuildCode(@NotNull PsiVariable psiVariable, @NotNull String fieldName, @NotNull String builderVariable) {
  final PsiManager psiManager = psiVariable.getManager();
  final PsiType psiFieldType = psiVariable.getType();

  final PsiType rowKeyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 0);
  final PsiType columnKeyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 1);
  final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, COM_GOOGLE_COMMON_COLLECT_TABLE, 2);

  return MessageFormat.format(
    "{4}<{1}, {2}, {3}> {0} = " +
      "{5}.{0} == null ? " +
      "{4}.<{1}, {2}, {3}>of() : " +
      "{5}.{0}.build();\n",
    fieldName, rowKeyType.getCanonicalText(false), columnKeyType.getCanonicalText(false),
    valueType.getCanonicalText(false), collectionQualifiedName, builderVariable);
}
 
public boolean instanceOf(PsiStatement statement) {
    if (statement instanceof PsiDeclarationStatement) {
        PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) statement;
        PsiElement[] declaredElements = declarationStatement.getDeclaredElements();
        for (PsiElement element : declaredElements) {
            if (element instanceof PsiVariable) {
                return true;
            }
        }
    }
    return false;
}
 
源代码8 项目: IntelliJDeodorant   文件: ReachingAliasSet.java
boolean containsAlias(AbstractVariable variable) {
    for (LinkedHashSet<PsiVariable> aliasSet : aliasSets) {
        for (PsiVariable alias : aliasSet) {
            if (alias.equals(variable.getOrigin()))
                return true;
        }
    }
    return false;
}
 
源代码9 项目: IntelliJDeodorant   文件: ReachingAliasSet.java
ReachingAliasSet copy() {
    List<LinkedHashSet<PsiVariable>> aliasSetsCopy = new ArrayList<>();
    for (LinkedHashSet<PsiVariable> aliasSet : aliasSets) {
        LinkedHashSet<PsiVariable> aliasSetCopy = new LinkedHashSet<>(aliasSet);
        aliasSetsCopy.add(aliasSetCopy);
    }
    return new ReachingAliasSet(aliasSetsCopy);
}
 
@Override
protected void processAnnotation(PsiModifierListOwner psiElement, PsiAnnotation configPropertyAnnotation,
								 String annotationName, SearchContext context) {
	if (psiElement instanceof PsiField || psiElement instanceof PsiMethod || psiElement instanceof PsiParameter) {
		IPropertiesCollector collector = context.getCollector();
		String name = AnnotationUtils.getAnnotationMemberValue(configPropertyAnnotation,
				QuarkusConstants.CONFIG_PROPERTY_ANNOTATION_NAME);
		if (StringUtils.isNotEmpty(name)) {
			String propertyTypeName = "";
			if (psiElement instanceof PsiField) {
				propertyTypeName = PsiTypeUtils.getResolvedTypeName((PsiField) psiElement);
			} else if (psiElement instanceof PsiMethod) {
				propertyTypeName = PsiTypeUtils.getResolvedResultTypeName((PsiMethod) psiElement);
			} else if (psiElement instanceof PsiVariable) {
				propertyTypeName = PsiTypeUtils.getResolvedTypeName((PsiVariable) psiElement);
			}
			PsiClass fieldClass = JavaPsiFacade.getInstance(psiElement.getProject()).findClass(propertyTypeName, GlobalSearchScope.allScope(psiElement.getProject()));

			String type = PsiTypeUtils.getPropertyType(fieldClass, propertyTypeName);
			String description = null;
			String sourceType = PsiTypeUtils.getSourceType(psiElement);
			String sourceField = null;
			String sourceMethod = null;
			if (psiElement instanceof PsiField || psiElement instanceof PsiMethod) {
				sourceField = PsiTypeUtils.getSourceField((PsiMember) psiElement);
			} else if (psiElement instanceof PsiParameter) {
				PsiMethod method = (PsiMethod) ((PsiParameter)psiElement).getDeclarationScope();
					sourceMethod = PsiTypeUtils.getSourceMethod(method);
			}
			String defaultValue = AnnotationUtils.getAnnotationMemberValue(configPropertyAnnotation,
					QuarkusConstants.CONFIG_PROPERTY_ANNOTATION_DEFAULT_VALUE);
			String extensionName = null;

			super.updateHint(collector, fieldClass);

			addItemMetadata(collector, name, type, description, sourceType, sourceField, sourceMethod, defaultValue,
					extensionName, PsiTypeUtils.isBinary(psiElement));
		}
	}
}
 
/**
 * Return the resolved reference to a {@link PsiVariable} or {@link PsiMethod}
 * for the given element if it is a {@link PsiIdentifier}.
 *
 * @param element the element to resolve
 * @return an {@link Optional} representing the resolved reference or empty
 *         if reference could not be resolved.
 */
private Optional<PsiElement> resolvedIdentifier(PsiElement element) {
    if (element instanceof PsiIdentifier) {
        return Optional.ofNullable(element.getParent())
            .map(PsiElement::getReference)
            .map(PsiReference::resolve)
            .filter(resolved -> PsiVariable.class.isInstance(resolved) || PsiMethod.class.isInstance(resolved));
    }
    return Optional.empty();
}
 
/**
 * Returns the Camel route from a PsiElement
 *
 * @param element the element
 * @return the String route or null if there nothing can be found
 */
private String findRouteFromElement(PsiElement element) {
    XmlTag xml = PsiTreeUtil.getParentOfType(element, XmlTag.class);
    if (xml != null) {
        return ((XmlTagImpl) element.getParent()).getAttributeValue("uri");
    }


    if (element instanceof PsiIdentifier) {
        PsiIdentifier id = (PsiIdentifier) element;
        String text = id.getText();
        if (text != null) {
            return text;
        }
    }

    if (element instanceof PsiJavaToken) {
        return element.getText();
    }

    // Only variables can be resolved?
    Optional<PsiVariable> variable = resolvedIdentifier(element)
        .filter(PsiVariable.class::isInstance)
        .map(PsiVariable.class::cast);
    if (variable.isPresent()) {
        // Try to resolve variable and recursive search route
        return variable.map(PsiVariable::getInitializer)
            .map(this::findRouteFromElement)
            .orElse(null);
    }

    return null;
}
 
public void testStartRouteWithVariableIdentifier() {
    // caret is at start of rout in the test java file
    PsiFile psiFile = myFixture.configureByText("DummyTestData.java", CODE_VAR_URI);

    PsiVariable variable = myFixture.findElementByText("uri", PsiVariable.class);
    PsiElement identifier = myFixture.findUsages(variable).iterator().next().getElement();
    assertInstanceOf(identifier, PsiReferenceExpression.class);

    assertTrue(getCamelIdeaUtils().isCamelRouteStart(identifier));
}
 
public void testStartRouteWithConstantIdentifier() {
    // caret is at start of rout in the test java file
    PsiFile psiFile = myFixture.configureByText("DummyTestData.java", CODE_CONST_URI);

    PsiVariable variable = myFixture.findElementByText("URI", PsiVariable.class);
    PsiElement identifier = myFixture.findUsages(variable).iterator().next().getElement();
    assertInstanceOf(identifier, PsiReferenceExpression.class);

    assertTrue(getCamelIdeaUtils().isCamelRouteStart(identifier));
}
 
/**
 * Validates whether the variable has {@code @Service} annotation declared correctly.
 *
 * @param variable variable to check.
 * @return Look at {@link ServiceAnnotationDeclarationValidationResult}.
 * @since 0.1
 */
@NotNull
public static ServiceAnnotationDeclarationValidationResult isValidServiceAnnotationDeclaration(
    @NotNull PsiVariable variable )
{
    PsiAnnotation serviceAnnotation = getServiceAnnotation( variable );
    if( serviceAnnotation == null )
    {
        return invalidServiceAnnotationNotDeclared;
    }

    PsiModifierList modifierList = variable.getModifierList();
    if( modifierList != null )
    {
        if( modifierList.hasModifierProperty( STATIC ) )
        {
            return invalidDeclaredOnStaticVariable;
        }
    }

    // Can't be type that is injected by @Structure
    if( isInjecteableByStructureAnnotation( variable ) )
    {
        return invalidTypeIsInjectedViaStructureAnnotation;
    }

    return valid;
}
 
/**
 * @param variable variable to check.
 * @return Annotation to check.
 * @see #getAnnotationToCheckQualifiedName()
 * @since 0.1
 */
@Nullable
protected final PsiAnnotation getAnnotationToCheck( @NotNull PsiVariable variable )
{
    String annotationQualifiedName = getAnnotationToCheckQualifiedName();
    return findAnnotation( variable, annotationQualifiedName );
}
 
源代码17 项目: lombok-intellij-plugin   文件: AccessorsInfo.java
@NotNull
public static AccessorsInfo build(@NotNull PsiVariable psiVariable, @Nullable PsiClass containingClass) {
  final PsiAnnotation accessorsFieldAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiVariable, Accessors.class);
  if (null != accessorsFieldAnnotation) {
    return buildFromAnnotation(accessorsFieldAnnotation, containingClass);
  } else {
    return build(containingClass);
  }
}
 
@NotNull
public static BuilderElementHandler getHandlerFor(@NotNull PsiVariable psiVariable, @Nullable PsiAnnotation singularAnnotation) {
  if (null == singularAnnotation) {
    return new NonSingularHandler();
  }

  final PsiType psiType = psiVariable.getType();
  final String qualifiedName = PsiTypeUtil.getQualifiedName(psiType);
  if (!isInvalidSingularType(qualifiedName)) {
    if (COLLECTION_TYPES.contains(qualifiedName)) {
      return new SingularCollectionHandler(qualifiedName);
    }
    if (MAP_TYPES.contains(qualifiedName)) {
      return new SingularMapHandler(qualifiedName);
    }
    if (GUAVA_COLLECTION_TYPES.contains(qualifiedName)) {
      return new SingularGuavaCollectionHandler(qualifiedName, qualifiedName.contains("Sorted"));
    }
    if (GUAVA_MAP_TYPES.contains(qualifiedName)) {
      return new SingularGuavaMapHandler(qualifiedName, qualifiedName.contains("Sorted"));
    }
    if (GUAVA_TABLE_TYPES.contains(qualifiedName)) {
      return new SingularGuavaTableHandler(qualifiedName, false);
    }
  }
  return new EmptyBuilderElementHandler();
}
 
@Override
public String renderBuildCode(@NotNull PsiVariable psiVariable, @NotNull String fieldName, @NotNull String builderVariable) {
  final PsiManager psiManager = psiVariable.getManager();
  final PsiType psiFieldType = psiVariable.getType();
  final PsiType keyType = getKeyType(psiManager, psiFieldType);
  final PsiType valueType = getValueType(psiManager, psiFieldType);

  final String selectedFormat;
  if (collectionQualifiedName.equals(SingularCollectionClassNames.JAVA_UTIL_SORTED_MAP)) {
    selectedFormat = "java.util.SortedMap<{1}, {2}> {0} = new java.util.TreeMap<{1}, {2}>();\n" +
      "      if ({3}.{0}$key != null) for (int $i = 0; $i < ({3}.{0}$key == null ? 0 : {3}.{0}$key.size()); $i++) {0}.put({3}.{0}$key.get($i), ({2}){3}.{0}$value.get($i));\n" +
      "      {0} = java.util.Collections.unmodifiableSortedMap({0});\n";
  } else if (collectionQualifiedName.equals(SingularCollectionClassNames.JAVA_UTIL_NAVIGABLE_MAP)) {
    selectedFormat = "java.util.NavigableMap<{1}, {2}> {0} = new java.util.TreeMap<{1}, {2}>();\n" +
      "      if ({3}.{0}$key != null) for (int $i = 0; $i < ({3}.{0}$key == null ? 0 : {3}.{0}$key.size()); $i++) {0}.put({3}.{0}$key.get($i), ({2}){3}.{0}$value.get($i));\n" +
      "      {0} = java.util.Collections.unmodifiableNavigableMap({0});\n";
  } else {
    selectedFormat = "java.util.Map<{1}, {2}> {0};\n" +
      "  switch ({3}.{0}$key == null ? 0 : {3}.{0}$key.size()) '{'\n" +
      "    case 0:\n" +
      "      {0} = java.util.Collections.emptyMap();\n" +
      "      break;\n" +
      "    case 1:\n" +
      "      {0} = java.util.Collections.singletonMap({3}.{0}$key.get(0), {3}.{0}$value.get(0));\n" +
      "      break;\n" +
      "    default:\n" +
      "      {0} = new java.util.LinkedHashMap<{1}, {2}>({3}.{0}$key.size() < 1073741824 ? 1 + {3}.{0}$key.size() + ({3}.{0}$key.size() - 3) / 3 : java.lang.Integer.MAX_VALUE);\n" +
      "      for (int $i = 0; $i < {3}.{0}$key.size(); $i++) {0}.put({3}.{0}$key.get($i), ({2}){3}.{0}$value.get($i));\n" +
      "      {0} = java.util.Collections.unmodifiableMap({0});\n" +
      "  '}'\n";
  }
  return MessageFormat.format(selectedFormat, fieldName, keyType.getCanonicalText(false),
    valueType.getCanonicalText(false), builderVariable);
}
 
@Override
public String renderBuildCode(@NotNull PsiVariable psiVariable, @NotNull String fieldName, @NotNull String builderVariable) {
  final PsiManager psiManager = psiVariable.getManager();
  final PsiType psiFieldType = psiVariable.getType();

  final PsiType elementType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager);
  return MessageFormat.format(
    "{2}<{1}> {0} = " +
      "{4}.{0} == null ? " +
      "{3}.<{1}>of() : " +
      "{4}.{0}.build();\n",
    fieldName, elementType.getCanonicalText(false), collectionQualifiedName, typeCollectionQualifiedName, builderVariable);
}
 
@Override
public String renderBuildCode(@NotNull PsiVariable psiVariable, @NotNull String fieldName, @NotNull String builderVariable) {
  final PsiManager psiManager = psiVariable.getManager();
  final PsiType psiFieldType = psiVariable.getType();

  final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
  final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

  return MessageFormat.format(
    "{3}<{1}, {2}> {0} = " +
      "{4}.{0} == null ? " +
      "{3}.<{1}, {2}>of() : " +
      "{4}.{0}.build();\n",
    fieldName, keyType.getCanonicalText(false), valueType.getCanonicalText(false), collectionQualifiedName, builderVariable);
}
 
源代码22 项目: dagger-intellij-plugin   文件: PsiConsultantImpl.java
public static PsiClass getClass(PsiElement psiElement) {
  if (psiElement instanceof PsiVariable) {
    PsiVariable variable = (PsiVariable) psiElement;
    return getClass(variable.getType());
  } else if (psiElement instanceof PsiMethod) {
    return ((PsiMethod) psiElement).getContainingClass();
  }

  return null;
}
 
源代码23 项目: dagger-intellij-plugin   文件: PsiConsultantImpl.java
private static PsiClassType getPsiClassType(PsiElement psiElement) {
  if (psiElement instanceof PsiVariable) {
    return (PsiClassType) ((PsiVariable) psiElement).getType();
  } else if (psiElement instanceof PsiMethod) {
    return (PsiClassType) ((PsiMethod) psiElement).getReturnType();
  }
  return null;
}
 
public PsiVariable getVariableDeclaration() {
    return variableDeclaration;
}
 
public void setVariableDeclaration(PsiVariable variableDeclaration) {
    this.variableDeclaration = variableDeclaration;
}
 
源代码26 项目: IntelliJDeodorant   文件: ParameterObject.java
public void setSingleVariableDeclaration(PsiVariable singleVariableDeclaration) {
    this.singleVariableDeclaration = toPointer(singleVariableDeclaration);
}
 
源代码27 项目: IntelliJDeodorant   文件: ParameterObject.java
public PsiVariable getSingleVariableDeclaration() {
    return (PsiVariable) this.singleVariableDeclaration.getElement();
}
 
源代码28 项目: IntelliJDeodorant   文件: ParameterObject.java
public PsiVariable getVariableDeclaration() {
    return getSingleVariableDeclaration();
}
 
源代码29 项目: IntelliJDeodorant   文件: InstanceOfVariable.java
public boolean instanceOf(PsiExpression expression) {
    return (expression instanceof PsiReferenceExpression &&
            ((PsiReferenceExpression) expression).resolve() instanceof PsiVariable);
}
 
源代码30 项目: IntelliJDeodorant   文件: PDGObjectSliceUnion.java
public Set<PsiVariable> getVariableDeclarationsAndAccessedFieldsInMethod() {
    return pdg.getVariableDeclarationsAndAccessedFieldsInMethod();
}
 
 类所在包
 类方法
 同包方法