1

I have a cast issue whose generates a java.lang.ClassCastException exception:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.message.MessageFactory;

public class MyLogger extends org.apache.logging.log4j.core.Logger {

    MyLogger(LoggerContext context, String name, MessageFactory messageFactory) {
        super(context, name, messageFactory);
        // TODO Auto-generated constructor stub
    }

    public static MyLogger getLogger(String name) {
        org.apache.logging.log4j.core.Logger logger_ = (org.apache.logging.log4j.core.Logger) LogManager
            .getLogger(name);
        return (MyLogger) logger_;
    }
}

While this is the same principle as this one:

public class test {

    private static class A {

    }

    private static class B extends A {

    }

    public static void main(String [] args) {
        A a = new A();
        B b = new B();
        a = (A) b;
    }
}

Can someone give an explaination?

1
  • Are you sure LogManager.getLogger(name) returns an instance of MyLogger? Why would it? Commented Oct 30, 2014 at 14:12

2 Answers 2

1

LogManager.getlogger(name) returns a org.apache.logging.log4j object (the interface)

your are trying to store this object in a org.apache.logging.log4j.core.Logger object (the implementation), it won't work. Here is an example :

You can store a String in an Object :

String s = "foo";
Object o = s;

But you can't store an Object in a String :

Object o = new Object();
String s = o; // Won't compile

Here, the Object is org.apache.logging.log4j and the String is org.apache.logging.log4j.core.Logger

Here is a solution :

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext; 
import org.apache.logging.log4j.message.MessageFactory;

public class MyLogger extends org.apache.logging.log4j.core.Logger
{
    MyLogger(LoggerContext context, String name, MessageFactory messageFactory) {
        super(context, name, messageFactory);
    }

    public static Logger getLogger(String name) {
        Logger logger_ = LogManager.getLogger(name);
        return logger_;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you I have missed that this function returns the interface and not the object, it's clear now :)
0

In english please! (En anglais svp!)

In your code:

MyLogger extends Logger

means that you can cast MyLogger to Logger, but you cannot surely cast Logger to MyLogger.

For example if Dog extends Animal -> you can say that a Dog is an Animal but you cannot say that an Animal is always a Dog !

Comments

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.