I have two inner classes and a method:
protected <A extends Obj1> void a(P<A> p)
{}
private static class P<Z extends Obj1>
{}
private static class Obj1
{}
It's compiling fine, but when I try in Eclipse Mars to change the method a's signature by giving it a second attribute this way
gives the following error:
Bound mismatch: The type A is not a valid substitute for the bounded
parameter <Z extends Test.Obj1> of the type Test.P<Z>
Now actually I have two questions about this. Why is this behavioural difference and what did I do wrong?
Strangely when I modify manually a to this:
protected <A extends Obj1> void a(P<? extends A> p)
{}
and try to change the signature the previously decribed manner I don't get that error. I have a feeling that this may be a bug either in Java or Eclipse, but don't really know where to start seaching. so I give you their versions too if it helps:
Eclipse Mars.2 (4.5.2)
Java 1.8.0.77
UPDATE:
So I've tried more different versions of Eclipse and Java 8, but the result is the same.
Related
I seems to be running into a compilation error with a generic interface. The goal is to create an interface for use in my test classes the defines the common methods of a domain class and a generated DTO class. I did some digging and none of the answers I have found so far seem applicable to what I am trying to do. Most of the answers either refer back to this: class not within type bounds or they suggest using wildcards which is not an option here.
When I try to compile, I get the following error:
[uberCompile] BrandDto.java:10: error: type argument T#1 is not within bounds of type-variable T#2
[uberCompile] public class BrandDto<T extends SubCollectionDto> implements IBrand<T> {
[uberCompile]
[uberCompile] where T#1,T#2 are type-variables:
[uberCompile] T#1 extends SubCollectionDto declared in class BrandDto
[uberCompile] T#2 extends ISubCollection declared in interface IBrand
My class structure looks like this:
interface IBrand<T extends ISubCollection>
interface ISubCollection
class Brand<T extends SubCollection>
extends Entity
implements IBrand<T>
class SubCollection
extends Entity
implements ISubCollection
The generated DTO classes look like:
class SubCollectionDto
implements ISubCollection
class BrandDto<T extends SubCollectionDto>
implements IBrand<T>
I'm really racking my brain trying to understand what I am doing wrong here. Any help would be greatly appreciated.
I think I finally figured it out. It was actually a classpath issue that had nothing to do with the generics. When I originally set up the DTO classes they were under the 'src' directory. I later moved them into a different directory, but apparently the old folder never got removed. So eclipse was looking at the current, correct set of files while ant was looking at the old directory. No idea why that was causing the error message I saw, but it is working now.
The code my Teacher gave me has an error in it. I get this error on MyArrayList:
Duplicate methods named spliterator with the parameters () and () are
inherited from the types Collection and Iterable
Why does it not work for me but it works for other students?
import java.util.*;
public class MyArrayList<E> extends AbstractList<E>
{
private E[] data_store = (E[])new Object[50];
private int how_many = 0;
The only reason I can think of is the mismatch of Java version that the code was compiled with what you have setup in your environment.
Another reason could be that you are using an older version of IDE that does not support Java 8. For example, eclipse versions prior to Luna do not fully support Java 8 (Others correct me here if I got my facts wrong).
I have following code:
class Key{
--Some Implementation
}
class A{
public void grpMap(ConcurrentMap<Key,List<Key> keyList){
--Some Implementation
}
}
public class B extends A{
--Edited
#Override -- [Made "O" capital case after kocko's reply]
public void grpMap(ConcurrentMap<Key,List<Key> keyList){
--Some Implementation
}
}
With above code i get following error in class B
The method grpMap(ConcurrentMap) of type B must override
or implement a supertype method
My problem is i can't change the way classes key, B and C are declared as these are legacy classes.
Any suggestion on how to get rid of this error?
EDIT---
JDK version used is 1.6.43
I am using eclipse which will auto generate annotations.
The annotation
#override
should be
#Override
↑
Also, check you Java version in the IDE - it should be 1.6, or newer, in order to get rid of the error.
Open Project properties -> Java compiler -> Set compliance level to 1.6 -> OK.
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)
I have an issue where NoClasDefFoundError is being thrown. It puzzles me since I am using interfaces, and no class definition should be available. I have read through some posts which point to Classpath, but I don't believe that to be the issue here (although I may be wrong). I am using NetBeans 6.9.1 IDE.
I have created a sample setup to reproduce the issue. Four projects: Interfaces, Objects, Locator and Consumer. Below you will find the implementations.
At runtime consumer coplains about missing SomeObject implementation, which it should not be aware of since it is accepting interface.
Exception in thread "main"
java.lang.NoClassDefFoundError:
objects/SomeObject
What am I missing?
package interfaces;
public interface ISomeInterface { }
package objects;
import interfaces.ISomeInterface;
public class SomeObject implements ISomeInterface{ }
package locator;
import interfaces.ISomeInterface;
import objects.SomeObject;
public class Locator { public static ISomeInterface LocateImplementation() { return new SomeObject(); }}
package consumer;
import interfaces.ISomeInterface;
import locator.Locator;
public class Main { public static void main(String[] args) { ISomeInterface object = Locator.LocateImplementation(); }}
You can get a NoClassDefFoundError exception with interfaces just as you can with classes. Consider the "Class" in the name of the exception to be the .class file that is generated from compiling a class or interface, not a Java class.
This is saying that the class/interface objects.SomeObject isn't visible on your classpath. Check the location of that .class file and ensure that it's on your classpath - if you're positive it's there, give us some screen shots or something that might help to debug the problem.
Think of NoClassDefFoundError as a runtime linkage problem. JRE loaded one class (or an interface) and it references another class (or an interface), but that referenced class isn't found.
The only way this can happen if you have packaging/classpath issues such that your runtime environment doesn't reflect how things are at build time.
If you are launching this from IDE, make sure that you aren't ignoring any errors and launching anyway. Some classes will not be generated that way.
Usually I run into these problems not when a class is missing, but when there is an error in the static initializers.
Try running your code in a debugger, and set the exception breakpoint to break when any exception is thrown, whether caught or not. I bet you have an uncaught exception in the static initializer for some reason.
In the locateImplementation() method you are returning "new SomeObject()",
JVM needs to have its definition when called. I think it is missing.
You should check if your SomeObject class is in class path because -
Well the JVM will be running the below code -
ISomeInterface object = Locator.LocateImplementation();
and when it does that it will call Locator.LocateImplementation(). This code internally tries to instantiate your SomeObject class which it does not find in the classpath.
So your below understanding
It puzzles me since I am using
interfaces, and no class definition
should be available.
Is not really valid.
Any Interface must be declared inside class
public class Calbacks {
public interface IBaseFragmentInterface {
void NotifyMainActivity();
}
}