Billy Brown Billy Brown - 18 days ago 8
Java Question

Processing error: expecting "class", found 'Set' in implemented interface method

I have the following Entity-Component-System interface to represent a System (in

ECSSystem.java
):

interface ECSSystem extends Consumer<Set<Entity>> {
public void accept(Set<Entity> entities);
public Set<Class<? extends Component>> required();
}


where
Component
is an empty interface and
Entity
is a class.

I am then implementing the interface with the following code (in
PlayerInputSystem.pde
):

class PlayerInputSystem implements System {
public void accept(Set<Entity> entities) {
// implementation here
}

public Set<Class<? extends Component>> required() {
final Set<Class<? extends Component>> reqs = new HashSet<>();
// implementation here
return reqs;
}
}


For the line
final Set<Class<? extends Component>> reqs = new HashSet<>();
in
PlayerInputSystem.pde
, I get the following error:

expecting "class", found 'Set'


Is this just Processing (3.2.3) not understanding, or overriding the
Class
object? How can I fix it, or do I need to work around this?

EDIT: here is a minimum viable test example that fails with the same error:

import java.util.function.Consumer;
import java.util.HashSet;
import java.util.Set;

void setup() {

}

void draw() {

}

interface Component {

}

class Entity {

}

interface ECSSystem extends Consumer<Set<Entity>> {
public void accept(Set<Entity> entities);
public Set<Class<? extends Component>> required();
}

class PlayerInputSystem implements ECSSystem {
public void accept(Set<Entity> entities) {

}

public Set<Class<? extends Component>> required() {
final Set<Class<? extends Component>> reqs = new HashSet<>(); // error here
return reqs;
}
}


Removing the
final
keyword where the error is reported then gives a new error:

unexpected token: ?

Answer

Please try to post a [mcve] instead of a disconnected snippet. The code you posted contains a bunch of errors because you haven't included all of the classes we need. But instead of simply adding those classes, try to narrow your problem down to as few lines as possible. And like Eran said, you should really stay away from class names that are already being used.

Here is my attempt at getting around the classes you haven't provided, by just using Object instead. This compiles fine:

import java.util.Set;
import java.util.HashSet;
import java.awt.Component;

void draw(){

}

public interface System{
  public void accept(Set<Object> entities);
  public Set<Class<? extends Component>> required();
}

class PlayerInputSystem implements System {
  public void accept(Set<Object> entities) {
    // implementation here
  }

  public Set<Class<? extends Component>> required() {
    final Set<Class<? extends Component>> reqs = new HashSet<>();
    // implementation here
    return reqs;
  }
}

Edit: The real issue you have is that Processing doesn't support the diamond operator, which is what you're trying to use on this line:

final Set<Class<? extends Component>> reqs = new HashSet<>();

This is a known issue here.