com.intellij.psi.PsiVariable Java Examples
The following examples show how to use
com.intellij.psi.PsiVariable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PDGSliceUnion.java From IntelliJDeodorant with MIT License | 6 votes |
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; }
Example #2
Source File: RequiredPropAnnotator.java From litho with Apache License 2.0 | 6 votes |
/** * 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); } }
Example #3
Source File: SingularGuavaTableHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
@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); }
Example #4
Source File: ExtractMethodTreeTableModel.java From IntelliJDeodorant with MIT License | 6 votes |
@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 ""; }
Example #5
Source File: StructureAnnotationDeclaredCorrectlyInspection.java From attic-polygene-java with Apache License 2.0 | 6 votes |
@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; }
Example #6
Source File: PDGObjectSliceUnion.java From IntelliJDeodorant with MIT License | 6 votes |
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; }
Example #7
Source File: AbstractInjectionAnnotationDeclarationOnFieldInspection.java From attic-polygene-java with Apache License 2.0 | 5 votes |
/** * @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 ); }
Example #8
Source File: CamelRouteLineMarkerProvider.java From camel-idea-plugin with Apache License 2.0 | 5 votes |
/** * 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(); }
Example #9
Source File: CamelRouteLineMarkerProvider.java From camel-idea-plugin with Apache License 2.0 | 5 votes |
/** * 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; }
Example #10
Source File: IdeaUtilsIsCamelRouteStartTestIT.java From camel-idea-plugin with Apache License 2.0 | 5 votes |
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)); }
Example #11
Source File: PolygeneServiceAnnotationUtil.java From attic-polygene-java with Apache License 2.0 | 5 votes |
/** * 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; }
Example #12
Source File: IdeaUtilsIsCamelRouteStartTestIT.java From camel-idea-plugin with Apache License 2.0 | 5 votes |
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)); }
Example #13
Source File: AccessorsInfo.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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); } }
Example #14
Source File: SingularHandlerFactory.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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(); }
Example #15
Source File: SingularMapHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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); }
Example #16
Source File: SingularGuavaCollectionHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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); }
Example #17
Source File: SingularGuavaMapHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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); }
Example #18
Source File: PsiConsultantImpl.java From dagger-intellij-plugin with Apache License 2.0 | 5 votes |
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; }
Example #19
Source File: PsiConsultantImpl.java From dagger-intellij-plugin with Apache License 2.0 | 5 votes |
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; }
Example #20
Source File: MicroProfileConfigPropertyProvider.java From intellij-quarkus with Eclipse Public License 2.0 | 5 votes |
@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)); } } }
Example #21
Source File: ReachingAliasSet.java From IntelliJDeodorant with MIT License | 5 votes |
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); }
Example #22
Source File: ReachingAliasSet.java From IntelliJDeodorant with MIT License | 5 votes |
boolean containsAlias(AbstractVariable variable) { for (LinkedHashSet<PsiVariable> aliasSet : aliasSets) { for (PsiVariable alias : aliasSet) { if (alias.equals(variable.getOrigin())) return true; } } return false; }
Example #23
Source File: InstanceOfVariableDeclarationStatement.java From IntelliJDeodorant with MIT License | 5 votes |
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; }
Example #24
Source File: BuilderElementHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 4 votes |
default String renderBuildPrepare(@NotNull PsiVariable psiVariable, @NotNull String fieldName) { return ""; }
Example #25
Source File: LocalVariableDeclarationObject.java From IntelliJDeodorant with MIT License | 4 votes |
public PsiVariable getVariableDeclaration() { return variableDeclaration; }
Example #26
Source File: BuilderElementHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 4 votes |
default String renderBuildCode(@NotNull PsiVariable psiVariable, @NotNull String fieldName, @NotNull String builderVariable) { return ""; }
Example #27
Source File: BuilderElementHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 4 votes |
default String renderSuperBuilderConstruction(@NotNull PsiVariable psiVariable, @NotNull String fieldName) { return "this." + psiVariable.getName() + "=b." + fieldName + ";\n"; }
Example #28
Source File: SingularMapHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public String renderBuildPrepare(@NotNull PsiVariable psiVariable, @NotNull String fieldName) { return renderBuildCode(psiVariable, fieldName, "this"); }
Example #29
Source File: ParameterObject.java From IntelliJDeodorant with MIT License | 4 votes |
public PsiVariable getVariableDeclaration() { return getSingleVariableDeclaration(); }
Example #30
Source File: SingularMapHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 4 votes |
public String renderSuperBuilderConstruction(@NotNull PsiVariable psiVariable, @NotNull String fieldName) { final String basicCode = renderBuildCode(psiVariable, fieldName, "b"); final String assignment = "this." + psiVariable.getName() + "=" + fieldName + ";\n"; return basicCode + assignment; }