When I compile following code in Eclipse - there are no any errors.
There is only one warning:
SomeDerivedAbstractClass is a raw type. References to generic type
SomeDerivedAbstractClass should be parameterized
I tested this code in the newest Eclipse Indigo(3.7.1).
But there is following error when I tried to compile this code by javac:
SomeConcreateClass.java:1: ISomeBaseInterface cannot be inherited with different arguments: <java.lang.Object> and <>
public class SomeConcreateClass
^
1 error
I compiled this code suing Java 5 and Java 6. In both cases there is error.
What is wrong in this code?
public class SomeConcreateClass
extends SomeDerivedClass
implements ISomeInterface
{}
class SomeDerivedClass<T>
extends SomeAbstractClass<Object>
implements ISomeInterface
{
}
abstract class SomeAbstractClass<T>
implements ISomeBaseInterface<T>
{
}
interface ISomeInterface extends ISomeBaseInterface<Object>
{}
interface ISomeBaseInterface<T>
{
}
But following code does not compile either in Eclipse or by javac:
public class SomeConcreateClass
extends SomeAbstractClass
implements ISomeInterface
{}
abstract class SomeAbstractClass<T>
implements ISomeBaseInterface<Object>
{}
interface ISomeInterface extends ISomeBaseInterface<Object>
{}
interface ISomeBaseInterface<T>
{}
javac:
SomeConcreateClass.java:1: ISomeBaseInterface cannot be inherited with
different arguments: and <> public class
SomeConcreateClass
^ 1 error
Eclipse:
The interface ISomeBaseInterface cannot be implemented more than once
with different arguments: ISomeBaseInterface and
ISomeBaseInterface
So - is it a bug in Eclipse?
Is it the same bug as https://bugs.eclipse.org/bugs/show_bug.cgi?id=81824 ?
ONE MORE UPDATE:
This code compiles without errors both by javac and Eclipse:
public class SomeConcreateClass
extends SomeDerivedClass
implements ISomeInterface
{}
class SomeDerivedClass
extends SomeAbstractClass<Object>
implements ISomeInterface
{}
abstract class SomeAbstractClass<T>
implements ISomeBaseInterface<T>
{}
interface ISomeInterface extends ISomeBaseInterface<Object>
{}
interface ISomeBaseInterface<T>
{}
There is only one difference: SomeDerivedClass is not parameterized.
I do not understand how does this influence on ISomeBaseInterface.
AND ONE MORE UPDATE:
I checked code from the first example in IntellijIDEA - this IDE shows error.
But I think it uses different approach for compilation than Eclipse.
Something<> is NOT the same as Something<Object> (even though it might seem like that is reasonable).
SomeAbstractClass<T> implements ISomeBaseInterface<T>, and SomeAbstractClass<> implements ISomeBaseInterface<> So when you use SomeDerivedAbstractClass<> (in SomeConcreateClass), you're asking the class to implement both ISomeConcreateInterface (that is, ISomeBaseInterface<Object>) and ISomeBaseInterface<> at the same time, which it cannot do.
You might want to use SomeDerivedAbstractClass<?>, I think.
Related
I have a class like that:
public class ClassA implements InterfaceA {}
but when I compile this using Maven and decompile the file ClassA.class using JAD i got only this.
public class ClassA {}
In another project package this don't happenig...
Someone knows why this is happening?
I have the following class:
public class Blub extends AbstractPreloadDefinition<AddressmasterModel>
The javac compiler gives me the following error:
Error:(15, 79) java: type argument AddressmasterModel is not within
bounds of type-variable T
AbstractPreloadDefinition looks as follows:
abstract class AbstractPreloadDefinition<T extends PersistedEntity<?>> implements PreloadDefinition<T>
and AddressmasterModel looks as follows:
public abstract class AddressmasterModel<V extends VoucherModel> implements Serializable, Auditable, PersistedEntity<Integer>, Comparable<AddressmasterModel<V>>
So, AddressmasterModel implements PersistedEntity. Where's the error? I can't find it :/
I'm using IntelliJ 2017.1 and javac as compiler. If I switch to eclipse as compiler, this error is gone...
I think you need to extend PersistedEntity<Something> and not PersistedEntity. This works:
class Blub extends AbstractPreloadDefinition<AddressmasterModel<VoucherModel>>
Salve...
some Problem for me in Java. I have generated an Interface and an Class implements this Interface. In the Interface i have declared one Method... and in the Class i will Override this. But not works! But don`t why....
See my example Interface:
public interface IMyClass extends IInterfaceA<IInterfaceB> {
public List<IInterfaceB> getMethod(Integer id);
}
See now my Class File:
public class MyClass implements IMyClass {
#Override
public List<IInterfaceB> getMethod(Integer id) {
return anything;
}
}
Problem is... i think i make no mistake. And next - i surprise that i have another Interfaces and Classes with the same Logic - and it will work and make no Problems!
In my another Interfaces & Classes i don`t need to make the Class with implements the Interface an Abstract Class.
When i make an build... Jenkins will surprise me with this Message:
[ERROR] COMPILATION ERROR :
error: MyClass is not abstract and does not override abstract method getMethode() in IMyClass [ERROR]
error: method does not override or implement a method from a supertype [INFO] 2 errors [INFO]
Solved . The " solution " - was a build problem . After tens of Maven Clean Installs and re- builds he has the connection interface and class savvy and it goes... oh men ;)
This is from a complex build system. And I am looking for help to localize the problem.
It seems like I am hitting this problem: Covariant Return Type in Interface not compiling via Javac
My code compiles fine when I compile after cleaning all compiled code. But if I open AImpl.java and change one character (modify any string literal, which shouldn't generate any compile time error) and compile then I get following error:
AImpl.java:22: types BImpl<T> and BImpl<T> are incompatible; both define waitFor(org.openqa.selenium.support.ui.ExpectedCondition,java.lang.String,long,java.util.Collection), but with unrelated return types
public class AImpl<T extends AImpl<T>> extends BImpl<T> implements A<T> {
This is specific to AImpl.java ; changing any other code doesn't generate this vicious error.
BImpl.java is defined as:
public class BImpl<T extends BImpl<T>> extends CImpl<T> implements B<T> {
The erroring method waitFor is defined in CImpl as:
public T waitFor(ExpectedCondition<?> condition, String message, long timeOutInMillis, Collection<Class<? extends Throwable>> exceptions)
Problem:
I have an abstract class that external users can implement. These classes will be used in my application. However I do not know these classes beforehand. I would like to load them during runtime.
Current setup:
The current setup is to add the classes in a jar file and add them to the classpath when running the program. The program then searches the class path for any class files and looks whether they are not abstract, not an interface and whether they the abstract class is assignable from that class.
Add jar files to classpath
Start program
For every (class or class in jar file) in class path
check if abstract class is assignable from class and
class is not abstract not interface
Is there a library or easy way for doing this? I'm afraid this code might be a bit error-prone.
UPDATE:
Something like this?
private static <T> Class<? extends T> loadSubclasses(Class<T> superClass)
UPDATE:
I think this will do the trick
I want to find the classes in order to use them later on. I think this will do the trick (still have to test this code):
public static <T> Set<Class<? extends T>>
loadSubClasses(Class<T> superClass) {
return loadSubClasses(superClass,
ClasspathHelper.staticClassLoader());
}
public static <T> Set<Class<? extends T>> loadSubClasses(
Class<T> superClass, ClassLoader classLoader) {
Reflections reflections = new Reflections(classLoader);
return reflections.getSubTypesOf(superClass);
}