Pour la gestion du threading de mon application, cf.
http://gfx.developpez.com/tutoriel/java/swing/swing-threading/, je
voudrais faire une annotation pour les m=E9thodes du type de l'exemple
5 ; l'id=E9e =E9tant d'encapsuler le traitement de chaque fonction lui-
m=EAme dans un Runnable et d'effectuer le test de lancement ad'hoc.
Est-ce possible ? Puis-je, par exemple, impl=E9menter dans l'annotation
une m=E9thode qui serait ex=E9cut=E9e au moment de l'appel =E0 une m=E9thode
annot=E9e, du type code =3D new Runnable() { ... run()
{ this.getAnnotatedMethod().execute(); }...
Ou est-ce plut=F4t de l'AOP qui ne correspond pas aux annotations ?
/** * Cette annotation permet de gérer facilement le threading des applications * Swing qui, pour fonctionner de manière souple, doivent "inscrire" leurs * traitements coûteux dans une excéution asynchrone centralisée. * @see http://gfx.developpez.com/tutoriel/java/swing/swing-threading/ * * @see http://adiguba.developpez.com/tutoriels/java/tiger/annotations/ * @see http://gfx.developpez.com/tutoriel/java/annotation/ * * @see http://java.sun.com/j2se/1.5.0/docs/guide/apt/index.html * * @author Thomas Escolan * @version 20070712 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SwingThreading {
/** * This class is used to run an annotation processor that threads * swing application methods. * @see http://java.sun.com/j2se/1.5.0/docs/guide/apt/GettingStarted.html * @author Thomas Escolan */ public class SwingThreadingApf implements AnnotationProcessorFactory { private static final Collection<String> supportedAnnotations =
unmodifiableCollection(Arrays.asList("fr.tooms.gui.SwingThreading")); // No supported options private static final Collection<String> supportedOptions = emptySet();
public Collection<String> supportedAnnotationTypes() { return supportedAnnotations; } public Collection<String> supportedOptions() { return supportedOptions; }
public AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env) { return new SwingThreadingAp(env); }
private static class SwingThreadingAp implements AnnotationProcessor { private final AnnotationProcessorEnvironment env; SwingThreadingAp(AnnotationProcessorEnvironment env) { this.env = env; }
public void process() { for (TypeDeclaration typeDecl : env.getSpecifiedTypeDeclarations()) typeDecl.accept(getDeclarationScanner(new SwingThreadingVisitor(), NO_OP)); }
private static class SwingThreadingVisitor extends SimpleDeclarationVisitor { @Override public void visitMethodDeclaration(MethodDeclaration m) { Runnable code = new Runnable() { public void run() { //TODO } }; if (SwingUtilities.isEventDispatchThread()) { code.run(); } else { SwingUtilities.invokeLater(code); } } @Override public void visitClassDeclaration(ClassDeclaration d) { System.out.println(d.getQualifiedName()); //DEBUG } } } } </code>
Mais je ne vois rien, a priori, pour éxecuter ma méthode ou injecter mon code. Dois-je réécrire la classe et recompiler ? Où trouver un tutoriel, SVP ? Merci d'avance,
/**
* Cette annotation permet de gérer facilement le threading des
applications
* Swing qui, pour fonctionner de manière souple, doivent "inscrire"
leurs
* traitements coûteux dans une excéution asynchrone centralisée.
* @see http://gfx.developpez.com/tutoriel/java/swing/swing-threading/
*
* @see http://adiguba.developpez.com/tutoriels/java/tiger/annotations/
* @see http://gfx.developpez.com/tutoriel/java/annotation/
*
* @see http://java.sun.com/j2se/1.5.0/docs/guide/apt/index.html
*
* @author Thomas Escolan
* @version 20070712
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SwingThreading {
/**
* This class is used to run an annotation processor that threads
* swing application methods.
* @see http://java.sun.com/j2se/1.5.0/docs/guide/apt/GettingStarted.html
* @author Thomas Escolan
*/
public class SwingThreadingApf implements AnnotationProcessorFactory {
private static final Collection<String> supportedAnnotations =
unmodifiableCollection(Arrays.asList("fr.tooms.gui.SwingThreading"));
// No supported options
private static final Collection<String> supportedOptions =
emptySet();
public Collection<String> supportedAnnotationTypes() {
return supportedAnnotations;
}
public Collection<String> supportedOptions() {
return supportedOptions;
}
public AnnotationProcessor getProcessorFor(
Set<AnnotationTypeDeclaration> atds,
AnnotationProcessorEnvironment env) {
return new SwingThreadingAp(env);
}
private static class SwingThreadingAp implements AnnotationProcessor
{
private final AnnotationProcessorEnvironment env;
SwingThreadingAp(AnnotationProcessorEnvironment env) {
this.env = env;
}
public void process() {
for (TypeDeclaration typeDecl : env.getSpecifiedTypeDeclarations())
typeDecl.accept(getDeclarationScanner(new SwingThreadingVisitor(),
NO_OP));
}
private static class SwingThreadingVisitor extends
SimpleDeclarationVisitor {
@Override
public void visitMethodDeclaration(MethodDeclaration m) {
Runnable code = new Runnable() {
public void run() {
//TODO
}
};
if (SwingUtilities.isEventDispatchThread()) {
code.run();
} else {
SwingUtilities.invokeLater(code);
}
}
@Override
public void visitClassDeclaration(ClassDeclaration d) {
System.out.println(d.getQualifiedName()); //DEBUG
}
}
}
}
</code>
Mais je ne vois rien, a priori, pour éxecuter ma méthode ou injecter
mon code. Dois-je réécrire la classe et recompiler ? Où trouver un
tutoriel, SVP ?
Merci d'avance,
/** * Cette annotation permet de gérer facilement le threading des applications * Swing qui, pour fonctionner de manière souple, doivent "inscrire" leurs * traitements coûteux dans une excéution asynchrone centralisée. * @see http://gfx.developpez.com/tutoriel/java/swing/swing-threading/ * * @see http://adiguba.developpez.com/tutoriels/java/tiger/annotations/ * @see http://gfx.developpez.com/tutoriel/java/annotation/ * * @see http://java.sun.com/j2se/1.5.0/docs/guide/apt/index.html * * @author Thomas Escolan * @version 20070712 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SwingThreading {
/** * This class is used to run an annotation processor that threads * swing application methods. * @see http://java.sun.com/j2se/1.5.0/docs/guide/apt/GettingStarted.html * @author Thomas Escolan */ public class SwingThreadingApf implements AnnotationProcessorFactory { private static final Collection<String> supportedAnnotations =
unmodifiableCollection(Arrays.asList("fr.tooms.gui.SwingThreading")); // No supported options private static final Collection<String> supportedOptions = emptySet();
public Collection<String> supportedAnnotationTypes() { return supportedAnnotations; } public Collection<String> supportedOptions() { return supportedOptions; }
public AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env) { return new SwingThreadingAp(env); }
private static class SwingThreadingAp implements AnnotationProcessor { private final AnnotationProcessorEnvironment env; SwingThreadingAp(AnnotationProcessorEnvironment env) { this.env = env; }
public void process() { for (TypeDeclaration typeDecl : env.getSpecifiedTypeDeclarations()) typeDecl.accept(getDeclarationScanner(new SwingThreadingVisitor(), NO_OP)); }
private static class SwingThreadingVisitor extends SimpleDeclarationVisitor { @Override public void visitMethodDeclaration(MethodDeclaration m) { Runnable code = new Runnable() { public void run() { //TODO } }; if (SwingUtilities.isEventDispatchThread()) { code.run(); } else { SwingUtilities.invokeLater(code); } } @Override public void visitClassDeclaration(ClassDeclaration d) { System.out.println(d.getQualifiedName()); //DEBUG } } } } </code>
Mais je ne vois rien, a priori, pour éxecuter ma méthode ou injecter mon code. Dois-je réécrire la classe et recompiler ? Où trouver un tutoriel, SVP ? Merci d'avance,