You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by abosch <ar...@gmx.de> on 2016/07/31 22:08:33 UTC

Undeclared type variable: T when calling a method from a class with a trait from java

Heyho,

I have the following problem, maybe someone can help me here, thx :):

I have a trait, which adds recursive behaviour to a class like a
DirectoryEntity.

@CompileStatic
trait RecursiveEntity<T extends RecursiveEntity<T>> extends Entity {
   T successor;
   final List<T> subEntities = new ArrayList<T>();

   void setParent(final T newParent) {
      if (successor) {
         successor.removeSubEntity(this as T);
      }
      successor = newParent;
      if (newParent) {
         newParent.addSubEntity(this as T);
      }
   }

   void addSubEntity(final T newEntity) {
      Validate.notNull(newEntity)
      def parent = newEntity.successor;
      if (parent) {
         parent.removeSubEntity(newEntity);
      }
      newEntity.successor = this as T;
      subEntities.add(newEntity);
   }

   void removeSubEntity(final T toRemove) {
      Validate.notNull(toRemove)
      if (toRemove.successor == this) {
         subEntities.remove(toRemove);
         toRemove.successor = null;
      }
   }
...
}

@CompileStatic
class DirectoryEntity implements RecursiveDirectoryEntity {

   @Delegate
   Container<FileEntity> container = new Container<>()
...
}

If I construct a DirectoryEntity from within Java code and use any method, I
get following Error message: Error:(13, 24) java: cannot access
de.unibremen.st.entitygraph.traits.RecursiveEntity$Trait$FieldHelper   bad
class file:
/home/artur/Arbeit/tools/entitygraph/build/classes/main/de/unibremen/st/entitygraph/traits/RecursiveEntity$Trait$FieldHelper.class    
undeclared type variable: T     Please remove or make sure it appears in the
correct subdirectory of the classpath. 

Is there a solution to my problem? I already had to remove multiple trait
usage and introduce Delegates because java can't handle it.

 To just use groovy code is not an option as my coworker's project is plain
java...
 Big thx you and greets! Artur 



--
View this message in context: http://groovy.329449.n5.nabble.com/Undeclared-type-variable-T-when-calling-a-method-from-a-class-with-a-trait-from-java-tp5734382.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: Undeclared type variable: T when calling a method from a class with a trait from java

Posted by abosch <ar...@gmx.de>.
The groovy version is 2.4.6 and java is 1.8.0_92 (Oracle).

If i use more than one trait in a class and call its methods from within
java, I get the same error message.

Then i removed multiple traits and only used one with @Delegate. This is
possible in Java, when there is no generic type in the trait e.g.:

@CompileStatic
trait RecursiveDirectoryEntity extends Entity {

	DirectoryEntity successor;
	final List<DirectoryEntity> subEntities = new ArrayList<>();
...
}

This is allowed, but that not:

trait RecursiveEntity<T extends RecursiveEntity<T>> extends Entity {
	T successor;
	final List<T> subEntities = new ArrayList<T>();
...
}

greets



--
View this message in context: http://groovy.329449.n5.nabble.com/Undeclared-type-variable-T-when-calling-a-method-from-a-class-with-a-trait-from-java-tp5734382p5734393.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: Undeclared type variable: T when calling a method from a class with a trait from java

Posted by Jochen Theodorou <bl...@gmx.org>.
that sounds like a bug in the code generated by the groovy compiler for 
the one of the fields you use. Though I remember a bug like this being 
fixed... Can you ell us what groovy version you use? Also... that error 
message from Java is strangely descriptive. Normally you end up with a 
some very cryptic stuff from inside the jdk... could you tell me what 
Java version you use as well?

bye Jochen

On 01.08.2016 00:08, abosch wrote:
> Heyho,
>
> I have the following problem, maybe someone can help me here, thx :):
>
> I have a trait, which adds recursive behaviour to a class like a
> DirectoryEntity.
>
> @CompileStatic
> trait RecursiveEntity<T extends RecursiveEntity<T>> extends Entity {
>     T successor;
>     final List<T> subEntities = new ArrayList<T>();
>
>     void setParent(final T newParent) {
>        if (successor) {
>           successor.removeSubEntity(this as T);
>        }
>        successor = newParent;
>        if (newParent) {
>           newParent.addSubEntity(this as T);
>        }
>     }
>
>     void addSubEntity(final T newEntity) {
>        Validate.notNull(newEntity)
>        def parent = newEntity.successor;
>        if (parent) {
>           parent.removeSubEntity(newEntity);
>        }
>        newEntity.successor = this as T;
>        subEntities.add(newEntity);
>     }
>
>     void removeSubEntity(final T toRemove) {
>        Validate.notNull(toRemove)
>        if (toRemove.successor == this) {
>           subEntities.remove(toRemove);
>           toRemove.successor = null;
>        }
>     }
> ...
> }
>
> @CompileStatic
> class DirectoryEntity implements RecursiveDirectoryEntity {
>
>     @Delegate
>     Container<FileEntity> container = new Container<>()
> ...
> }
>
> If I construct a DirectoryEntity from within Java code and use any method, I
> get following Error message: Error:(13, 24) java: cannot access
> de.unibremen.st.entitygraph.traits.RecursiveEntity$Trait$FieldHelper   bad
> class file:
> /home/artur/Arbeit/tools/entitygraph/build/classes/main/de/unibremen/st/entitygraph/traits/RecursiveEntity$Trait$FieldHelper.class
> undeclared type variable: T     Please remove or make sure it appears in the
> correct subdirectory of the classpath.
>
> Is there a solution to my problem? I already had to remove multiple trait
> usage and introduce Delegates because java can't handle it.
>
>   To just use groovy code is not an option as my coworker's project is plain
> java...
>   Big thx you and greets! Artur
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Undeclared-type-variable-T-when-calling-a-method-from-a-class-with-a-trait-from-java-tp5734382.html
> Sent from the Groovy Users mailing list archive at Nabble.com.
>