2

I need to make a generic class with implements cloneable, and to accomplish this I needed to do a clone method of the class and another method to get this method. The teacher passed it in the room, but is not working and giving Method error. What should I do?

  public class Deposito <X> implements Cloneable {
       public Object clone () {
           Deposito ret=null;

           try {
               ret = new Deposito (this);
           }
           catch (Exception erro) { 
           } 
        return ret;
      }

      private X meuCloneDeX (X x) {
          X ret = null;

          try {
              Class<?> classe = x.getClass();
              Class<?>[] tipoDoParametroFormal = null; 
              Method metodo = classe.getMethod ("clone", tipoDoParametroFormal);
              Object[] parametroReal = null;// pq clone tem 0 parametros
              ret = ((X)metodo.invoke (x, parametroReal));
          }
          catch (NoSuchMethodException erro)
          {}
          catch (InvocationTargetException erro)
          {}
          catch (IllegalAccessException erro)
          {}

          return ret;
       }
}

Error - cannot find symbol:

Method metodo = classe.getMethod ("clone", tipoDoParametroFormal);

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
lisa15x
  • 23
  • 3

1 Answers1

0

Maybe you should use Class#getDeclaredMethod. Cuz u just "declared" your clone method...and it's better if just ignore second parameter of this method when it's NULL.

Troy Liu
  • 81
  • 4