0

I am using a common RestTemplate to submit a request to Remote server. Where I am first making a login request, fetching the webToken and then making subsequent request using that token.

In this common RestTemplate, I am logging the request and response. But I want to avert logging the username and password in logging. Following is the example which I am trying to do.

 package reflection;

public class TypeCheck {

    public static void main(String[] args) {

        Object animal = new Animal("Elephant");     
        classType(animal.getClass());
        //Passing Class Type 
         // Output - Unmatched  class reflection.Animal
    }

    private static void classType(Object type) {
        if(type instanceof Animal) {
            System.out.println("Matched  "+ type);
        }

        System.out.println("Unmatched  " + type);
    }

 }

 class Animal {

    String name;

    public Animal(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

 }

Now, I want to extract the Class Type (i.e. reflection.Animal) from type parameter. I tried for type.getClass().getSimpleName() and some other methods but not getting better way to do it.

P.S. - Currently I am trying one clumsy way (as I think) to fetch this. But don't know if this is correct or we have some better way to achieve this.

if(type.toString().contains("Animal")) {
            System.out.println("Matched "+ type);
        }

Thanks in advance.

1
  • 1
    Try to pass animal instead of animal.getClass() to the classType method. Commented Feb 28, 2020 at 8:03

1 Answer 1

1

You can simply check the same with istanceof if you pass the object itself to the classType method rather than the class of it.

    public static void main(String[] args) {
        Object animal = new Animal("Elephant");
        classType(animal);
    }

    private static void classType(Object type) {
        if (type instanceof Animal) {
            System.out.println("Matched  " + type);
        } else {
            System.out.println("Unmatched  " + type);
        }
    }

Or else if you want to check the same with class, you can try the following.

    public static void main(String[] args) {
        Object animal = new Animal("Elephant");
        classType(animal.getClass());
    }

    private static <T> void classType(Class<T> tClass) {
        if (tClass.getName().equals("Animal")) {
            System.out.println("Matched  " + tClass.getName());
        } else {
            System.out.println("Unmatched  " + tClass.getName());
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for prompt response. The option one is not possible as original API gives .class as an argument . So , the option 2, is fine with me and it is close to the attempt which I am also making. Many thanks it is working for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.