0

I wonder if you can see what is wrong with my code.

First I have this class

package de.daisi.async.infrastructure.component.event;
import static de.daisi.async.infrastructure.component.event.CType.*;

public class TemperatureEvent implements IEvent {

private static final long serialVersionUID = 1L;
private CType cType = ONEP_ONEC;

public String toString(){
    return "TemperatureEvent";
}


public CType getcType() {
    return cType;
}
}

Via java reflection I want to get the CType value (ONEP_ONEC)

package de.daisi.async.infrastructure.comunicationtype;
import de.daisi.async.infrastructure.component.event.*;
import java.lang.reflect.Method;

public class CheckComType {

public CType checkType(Class<? extends IEvent> eventClass) {
    System.out.println("Check communcationType: " + eventClass);

    CType cType = null;

    try {
        System.out.println("---In Try---");
        Class cls = (Class) eventClass;
        System.out.println("cls: " + cls);

        Method method = cls.getDeclaredMethod("getcType");
        System.out.println("method: " + method);
        Object instance = cls.newInstance();

        cType = (CType) method.invoke(instance);
        System.out.println("instance: " + instance);
        System.out.println("cType: " + cType);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return cType;

}

public static void main(String... args){
    CheckComType type = new CheckComType();
    CType testType = type.checkType(TemperatureEvent.class);
    System.out.println("testType: " + testType);

}

}

The testType result is null and I got a ClassCastException

java.lang.ClassCastException: de.daisi.async.infrastructure.component.event.CType cannot be cast to de.daisi.async.infrastructure.comunicationtype.CType at de.daisi.async.infrastructure.comunicationtype.CheckComType.checkType at de.daisi.async.infrastructure.comunicationtype.CheckComType.main

Any suggestions? Thank you in advance

7
  • What do your imports look like in CheckComType and TemperatureEvent ? Commented Aug 10, 2016 at 17:34
  • 1
    You have two CType classes in different packages - de.daisi.async.infrastructure.component.event.CType and de.daisi.async.infrastructure.comunicationtype.CType. Make sure you don't mix them. Unfortunately, it's hard to see as you didn't include the imports. Commented Aug 10, 2016 at 17:36
  • 1
    Dont put more information into comments. Update your question instead! Commented Aug 10, 2016 at 17:45
  • Because full class names is what counts. Packages are essentially namespaces, classes that belong to different packages are different classes even if their names are identical. en.wikipedia.org/wiki/Java_package Commented Aug 10, 2016 at 17:46
  • I just added the packages Commented Aug 10, 2016 at 17:46

1 Answer 1

1

You apparently have two different CType classes, one in the de.daisi.async.infrastructure.component.event package and the other in de.daisi.async.infrastructure.comunicationtype. Since you don't explicitly reference de.daisi.async.infrastructure.component.event.CType in CheckComType, the class from the same package (namely de.daisi.async.infrastructure.comunicationtype.CType) is used.

In Java, full class names is what counts. Packages are essentially namespaces, classes that belong to different packages are different classes even if their names are identical.

de.daisi.async.infrastructure.component.event.CType cType = null;

try {

    //...
    cType = (de.daisi.async.infrastructure.component.event.CType) method.invoke(instance);
}

and so on.

Or just explicitly import de.daisi.async.infrastructure.component.event.CType in CheckComType if you don't intend to use both CType's in the same class.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Now is solved, I didn't realize that i had two CType classes ... :)
Great! You can mark it as answered then. I hope this also answers your question (in a comment) about how the imports should be declared.

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.