com.intellij.psi.PsiParameter Java Examples
The following examples show how to use
com.intellij.psi.PsiParameter.
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: OnEventGenerateAction.java From litho with Apache License 2.0 | 6 votes |
/** @return method based on user choice. */ @Override protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project) { return Optional.ofNullable(eventChooser.choose(aClass, project)) .map( eventClass -> { final List<PsiParameter> propsAndStates = LithoPluginUtils.getPsiParameterStream(null, aClass.getMethods()) .filter(LithoPluginUtils::isPropOrState) .collect(Collectors.toList()); return OnEventGenerateUtils.createOnEventMethod(aClass, eventClass, propsAndStates); }) .map(onEventMethod -> onEventRefactorer.changeSignature(project, onEventMethod, aClass)) .map( customMethod -> { OnEventGenerateUtils.addComment(aClass, customMethod); onEventGeneratedListener.onGenerated(customMethod); return new ClassMember[] {new PsiMethodMember(customMethod)}; }) .orElse(ClassMember.EMPTY_ARRAY); }
Example #2
Source File: PsiMethodExtractorUtils.java From litho with Apache License 2.0 | 6 votes |
private static List<AnnotationSpec> getExternalAnnotations(PsiParameter param) { PsiAnnotation[] annotationsOnParam = AnnotationUtil.getAllAnnotations(param, false, null); final List<AnnotationSpec> annotations = new ArrayList<>(); for (PsiAnnotation annotationOnParam : annotationsOnParam) { if (annotationOnParam.getQualifiedName().startsWith(COMPONENTS_PACKAGE)) { continue; } final AnnotationSpec.Builder annotationSpec = AnnotationSpec.builder(PsiTypeUtils.guessClassName(annotationOnParam.getQualifiedName())); PsiNameValuePair[] paramAttributes = annotationOnParam.getParameterList().getAttributes(); for (PsiNameValuePair attribute : paramAttributes) { annotationSpec.addMember(attribute.getName(), attribute.getDetachedValue().getText()); } annotations.add(annotationSpec.build()); } return annotations; }
Example #3
Source File: PsiAnnotationProxyUtilsTest.java From litho with Apache License 2.0 | 6 votes |
@Test public void defaultValues() { testHelper.getPsiClass( psiClasses -> { assertNotNull(psiClasses); PsiClass psiClass = psiClasses.get(0); PsiParameter[] parameters = PsiTreeUtil.findChildOfType(psiClass, PsiParameterList.class).getParameters(); Prop prop = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[0], Prop.class); assertNotNull(prop); assertFalse(prop.optional()); assertFalse(prop.isCommonProp()); assertFalse(prop.overrideCommonPropBehavior()); assertFalse(prop.dynamic()); assertEquals(ResType.NONE, prop.resType()); return true; }, "WithAnnotationClass.java"); }
Example #4
Source File: PsiMethodExtractorUtils.java From litho with Apache License 2.0 | 6 votes |
static List<MethodParamModel> getMethodParams( PsiMethod method, List<Class<? extends Annotation>> permittedAnnotations, List<Class<? extends Annotation>> permittedInterStageInputAnnotations, List<Class<? extends Annotation>> delegateMethodAnnotationsThatSkipDiffModels) { final List<MethodParamModel> methodParamModels = new ArrayList<>(); PsiParameter[] params = method.getParameterList().getParameters(); for (final PsiParameter param : params) { methodParamModels.add( MethodParamModelFactory.create( PsiTypeUtils.generateTypeSpec(param.getType()), param.getName(), getLibraryAnnotations(param, permittedAnnotations), getExternalAnnotations(param), permittedInterStageInputAnnotations, canCreateDiffModels(method, delegateMethodAnnotationsThatSkipDiffModels), param)); } return methodParamModels; }
Example #5
Source File: CodeGenerator.java From ParcelablePlease with Apache License 2.0 | 6 votes |
/** * Finds and removes a given method * @param methodName * @param arguments */ private void findAndRemoveMethod(String methodName, String... arguments) { // Maybe there's an easier way to do this with mClass.findMethodBySignature(), but I'm not an expert on Psi* PsiMethod[] methods = psiClass.findMethodsByName(methodName, false); for (PsiMethod method : methods) { PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() == arguments.length) { boolean shouldDelete = true; PsiParameter[] parameters = parameterList.getParameters(); for (int i = 0; i < arguments.length; i++) { if (!parameters[i].getType().getCanonicalText().equals(arguments[i])) { shouldDelete = false; } } if (shouldDelete) { method.delete(); } } } }
Example #6
Source File: PsiAnnotationProxyUtilsTest.java From litho with Apache License 2.0 | 6 votes |
@Test public void setValues() { testHelper.getPsiClass( psiClasses -> { assertNotNull(psiClasses); PsiClass psiClass = psiClasses.get(0); PsiParameter[] parameters = PsiTreeUtil.findChildOfType(psiClass, PsiParameterList.class).getParameters(); Prop prop = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[1], Prop.class); assertNotNull(prop); assertTrue(prop.optional()); assertTrue(prop.isCommonProp()); assertTrue(prop.overrideCommonPropBehavior()); assertTrue(prop.dynamic()); assertEquals(ResType.DRAWABLE, prop.resType()); return true; }, "WithAnnotationClass.java"); }
Example #7
Source File: ValTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void testIntParameter() { myFixture.configureByText("a.java", "import lombok.val;\n" + "abstract class Test {\n" + " private void test() {\n" + " int[] myArray = new int[] {1, 2, 3, 4, 5};\n" + " for(val my<caret>Var: myArray) {" + " }\n" + " } \n" + "}\n"); final PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); assertTrue(elementAtCaret instanceof PsiIdentifier); final PsiElement localParameter = elementAtCaret.getParent(); assertTrue(localParameter.toString(), localParameter instanceof PsiParameter); final PsiType type = ((PsiParameter) localParameter).getType(); assertNotNull(localParameter.toString(), type); assertTrue(type.getCanonicalText(), type.equalsToText("int")); }
Example #8
Source File: VarTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void testIntParameter() { myFixture.configureByText("a.java", "import lombok.experimental.var;\n" + "abstract class Test {\n" + " private void test() {\n" + " int[] myArray = new int[] {1, 2, 3, 4, 5};\n" + " for(var my<caret>Var: myArray) {" + " }\n" + " } \n" + "}\n"); final PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); assertTrue(elementAtCaret instanceof PsiIdentifier); final PsiElement localParameter = elementAtCaret.getParent(); assertTrue(localParameter.toString(), localParameter instanceof PsiParameter); final PsiType type = ((PsiParameter) localParameter).getType(); assertNotNull(localParameter.toString(), type); assertTrue(type.getCanonicalText(), type.equalsToText("int")); }
Example #9
Source File: Decider.java From dagger-intellij-plugin with Apache License 2.0 | 6 votes |
@Override public boolean shouldShow(UsageTarget target, Usage usage) { PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement(); PsiField field = PsiConsultantImpl.findField(element); if (field != null // && PsiConsultantImpl.hasAnnotation(field, CLASS_INJECT) // && PsiConsultantImpl.hasQuailifierAnnotations(field, qualifierAnnotations) && PsiConsultantImpl.hasTypeParameters(field, typeParameters)) { return true; } PsiMethod method = PsiConsultantImpl.findMethod(element); if (method != null && (PsiConsultantImpl.hasAnnotation(method, CLASS_INJECT) || PsiConsultantImpl.hasAnnotation(method, CLASS_PROVIDES))) { for (PsiParameter parameter : method.getParameterList().getParameters()) { PsiClass parameterClass = PsiConsultantImpl.checkForLazyOrProvider(parameter); if (parameterClass.equals(returnType) && PsiConsultantImpl.hasQuailifierAnnotations( parameter, qualifierAnnotations) && PsiConsultantImpl.hasTypeParameters(parameter, typeParameters)) { return true; } } } return false; }
Example #10
Source File: PsiAnnotationProxyUtilsTest.java From litho with Apache License 2.0 | 6 votes |
@Test public void proxyEquals_equal() { testHelper.getPsiClass( psiClasses -> { assertNotNull(psiClasses); PsiClass psiClass = psiClasses.get(0); PsiParameter[] parameters = PsiTreeUtil.findChildOfType(psiClass, PsiParameterList.class).getParameters(); Prop prop1 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[0], Prop.class); Prop prop2 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[2], Prop.class); // Calls proxy assertEquals(prop1, prop2); return true; }, "WithAnnotationClass.java"); }
Example #11
Source File: UppercaseStatePropInspection.java From litho with Apache License 2.0 | 6 votes |
@Nullable @Override public ProblemDescriptor[] checkMethod( @NotNull PsiMethod method, @NotNull InspectionManager manager, boolean isOnTheFly) { if (!LithoPluginUtils.isLithoSpec(method.getContainingClass())) { return ProblemDescriptor.EMPTY_ARRAY; } return Optional.of(method) .map(PsiMethod::getParameterList) .map(PsiParameterList::getParameters) .map( psiParameters -> Stream.of(psiParameters) .filter(LithoPluginUtils::isPropOrState) .filter(UppercaseStatePropInspection::isFirstLetterCapital) .map(PsiParameter::getNameIdentifier) .filter(Objects::nonNull) .map(identifier -> createWarning(identifier, manager, isOnTheFly)) .toArray(ProblemDescriptor[]::new)) .orElse(ProblemDescriptor.EMPTY_ARRAY); }
Example #12
Source File: OnEventChangeSignatureDialog.java From litho with Apache License 2.0 | 6 votes |
@Override protected boolean isRowEditable(int row) { // If table parameter is an initial method parameter, than it's not editable. if (row == 0) { stopEditing(); } final List<ParameterTableModelItemBase<ParameterInfoImpl>> currentTableItems = myParametersTableModel.getItems(); if (row > currentTableItems.size()) { return true; } final ParameterInfoImpl parameterInfo = currentTableItems.get(row).parameter; final PsiParameter initialMethodParameter = getInitialMethodParameter(parameterInfo.getName(), parameterInfo.getTypeText()); return initialMethodParameter == null; }
Example #13
Source File: PsiAnnotationProxyUtilsTest.java From litho with Apache License 2.0 | 6 votes |
@Test public void proxyEquals_not_equal() { testHelper.getPsiClass( psiClasses -> { assertNotNull(psiClasses); PsiClass psiClass = psiClasses.get(0); PsiParameter[] parameters = PsiTreeUtil.findChildOfType(psiClass, PsiParameterList.class).getParameters(); Prop prop1 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[0], Prop.class); Prop prop2 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[1], Prop.class); // Calls proxy assertNotEquals(prop1, prop2); return true; }, "WithAnnotationClass.java"); }
Example #14
Source File: ConstructorInjectToProvidesHandler.java From dagger-intellij-plugin with Apache License 2.0 | 6 votes |
@Override public void navigate(final MouseEvent mouseEvent, PsiElement psiElement) { if (!(psiElement instanceof PsiMethod)) { throw new IllegalStateException("Called with non-method: " + psiElement); } PsiMethod psiMethod = (PsiMethod) psiElement; PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); if (parameters.length == 1) { showUsages(mouseEvent, parameters[0]); } else { new PickTypeAction().startPickTypes(new RelativePoint(mouseEvent), parameters, new PickTypeAction.Callback() { @Override public void onParameterChosen(PsiParameter selected) { showUsages(mouseEvent, selected); } }); } }
Example #15
Source File: PsiAnnotationProxyUtilsTest.java From litho with Apache License 2.0 | 6 votes |
@Test public void proxyHashCode() { testHelper.getPsiClass( psiClasses -> { assertNotNull(psiClasses); PsiClass psiClass = psiClasses.get(0); PsiParameter[] parameters = PsiTreeUtil.findChildOfType(psiClass, PsiParameterList.class).getParameters(); Prop prop1 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[0], Prop.class); Prop prop2 = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[2], Prop.class); Set<Prop> props = new HashSet<>(1); props.add(prop1); props.add(prop2); // Calls proxy assertEquals(1, props.size()); return true; }, "WithAnnotationClass.java"); }
Example #16
Source File: StatePropCompletionContributor.java From litho with Apache License 2.0 | 6 votes |
static void addCompletionResult( @NotNull CompletionResultSet completionResultSet, PsiMethod currentMethod, PsiMethod[] allMethods, Predicate<PsiParameter> annotationCheck) { // Don't propose completion with current method parameters Set<String> excludingParameters = Stream.of(currentMethod.getParameterList().getParameters()) .filter(annotationCheck) .map(PsiParameter::getName) .filter(Objects::nonNull) .collect(Collectors.toSet()); LithoPluginUtils.getPsiParameterStream(currentMethod, allMethods) .filter(annotationCheck) .filter(parameter -> !excludingParameters.contains(parameter.getName())) .map(StatePropCompletionContributor::createCompletionResult) .forEach(completionResultSet::addElement); }
Example #17
Source File: OnEventGenerateUtils.java From litho with Apache License 2.0 | 6 votes |
/** * Adds comment to the given method "// An event handler ContextClassName.methodName(c, * parameterName) */ public static void addComment(PsiClass contextClass, PsiMethod method) { final Project project = contextClass.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final StringBuilder builder = new StringBuilder("// An event handler ") .append(LithoPluginUtils.getLithoComponentNameFromSpec(contextClass.getName())) .append(".") .append(method.getName()) .append("(") .append(CONTEXT_PARAMETER_NAME); for (PsiParameter parameter : method.getParameterList().getParameters()) { if (LithoPluginUtils.isParam(parameter)) { builder.append(", ").append(parameter.getName()); } } builder.append(")"); final PsiComment comment = factory.createCommentFromText(builder.toString(), method); method.addBefore(comment, method.getModifierList()); }
Example #18
Source File: ConstructorInjectToProvidesHandler.java From dagger-intellij-plugin with Apache License 2.0 | 6 votes |
private boolean navigateToConstructorIfProvider(PsiParameter psiParameter) { PsiTypeElement declaringTypeElement = psiParameter.getTypeElement(); PsiClass classElement = JavaPsiFacade.getInstance(psiParameter.getProject()).findClass( declaringTypeElement.getType().getCanonicalText(), declaringTypeElement.getResolveScope()); if (classElement == null) { return false; } for (PsiMethod method : classElement.getConstructors()) { if (PsiConsultantImpl.hasAnnotation(method, CLASS_INJECT) && navigateToElement(method)) { return true; } } return false; }
Example #19
Source File: InnerBuilderUtils.java From innerbuilder with Apache License 2.0 | 6 votes |
static boolean areParameterListsEqual(PsiParameterList paramList1, PsiParameterList paramList2) { if (paramList1.getParametersCount() != paramList2.getParametersCount()) { return false; } final PsiParameter[] param1Params = paramList1.getParameters(); final PsiParameter[] param2Params = paramList2.getParameters(); for (int i = 0; i < param1Params.length; i++) { final PsiParameter param1Param = param1Params[i]; final PsiParameter param2Param = param2Params[i]; if (!areTypesPresentableEqual(param1Param.getType(), param2Param.getType())) { return false; } } return true; }
Example #20
Source File: PsiAnnotationProxyUtilsTest.java From litho with Apache License 2.0 | 6 votes |
@Test public void proxyToString() { testHelper.getPsiClass( psiClasses -> { assertNotNull(psiClasses); PsiClass psiClass = psiClasses.get(0); PsiParameter[] parameters = PsiTreeUtil.findChildOfType(psiClass, PsiParameterList.class).getParameters(); Prop prop = PsiAnnotationProxyUtils.findAnnotationInHierarchy(parameters[0], Prop.class); // Calls proxy assertEquals("@com.facebook.litho.annotations.Prop()", prop.toString()); return true; }, "WithAnnotationClass.java"); }
Example #21
Source File: JavaMethodUtils.java From camel-idea-plugin with Apache License 2.0 | 5 votes |
/** * * @param method - method to generate display text for * @return parameters as text separated with comma */ public String getMethodParameters(PsiMethod method) { return Arrays.stream(method.getParameters()) .map(PsiParameter.class::cast) .map(PsiParameter::getText) .collect(Collectors.joining(", ")); }
Example #22
Source File: ThinrDetector.java From thinr with Apache License 2.0 | 5 votes |
private void markLeakSuspects(PsiElement element, PsiElement lambdaBody, @NonNull final JavaContext context) { if (element instanceof PsiReferenceExpression) { PsiReferenceExpression ref = (PsiReferenceExpression) element; if (ref.getQualifierExpression() == null) { PsiElement res = ref.resolve(); if (!(res instanceof PsiParameter)) { if (!(res instanceof PsiClass)) { boolean error = false; if (res instanceof PsiLocalVariable) { PsiLocalVariable lVar = (PsiLocalVariable) res; if (!isParent(lambdaBody, lVar.getParent())) { error = true; } } if (res instanceof PsiField) { PsiField field = (PsiField) res; final PsiModifierList modifierList = field.getModifierList(); if (modifierList == null) { error = true; } else if (!modifierList.hasExplicitModifier(PsiModifier.STATIC)) { error = true; } } if (error) { context.report(ISSUE, element, context.getNameLocation(element), "Possible leak"); } } } } } for (PsiElement psiElement : element.getChildren()) { markLeakSuspects(psiElement, lambdaBody, context); } }
Example #23
Source File: OttoLineMarkerProvider.java From otto-intellij-plugin with Apache License 2.0 | 5 votes |
public static @Nullable PsiTypeElement getMethodParameter(PsiMethod subscribeMethod) { PsiParameterList parameterList = subscribeMethod.getParameterList(); if (parameterList.getParametersCount() != 1) { return null; } else { PsiParameter subscribeMethodParam = parameterList.getParameters()[0]; return subscribeMethodParam.getTypeElement(); } }
Example #24
Source File: MethodFQN.java From intellij-reference-diagram with Apache License 2.0 | 5 votes |
@NotNull public static List<String> getParameterArray(PsiMethod psiMethod) { List<String> parameters = new ArrayList<>(); for (PsiParameter psiParameter : psiMethod.getParameterList().getParameters()) { String parameter = psiParameter.getType().getPresentableText(); parameters.add(parameter); } return parameters; }
Example #25
Source File: ForEachStatementTranslator.java From java2typescript with Apache License 2.0 | 5 votes |
public static void translate(PsiForeachStatement element, TranslationContext ctx) { PsiParameter parameter = element.getIterationParameter(); ctx.print(""); ExpressionTranslator.translate(element.getIteratedValue(), ctx); ctx.append(".forEach(("); ctx.append(KeywordHelper.process(parameter.getName(), ctx)); ctx.append(": "); ctx.append(TypeHelper.printType(parameter.getType(), ctx)); ctx.append(") => {\n"); ctx.increaseIdent(); StatementTranslator.translate(element.getBody(), ctx); ctx.decreaseIdent(); ctx.print("});\n"); }
Example #26
Source File: SetterFieldProcessor.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@NotNull private String createCodeBlockText(@NotNull PsiField psiField, @NotNull PsiClass psiClass, PsiType returnType, boolean isStatic, PsiParameter methodParameter) { final String blockText; final String thisOrClass = isStatic ? psiClass.getName() : "this"; blockText = String.format("%s.%s = %s; ", thisOrClass, psiField.getName(), methodParameter.getName()); String codeBlockText = blockText; if (!isStatic && !PsiType.VOID.equals(returnType)) { codeBlockText += "return this;"; } return codeBlockText; }
Example #27
Source File: PickTypeAction.java From dagger-intellij-plugin with Apache License 2.0 | 5 votes |
public void startPickTypes(RelativePoint relativePoint, PsiParameter[] psiParameters, final Callback callback) { if (psiParameters.length == 0) return; ListPopup listPopup = JBPopupFactory.getInstance() .createListPopup(new BaseListPopupStep<PsiParameter>("Select Type", psiParameters) { @NotNull @Override public String getTextFor(PsiParameter value) { StringBuilder builder = new StringBuilder(); Set<String> annotations = PsiConsultantImpl.getQualifierAnnotations(value); for (String annotation : annotations) { String trimmed = annotation.substring(annotation.lastIndexOf(".") + 1); builder.append("@").append(trimmed).append(" "); } PsiClass notLazyOrProvider = PsiConsultantImpl.checkForLazyOrProvider(value); return builder.append(notLazyOrProvider.getName()).toString(); } @Override public PopupStep onChosen(PsiParameter selectedValue, boolean finalChoice) { callback.onParameterChosen(selectedValue); return super.onChosen(selectedValue, finalChoice); } }); listPopup.show(relativePoint); }
Example #28
Source File: PsiConsultantImpl.java From dagger-intellij-plugin with Apache License 2.0 | 5 votes |
public static PsiClass checkForLazyOrProvider(PsiParameter psiParameter) { PsiClass wrapperClass = PsiConsultantImpl.getClass(psiParameter); PsiType psiParameterType = psiParameter.getType(); if (!(psiParameterType instanceof PsiClassType)) { return wrapperClass; } return getPsiClass(wrapperClass, psiParameterType); }
Example #29
Source File: ConstructorInjectToProvidesHandler.java From dagger-intellij-plugin with Apache License 2.0 | 5 votes |
private void showUsages(final MouseEvent mouseEvent, final PsiParameter psiParameter) { // Check to see if class type of psiParameter has constructor with @Inject. Otherwise, proceed. if (navigateToConstructorIfProvider(psiParameter)) { return; } // If psiParameter is Set<T>, check if @Provides(type=SET) T providesT exists. // Also check map (TODO(radford): Add check for map). List<PsiType> paramTypes = PsiConsultantImpl.getTypeParameters(psiParameter); if (paramTypes.isEmpty()) { new ShowUsagesAction( new Decider.ConstructorParameterInjectDecider(psiParameter)).startFindUsages( PsiConsultantImpl.checkForLazyOrProvider(psiParameter), new RelativePoint(mouseEvent), PsiUtilBase.findEditor(psiParameter), MAX_USAGES); } else { ShowUsagesAction actions = new ShowUsagesAction( new Decider.CollectionElementParameterInjectDecider(psiParameter)); actions.setListener(new ShowUsagesAction.Listener() { @Override public void onFinished(boolean hasResults) { if (!hasResults) { new ShowUsagesAction( new Decider.ConstructorParameterInjectDecider(psiParameter)).startFindUsages( PsiConsultantImpl.checkForLazyOrProvider(psiParameter), new RelativePoint(mouseEvent), PsiUtilBase.findEditor(psiParameter), MAX_USAGES); } } }); actions.startFindUsages(((PsiClassType) paramTypes.get(0)).resolve(), new RelativePoint(mouseEvent), PsiUtilBase.findEditor(psiParameter), MAX_USAGES); } }
Example #30
Source File: OttoLineMarkerProvider.java From otto-intellij-plugin with Apache License 2.0 | 5 votes |
@Override public boolean shouldShow(Usage usage) { PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement(); if (element instanceof PsiJavaCodeReferenceElement) { if ((element = element.getContext()) instanceof PsiTypeElement) { if ((element = element.getContext()) instanceof PsiParameter) { if ((element = element.getContext()) instanceof PsiParameterList) { if ((element = element.getContext()) instanceof PsiMethod) { return SubscriberMetadata.isAnnotatedWithSubscriber((PsiMethod) element); } } } } } return false; }