Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Threading Swing et Annotations

1 réponse
Avatar
ToOmS
Bonjour =E0 tous,

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 ?

Merci pour vos suggestions.

1 réponse

Avatar
ToOmS
Voilà où j'en suis :

<code>
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 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 {

}
</code>

<code>
import static com.sun.mirror.util.DeclarationVisitors.NO_OP;
import static
com.sun.mirror.util.DeclarationVisitors.getDeclarationScanner;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableCollection;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

import javax.swing.SwingUtilities;

import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.AnnotationProcessorFactory;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.declaration.TypeDeclaration;
import com.sun.mirror.util.SimpleDeclarationVisitor;

/**
* 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,