wood burning stoves*
The moose likes Java in General and the fly likes Using Java annotation for Marker Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Java annotation for Marker Interface" Watch "Using Java annotation for Marker Interface" New topic
Author

Using Java annotation for Marker Interface

Sendy Yagambrum
Greenhorn

Joined: Oct 07, 2008
Posts: 3
Hi,

I've tried to use annotation to define a marker interface but I think I didn't quite get how to use it. Can someone please help?

I've defined a marker interface using annotation:

@Target(ElementType.TYPE)
public @Interface Searchable{}

and I have defined a class

@Searchable
public class Test{
...
}

and finally there's this piece of code:

ArrayList<Searchable> list = new ArrayList<Searchable>();
Test test = new Test();
list.add(test);

The problem is that the above code does not compile. The compiler does not resolve the type of Test to Searchable. I thought by just putting the annotation @Searchable would automatically make Test to be of that type. Am I wrong?

Thanks,

Sendy
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

With annotations, a class does not extend nor implement it. Therefore, there is no IS-A relationship between the Searchable annotation and the Test class. In fact, no class at all can ever implement the Searchable annotation, so nothing can have an IS-A relationship with Searchable (well, except sub annotations).

In your case, Searchable should be a regular interface instead.


Also, I don't think Searchable should be a marker interface at all. Once you have your List<Searchable>, what are you going to do with its contents? Searchable has no methods, so there is not a lot you can do without casting.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sendy Yagambrum
Greenhorn

Joined: Oct 07, 2008
Posts: 3
Ok I understand now. Thanks.

Well, I wanted to use Searchable in this way (for e.g):

public class Test1 implements Searchable
{
// class definition here
}

public class Test2 implements Searchable
{
//class definition here
}

public abstract class AbstractSearch
{
public abstract ArrayList<Searchable> getList();
public abstract void doSomeOperationOnList(ArrayList<Searchable> list);

public void startExecution()
{
ArrayList<Searchable> list = getList();
doSomeOperationOnList(list);
}
}

public class SearchTest1 extends AbstractSearch
{

public ArrayList<Searchable> getList()
{
ArrayList<Searchable> list = new ArrayList<Searchable>();

//execute some query here
while (resultset.next())
{
Test1 test1 = new Test1(resultset);
list.add(test1);
}
return list;
}

public void doSomeOperationOnList(ArrayList<Searchable> list)
{
// iterates over the list and casts each element to Test1
// does something on each Test1 object
}
}

// A similar class : SearchTest2
Sendy Yagambrum
Greenhorn

Joined: Oct 07, 2008
Posts: 3
That is to say I wanted to use casting. Wouldn't do any good without it, as you said
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Java annotation for Marker Interface