-1

i have this class

class GameLoaderClass<V> {

V classValue = null;

Class<?> aClass;

ClassType classType;

public GameLoaderClass(ClassType classType, Class<?> aClass) {
    this.classType = classType;
    this.aClass = aClass;
}

public V getClassType(String name) {
    if (this.classType == ClassType.FIELD) {
        return this.classValue = aClass.getDeclaredField(name);
    } else
        return this.classValue = aClass.getMethod(name);

}

But i get this following error..

Required type: C Provided: Method

any way to fix it?, thanks

Mekdbd
  • 1
  • 1
  • Define class like `class GameLoaderClass` and use type casts `return this.classValue = (V)aClass.getMethod(name);` – onkar ruikar Mar 11 '21 at 03:59
  • @onkarruikar Yes casting it works, but is not safe, i want know if there is a way without cast – Mekdbd Mar 11 '21 at 04:08
  • @Mekdbd Do you want the `Method` object to be assigned to `classValue` or do you want the `classValue` to be assigned the value returned by the method named `name` ? – Gautham M Mar 11 '21 at 04:14
  • @GauthamM Yes i want the method object be assigned to classValue, but the object can be a field too, i only want to return the value as generic without casting. – Mekdbd Mar 11 '21 at 04:16
  • You have to assure the compiler that you are going to assign a assignable value to the variable. Hence the cast is needed. Use `@SuppressWarnings("unchecked")` on method `getClassType`. – onkar ruikar Mar 11 '21 at 04:23
  • This discussion has useful info https://stackoverflow.com/questions/262367/type-safety-unchecked-cast – onkar ruikar Mar 11 '21 at 04:30

1 Answers1

0

I would be better to use return type Object instead of using generics.

public Member getClassType(String name) {
    if (this.classType == ClassType.FIELD) {
        return aClass.getDeclaredField(name);
    } 
    return aClass.getMethod(name);
}

And in the place where you call the method you could do

Member obj = loader.getClassType("someName");
if(o instance of Field) {
    Field f = (Field) obj;
    // do the rest
} else /* Assuming Field or Method are the possible types of returned objects */ {
    Method m = (Method) obj;
    // do the rest
}

EDIT: Based on @Onkar's comment, it would be better to return an object of type Member instead of Object as both Method and Field implement Member.

Gautham M
  • 2,926
  • 2
  • 10
  • 27
  • 2
    `java.lang.reflect.Member` would be a better choice than Object – onkar ruikar Mar 11 '21 at 04:29
  • @onkarruikar yes that was a good info. I have updated – Gautham M Mar 11 '21 at 04:33
  • @onkarruikar Yes is a good solution, but i wanted to use generics so i can use that way Method method = new GameLoaderClass().getClassType(ClassType.Method); – Mekdbd Mar 11 '21 at 04:39
  • @Mekdbd You could still do that right? `Method method = (Method) new GameLoaderClass(ClassType.Method, SomeClass.class).getClassType("someName");` – Gautham M Mar 11 '21 at 04:55
  • @GauthamM i want use generics without casting – Mekdbd Mar 11 '21 at 05:02
  • @Mekdbd You need to use casting at one point either use casting inside the `GameLoader` class or in the place where you invoke the `getClassType` method. Otherwise I think it won't be possible. – Gautham M Mar 11 '21 at 05:16