AbstractDataServiceVisitor implements ElementVisitor<String, Writer> ,ProcessorConstants { boolean isConsiderateMethod(Collection<String> methodProceeds, ExecutableElement methodElement) { String signature = methodElement.getSimpleName().toString(); if (methodProceeds.contains(signature)) { return false; } TypeElement objectElement = environment.getElementUtils().getTypeElement(Object.class.getName()); if (objectElement.getEnclosedElements().contains(methodElement)) { return false; } if (!methodElement.getModifiers().contains(Modifier.PUBLIC) || methodElement.getModifiers().contains(Modifier.STATIC)) { return false; } List<? extends AnnotationMirror> annotationMirrors = methodElement.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (annotationMirror.getAnnotationType().toString().equals(TransientDataService.class.getName())) { return false; } } methodProceeds.add(signature); return true; } AbstractDataServiceVisitor(ProcessingEnvironment environment); @Override String visitType(TypeElement typeElement, Writer writer); @Override String visit(Element e, Writer p); @Override String visit(Element e); @Override String visitPackage(PackageElement e, Writer p); @Override String visitVariable(VariableElement e, Writer p); @Override String visitExecutable(ExecutableElement e, Writer p); @Override String visitTypeParameter(TypeParameterElement e, Writer p); @Override String visitUnknown(Element e, Writer p);  }
@Test public void testIsConsiderateMethod() { System.out.println("isConsiderateMethod"); String methodname = "methodName"; Collection<String> methodProceeds = new ArrayList<>(); ExecutableElement methodElement = mock(ExecutableElement.class); VariableElement var0 = mock(VariableElement.class); VariableElement var1 = mock(VariableElement.class); List ves = Arrays.asList(var0, var1); Name name = mock(Name.class); TypeElement objectElement = mock(TypeElement.class); List enclosedElements = mock(List.class); Set modifiers = new HashSet(); List annotationMirrors = new ArrayList(); AnnotationMirror anno = mock(AnnotationMirror.class); AnnotationMirror annoTransient = mock(AnnotationMirror.class); DeclaredType declaredType0 = mock(DeclaredType.class); DeclaredType declaredType1 = mock(DeclaredType.class); when(anno.getAnnotationType()).thenReturn(declaredType0); when(annoTransient.getAnnotationType()).thenReturn(declaredType1); when(declaredType0.toString()).thenReturn(DataService.class.getName()); when(declaredType1.toString()).thenReturn(TransientDataService.class.getName()); when(name.toString()).thenReturn(methodname); when(methodElement.getParameters()).thenReturn(ves); when(methodElement.getSimpleName()).thenReturn(name); when(methodElement.getModifiers()).thenReturn(modifiers); when(methodElement.getAnnotationMirrors()).thenReturn(annotationMirrors); when(elementUtils.getTypeElement(eq("java.lang.Object"))).thenReturn(objectElement); when(objectElement.getEnclosedElements()).thenReturn(enclosedElements); when(enclosedElements.contains(anyObject())).thenReturn(Boolean.FALSE) .thenReturn(Boolean.TRUE) .thenReturn(Boolean.FALSE) .thenReturn(Boolean.FALSE); modifiers.add(Modifier.PUBLIC); boolean result = instance.isConsiderateMethod(methodProceeds, methodElement); assertThat(result).isTrue(); assertThat(methodProceeds).hasSize(1); result = instance.isConsiderateMethod(methodProceeds, methodElement); assertThat(result).isFalse(); assertThat(methodProceeds).hasSize(1); methodProceeds.clear(); result = instance.isConsiderateMethod(methodProceeds, methodElement); assertThat(result).isFalse(); assertThat(methodProceeds).hasSize(0); methodProceeds.clear(); modifiers.add(Modifier.STATIC); result = instance.isConsiderateMethod(methodProceeds, methodElement); assertThat(result).isFalse(); assertThat(methodProceeds).hasSize(0); methodProceeds.clear(); modifiers.clear(); result = instance.isConsiderateMethod(methodProceeds, methodElement); assertThat(result).isFalse(); assertThat(methodProceeds).hasSize(0); methodProceeds.clear(); modifiers.add(Modifier.PUBLIC); annotationMirrors.add(anno); annotationMirrors.add(annoTransient); result = instance.isConsiderateMethod(methodProceeds, methodElement); assertThat(result).isFalse(); assertThat(methodProceeds).hasSize(0); }