jeudi 19 septembre 2019

How to check the type of typed class ?>

I am passing the parameter in the method which has declared to be accepted parameter type class...other, after passing the parameter like String.class and Integer.class I want to know the type (class of) parameter which has been passed in this methods.

What parameter I received, I converted it into object and tring to checking the type but it is not working.

public void processVarargIntegers(String label, Class<?>... others) {

    System.out.println(String.format("processing %s arguments for %s", others.length, label));
    Arrays.asList(others).forEach(a -> {

        try {
            Object o = a;
            if (o instanceof Integer) {
                System.out.println(" >>>>>>>>>>>>> Integer");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}

public void processVarargIntegers(String label, Class<?>... others) {

    System.out.println(String.format("processing %s arguments for %s", others.length, label));
    Arrays.asList(others).forEach(a -> {

        try {
            Object o = a;
            if (o instanceof Integer) {
                System.out.println(" Integer");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}

if a is the instance of the integer System.out.println(" Integer"); should be executed





Aucun commentaire:

Enregistrer un commentaire