开发者

Java Generics Creating Array from Class

I have a hierarchy where Square, Triangle and Circle all extend from Shape. I have a working method:

public void someMethod() {
   File file = new File("File_with_squares");
   ThirdPartyClass foo = new ThirdPartyClass();
   Square[] squares = foo.someMajicMethod(Square[].class,file);
   for (Square square: squares) 
      square.draw();

}

Now I want to make this method generic so that it can accept any shape. I want to be able to call it someMethod(Triangle.class,new File("File_with_triangles") or someMethod(Circle.class, new File("File_with_circles"). I am trying like this:

public void someMethod(Class<? extends Shape> type, File shapeFile) {
   ThirdPartyClass foo = new ThirdPartyClass();
   #### What goes here??? ####
   for (Shape shape: shapes)
       shape.draw();
}

What should be there at ##开发者_如何学C## What goes here??? #### ???


Assuming ThirdPartClass.someMajicMethod has a signature something like this:

public <T> T someMajicMethod(Class<T> class1, File file);

Then you should be able to do something like this:

public void someMethod(Class<? extends Shape> type, File shapeFile) {
    ThirdPartyClass foo = new ThirdPartyClass();

    @SuppressWarnings("unchecked")
    Class<? extends Shape[]> arrayType = 
        (Class<? extends Shape[]>) Array.newInstance(type, 0).getClass();
    assert Shape[].class.isAssignableFrom(arrayType);

    Shape[] shapes = foo.someMajicMethod(arrayType, shapeFile);

    for (Shape shape: shapes)
        shape.draw();
}

So if you call someMethod(Triangle.class, file), then arrayType will be Triangle[].class in the call to someMajicMethod.

Though you may find it simpler to have someMethod take the array type as a parameter instead of the element type so you can avoid that step.


Perhaps Array.newInstance(..) is of interest for you


Shape[] shapes = foo.someMajicMethod(type, file);

If foo is a third-party class, I assume you don't control the API of it. I assumed it has the appropriate method signature to handle the line I've written but there is no way for me to be certain without more information about that class.

If this doesn't work, what is the problem?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜