@Test public void annotateStatement() { testHelper.getPsiClass( psiClasses -> { assertEquals(2, psiClasses.size()); PsiClass underTest = psiClasses.get(0); PsiClass component = psiClasses.get(1); Function<PsiMethodCallExpression, PsiClass> resolver = psiMethodCallExpression -> component; RequiredPropAnnotator annotator = new RequiredPropAnnotator(resolver); TestHolder holder = new TestHolder(); Collection<PsiStatement> statements = PsiTreeUtil.findChildrenOfAnyType(underTest, PsiStatement.class); for (PsiStatement statement : statements) { annotator.annotate(statement, holder); } assertEquals(3, holder.errorMessages.size()); for (String errorMessage : holder.errorMessages) { assertEquals( "The following props are not " + "marked as optional and were not supplied: testRequiredPropName", errorMessage); } assertEquals(3, holder.errorElements.size()); for (PsiElement errorElement : holder.errorElements) { assertEquals("RequiredPropAnnotatorComponent.create", errorElement.getText()); } return true; }, "RequiredPropAnnotatorTest.java", "RequiredPropAnnotatorComponent.java"); }
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); } }
RequiredPropAnnotator implements Annotator { 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); } } }
RequiredPropAnnotator implements Annotator { 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); } } RequiredPropAnnotator(); @VisibleForTesting  RequiredPropAnnotator(Function<PsiMethodCallExpression, PsiClass> generatedClassResolver); }
RequiredPropAnnotator implements Annotator { 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); } } RequiredPropAnnotator(); @VisibleForTesting  RequiredPropAnnotator(Function<PsiMethodCallExpression, PsiClass> generatedClassResolver); @Override void annotate(PsiElement element, AnnotationHolder holder); }
RequiredPropAnnotator implements Annotator { 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); } } RequiredPropAnnotator(); @VisibleForTesting  RequiredPropAnnotator(Function<PsiMethodCallExpression, PsiClass> generatedClassResolver); @Override void annotate(PsiElement element, AnnotationHolder holder);  }