-
Notifications
You must be signed in to change notification settings - Fork 5
Niveau 2 #5
Comments
Pour les exos 5 à Z, l'idée est un genre de parcours dirigé des outils dispo et des bonnes pratiques. Je me dis que pour découvrir l'API Seulement, je ne vois pas trop comment l'écrire en test U. Ces codes ne peuvent être exécutés que dans le contexte d'un AP. L'alternative étant de mocker l'API La seule solution que je vois, c'est d'utiliser un AP dont le rôle est de cracher des messages de compilation produits à partir du résultat du code à écrire. Le test U consisterait alors à vérifier avec compile-testing ce qui est affiché. Encore faut-il que compile-testing le permette. |
Idée pour exo 5 à Z : extraire toutes les méthodes et constructeurs d'une classe (héritée ou non)code de @Nonnull
private List<JavaxDAMethod> retrieveMethods(final TypeElement classElement, final JavaxExtractor javaxExtractor) {
List<TypeElement> classHierarchy = extractClassHierarchyAsList(classElement);
List<JavaxDAMethod> res = Lists.of();
for (TypeElement clazz : classHierarchy) {
if (clazz.getEnclosedElements() == null) {
continue;
}
for (JavaxDAMethod javaxDAMethod : from(clazz.getEnclosedElements())
// methods are ExecutableElement
.filter(ExecutableElement.class)
// filter out super class constructors
.filter(
clazz.equals(classElement) ? Predicates.<ExecutableElement>alwaysTrue() : FilterOutConstructor.INSTANCE
)
// transform into object of the DAModel
.transform(new ExecutableElementToJavaxDAMethod(javaxExtractor, classElement))
.filter(notNull())) {
res.add(javaxDAMethod);
}
}
return res;
}
private static enum FilterOutConstructor implements Predicate<ExecutableElement> {
INSTANCE;
@Override
public boolean apply(@Nullable ExecutableElement executableElement) {
return executableElement != null && executableElement.getKind() != ElementKind.CONSTRUCTOR;
}
}
private List<TypeElement> extractClassHierarchyAsList(TypeElement classElement) {
return classElement.accept(new SuperTypeElementsVisitor(), new ArrayList<TypeElement>());
}
@Nullable
private static TypeElement asTypeElement(TypeMirror t) {
DeclaredType declaredType = asDeclaredType(t);
if (declaredType == null) {
return null;
}
return declaredType.asElement().accept(AsTypeElementVisitor.INSTANCE, null);
}
private static class AsTypeElementVisitor extends SimpleElementVisitor6<TypeElement, Void> {
public static final AsTypeElementVisitor INSTANCE = new AsTypeElementVisitor();
private AsTypeElementVisitor() {
// prevents instantiation
}
@Override
public TypeElement visitType(TypeElement e, Void o) {
return e;
}
}
@Nullable
private static DeclaredType asDeclaredType(TypeMirror t) {
return t.accept(AsDeclaredTypeTypeVisitor.INSTANCE, null);
}
private static class AsDeclaredTypeTypeVisitor extends SimpleTypeVisitor6<DeclaredType, Object> {
public static final AsDeclaredTypeTypeVisitor INSTANCE = new AsDeclaredTypeTypeVisitor();
private AsDeclaredTypeTypeVisitor() {
// prevents instantiation
}
@Override
public DeclaredType visitDeclared(DeclaredType t, Object o) {
return t;
}
} A noter qu'il semble que l'extraction des méthodes aurait pu être codée en exploitant List<? extends Element> allMembers = elementUtils.getAllMembers(classElement);
List<ExecutableElement> executableElements = ElementFilter.methodsIn(allMembers); |
sur la compréhension de
|
methods in source has not yet been emptied of their implementation methods have javadoc README for exo1 is done
methods in source has not yet been emptied of their implementation methods have javadoc README for exo1 is done
add to use another way of writing unit test again the element and type package because when used outside the annotation processing that created it, the Element returned by the Elements class are not equal to the one that existed during the annotation processing run (cf. http://stackoverflow.com/a/18620582)
add to use another way of writing unit test again the element and type package because when used outside the annotation processing that created it, the Element returned by the Elements class are not equal to the one that existed during the annotation processing run (cf. http://stackoverflow.com/a/18620582)
Je viens de modifier la description de cette issue:
@fbiville WDYT ? |
methods in source has not yet been emptied of their implementation methods have javadoc README for exo1 is done
add to use another way of writing unit test again the element and type package because when used outside the annotation processing that created it, the Element returned by the Elements class are not equal to the one that existed during the annotation processing run (cf. http://stackoverflow.com/a/18620582)
unit test tested exactly the opposite of the expected result
methods in source has not yet been emptied of their implementation methods have javadoc README for exo1 is done
add to use another way of writing unit test again the element and type package because when used outside the annotation processing that created it, the Element returned by the Elements class are not equal to the one that existed during the annotation processing run (cf. http://stackoverflow.com/a/18620582)
unit test tested exactly the opposite of the expected result
had to modify unit test to use same DontLookAtThisClass implementation as exo2 which actually supports Element#equals
in method extractAnnotations, one could use Elements#getAllAnnotationMirrors instead of Elements#getAnnotationMirrors, which is a wrong answer but was not visible with the provided sample Class
reste à faire :
|
Y'a encore des choses à faire ici ? |
dernier commentaire toujours d'actualité pour le nettoyage de l'historique, descopable pour ce qui est du README (sera couvert à l'oral) |
talk
Avec un AP, on peut étendre le compilateur => faire de la validation
exercices
exo 1 : validation d'annotations, erreur de compilationdéplacé du niveau 3exo 2 : et si vous affichiez la ligne à laquelle se trouve l'erreur ?déplacé au niveau 3exo 3 : et si vous n'affichiez qu'un warning ?déplacé du niveau 3exo 4 : valider qu'une valeur d'un paramètre d'une annotation est unique par packagemise en avant de l'aspect statefull de l'AP et de son cycle de viedéjà couvert par la solution bourrin pour traiter le bégaiement du processor au niveau 1javax.lang.model
Elements
,Types
,ElementFilters
, les visiteurs et comment les utiliserThe text was updated successfully, but these errors were encountered: