[Novice Q] How to unpack anonymous Inner Class?

24 views
Skip to first unread message

gorgeousSuccessfulWealthy

unread,
Jul 21, 2021, 10:10:54 AM7/21/21
to cqengine-discuss

Good day lovely readers,

I have not worked extensively with inner anonymous classes but I am trying to simplify the following lines of code in someone's package examples.introduction.novice.simpler.model;


public class Person {
    public final String name;
    
    
    // -------------------------- Attributes --------------------------
    public static final Attribute<Person, String> NAME = new SimpleAttribute<Person, String>("name") {
        public String getValue(Person person, QueryOptions queryOptions) { return person.name; }
    };

}

So to unpack the above & make it easier to read I rewrote it so (please correct me if I am wrong):


public class Person {
    public final String name;
    
    // TODO: Simplify
    // -------------------------- Attributes --------------------------
    public static final Attribute<Person, String> NAME = new Name<Person, String>("name") ;

}

And ofcourse I created the Name class so :

public class Name<O, A> extends SimpleAttribute<O, A> {

public Name(String attributeName) {

super(attributeName);

// TODO Auto-generated constructor stub

}

public String getValue(Person person, QueryOptions queryOptions) { return person.name; }

@Override

public A getValue(O arg0, QueryOptions arg1) {

// TODO Auto-generated method stub

return null;

}

}

However, I am getting an error so :

Exception in thread "main" java.lang.ExceptionInInitializerError at examples.introduction.novice.simpler.model.Introduction.main(Introduction.java:17) Caused by: java.lang.IllegalStateException: Attribute 'name' (class examples.introduction.novice.simpler.model.Name) is invalid, cannot read generic type information from it. Attributes should typically EITHER be declared in code with generic type information as a (possibly anonymous) subclass of one of the provided attribute types, OR you can use a constructor of the attribute which allows the types to be specified manually. at com.googlecode.cqengine.attribute.support.AbstractAttribute.readGenericObjectType(AbstractAttribute.java:139) at com.googlecode.cqengine.attribute.support.AbstractAttribute.<init>(AbstractAttribute.java:43) at com.googlecode.cqengine.attribute.SimpleAttribute.<init>(SimpleAttribute.java:55) at examples.introduction.novice.simpler.model.Name.<init>(Name.java:11) at examples.introduction.novice.simpler.model.Person.<clinit>(Person.java:23) ... 1 more

The sandbox link is :

https://codesandbox.io/s/sharp-bogdan-2viuc?file=/src/examples/introduction/novice/simpler/model/Person.java

The link to the main program running it is:

https://codesandbox.io/s/sharp-bogdan-2viuc?file=/src/examples/introduction/novice/simpler/model/Introduction.java

Apologies, but unsure how to format code in google groups. 

Any help would be appreciated.

Thanks in advance.

Reply all
Reply to author
Forward
0 new messages