IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

Problème réflexivité avec invoke


Sujet :

Langage Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2012
    Messages : 25
    Points : 46
    Points
    46
    Par défaut Problème réflexivité avec invoke
    Bonjour à vous,
    j'essaie d’appeler une certaine méthode en fonction de son nom et de ses paramètres.
    Donc je récupère les entrés de l'utilisateur et je regarde les arguments passés sous forme de commande.
    Le problème vient au moment où l'argument est un int, j'ai un:
    java.lang.IllegalArgumentException: argument type mismatch
    Pourtant si l'argument est un String j'ai pas de problème:

    Ma méthode:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    public void exec(String command){
    		String[] commandSplitted = command.split(" ");
    		Object[] arg = {};
    		String method = null;
    		for(String keys : commands.keySet()){
    			Integer diff = 0;
    			String[] keysSplitted = keys.split(" ");
    			if(keys.contains("#")){ // SI LA COMMAND CONTIENT UN ARGUMENT
    				for(int k = 0; k < keys.length(); k++){
    					if(keys.charAt(k) == '#'){
    						diff++; // DETERMINATION DU NOMBRE DARGUMENT
    					}
    				}
    			}
    			boolean same = true;
    			for(int i = 0; i < commandSplitted.length-diff; i++){
    				if(commandSplitted.length-diff <= keysSplitted.length){
    					if(!keysSplitted[i].equals(commandSplitted[i])){
    						same = false; // COMMANDE USER NE CORRESPOND PAS A CELLE INDEX
    					}
    				}
    			}
    			if(same && commandSplitted.length == keysSplitted.length){ // BONNE COMMANDE
    				if(diff > 0){ // SI ARGUMENT CREATION DU TABLEAU DE PARAMETRE
    					arg = new Object[diff];
    					for(int d = 0; d < diff; d++){
    						arg[d] = commandSplitted[commandSplitted.length-d-1];
    					}
    				}
    				method = commands.get(keys); // ACCES AU NOM DE LA METHOD
    				break;
    			}
     
    		}
    		if(method != null){
    			for(int d = 0; d < server.getClass().getMethods().length; d++){
    				if(server.getClass().getMethods()[d].getName().equals(method)){
    					try {
    						server.getClass().getMethods()[d].invoke(server, arg); // ERREUR ICI SI ARGUMENT ENTIER
    					} catch (IllegalAccessException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (IllegalArgumentException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (InvocationTargetException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (SecurityException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    			}
    		}else{
    			ServerSessionMumble.logger.warning("bad command !");
    		}
    	}
    Merci d'avance de vos réponses

  2. #2
    Membre averti Avatar de toutgrego
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2013
    Messages
    217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2013
    Messages : 217
    Points : 350
    Points
    350
    Par défaut
    Quelle est l'erreur complète ? Quelle ligne au moins ? =)
    F*ck it ! Do it !

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2012
    Messages : 25
    Points : 46
    Points
    46
    Par défaut
    Je l'avais précisé en commentaire dans le code
    Sinon voilà l'erreur complète:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    java.lang.IllegalArgumentException: argument type mismatch
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at net.slayug.console.Command.exec(Command.java:61)
    	at net.slayug.console.Logger.run(Logger.java:34)
    	at net.slayug.ServerSessionMumble.ServerSessionMumble.<init>(ServerSessionMumble.java:34)
    	at net.slayug.ServerSessionMumble.ServerSessionMumble.main(ServerSessionMumble.java:26)

  4. #4
    Expert éminent sénior
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut
    Salut,

    Citation Envoyé par Gibob Voir le message
    Le problème vient au moment où l'argument est un int, j'ai un:
    Tu lui passes bien un objet de type Integer ?

    a++

  5. #5
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2012
    Messages : 25
    Points : 46
    Points
    46
    Par défaut
    Oui j'ai essayé avec un Integer ensuite, mais l'erreur est la même qu'avec un int, désolé de l’imprécision;

  6. #6
    Expert éminent sénior
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut
    Pourtant l'exception indique le contraire.

    Quel est la méthode appelée ?


    a++

  7. #7
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2012
    Messages : 25
    Points : 46
    Points
    46
    Par défaut
    Oui j'ai indiqué avec int au début, mais après j'ai testé avec un Integer mon code.
    Mais dans les deux cas j'ai un IllegalArgumentException, et j'ai adapté l'argument de ma méthode en fonction du int ou du Integer.
    Voilà la méthode appelée:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    public void listUserMumble(int id){
    		if(validId(id)){
    			this.mumbles.get(id).getUsersOnline();
    		}
    	}
    Mais je viens de penser du coup, puisque j'ajoute les arguments avec la ligne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    arg[d] = commandSplitted[commandSplitted.length-d-1];
    Le chiffre a un type de string dans le tableau d'objet passé en argument, il faut peut être que je le cast en int en vérifiant si c'est bien un nombre ?

  8. #8
    Membre averti Avatar de toutgrego
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2013
    Messages
    217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2013
    Messages : 217
    Points : 350
    Points
    350
    Par défaut
    Tu as vérifié que diff était supérieur à 0 ?
    F*ck it ! Do it !

  9. #9
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2012
    Messages : 25
    Points : 46
    Points
    46
    Par défaut
    Ah oui c'est ça c'est bon ça marche
    Du coup il le passé en String dans le tableau d'objet !
    J'ai juste rajouté:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    if(isInteger((String) arg[d])){
    							arg[d] = Integer.valueOf((String) arg[d]);
    						}
    après:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    arg[d] = commandSplitted[commandSplitted.length-d-1];
    Merci de l'aide

  10. #10
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut
    peux-tu nous indiquer la ligne 61
    dans le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    public void exec(String command){
    		String[] commandSplitted = command.split(" ");
    		Object[] arg = {};
    		String method = null;
    		for(String keys : commands.keySet()){
    			Integer diff = 0;
    			String[] keysSplitted = keys.split(" ");
    			if(keys.contains("#")){ // SI LA COMMAND CONTIENT UN ARGUMENT
    				for(int k = 0; k < keys.length(); k++){
    					if(keys.charAt(k) == '#'){
    						diff++; // DETERMINATION DU NOMBRE DARGUMENT
    					}
    				}
    			}
    			boolean same = true;
    			for(int i = 0; i < commandSplitted.length-diff; i++){
    				if(commandSplitted.length-diff <= keysSplitted.length){
    					if(!keysSplitted[i].equals(commandSplitted[i])){
    						same = false; // COMMANDE USER NE CORRESPOND PAS A CELLE INDEX
    					}
    				}
    			}
    			if(same && commandSplitted.length == keysSplitted.length){ // BONNE COMMANDE
    				if(diff > 0){ // SI ARGUMENT CREATION DU TABLEAU DE PARAMETRE
    					arg = new Object[diff];
    					for(int d = 0; d < diff; d++){
    						arg[d] = commandSplitted[commandSplitted.length-d-1];
    					}
    				}
    				method = commands.get(keys); // ACCES AU NOM DE LA METHOD
    				break;
    			}
     
    		}
    		if(method != null){
    			for(int d = 0; d < server.getClass().getMethods().length; d++){
    				if(server.getClass().getMethods()[d].getName().equals(method)){
    					try {
    						server.getClass().getMethods()[d].invoke(server, arg); // ERREUR ICI SI ARGUMENT ENTIER
    					} catch (IllegalAccessException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (IllegalArgumentException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (InvocationTargetException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (SecurityException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    			}
    		}else{
    			ServerSessionMumble.logger.warning("bad command !");
    		}
    	}
    merci.

  11. #11
    Membre averti Avatar de toutgrego
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2013
    Messages
    217
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2013
    Messages : 217
    Points : 350
    Points
    350
    Par défaut
    Citation Envoyé par jeffray03 Voir le message
    salut
    peux-tu nous indiquer la ligne 61
    dans le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    public void exec(String command){
    		String[] commandSplitted = command.split(" ");
    		Object[] arg = {};
    		String method = null;
    		for(String keys : commands.keySet()){
    			Integer diff = 0;
    			String[] keysSplitted = keys.split(" ");
    			if(keys.contains("#")){ // SI LA COMMAND CONTIENT UN ARGUMENT
    				for(int k = 0; k < keys.length(); k++){
    					if(keys.charAt(k) == '#'){
    						diff++; // DETERMINATION DU NOMBRE DARGUMENT
    					}
    				}
    			}
    			boolean same = true;
    			for(int i = 0; i < commandSplitted.length-diff; i++){
    				if(commandSplitted.length-diff <= keysSplitted.length){
    					if(!keysSplitted[i].equals(commandSplitted[i])){
    						same = false; // COMMANDE USER NE CORRESPOND PAS A CELLE INDEX
    					}
    				}
    			}
    			if(same && commandSplitted.length == keysSplitted.length){ // BONNE COMMANDE
    				if(diff > 0){ // SI ARGUMENT CREATION DU TABLEAU DE PARAMETRE
    					arg = new Object[diff];
    					for(int d = 0; d < diff; d++){
    						arg[d] = commandSplitted[commandSplitted.length-d-1];
    					}
    				}
    				method = commands.get(keys); // ACCES AU NOM DE LA METHOD
    				break;
    			}
     
    		}
    		if(method != null){
    			for(int d = 0; d < server.getClass().getMethods().length; d++){
    				if(server.getClass().getMethods()[d].getName().equals(method)){
    					try {
    						server.getClass().getMethods()[d].invoke(server, arg); // ERREUR ICI SI ARGUMENT ENTIER
    					} catch (IllegalAccessException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (IllegalArgumentException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (InvocationTargetException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (SecurityException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    			}
    		}else{
    			ServerSessionMumble.logger.warning("bad command !");
    		}
    	}
    merci.
    39
    F*ck it ! Do it !

  12. #12
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut essaies ceci voir:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    server.getClass().getMethods()[d].invoke(server, arg + "");

  13. #13
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    Citation Envoyé par Gibob Voir le message
    Du coup il le passé en String dans le tableau d'objet !
    Ben oui, puisque au départ ça vient d'un tableau de String -_-°

    Citation Envoyé par jeffray03 Voir le message
    salut essaies ceci voir:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    server.getClass().getMethods()[d].invoke(server, arg + "");
    - tu aurais pu tester avant de proposer -_-°.
    - Il a trouvé son erreur et comment la résoudre. Inutile de chercher ailleurs.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec invoke
    Par yaya0057 dans le forum Langage
    Réponses: 11
    Dernier message: 24/09/2008, 09h54
  2. Petit problème avec invoke
    Par bwarff dans le forum Windows Forms
    Réponses: 2
    Dernier message: 21/01/2008, 18h02
  3. Problème mémoire avec une dll par chargement dynamique
    Par widze19 dans le forum C++Builder
    Réponses: 6
    Dernier message: 15/12/2003, 13h20
  4. problèmes bizarres avec jdbc
    Par jaimepasteevy dans le forum PostgreSQL
    Réponses: 8
    Dernier message: 12/12/2003, 12h00
  5. problème JSP avec JBuilder et Weblogic 7
    Par viny dans le forum JBuilder
    Réponses: 2
    Dernier message: 24/04/2003, 08h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo