反射找不到对象子类型

时间:2019-12-17 16:41:21

标签: java reflection

我试图通过使用Reflections将所有类都放在一个包中。 当我使用具体类的代码(在此示例中为A)时,它可以工作并打印子句信息(B扩展了A,因此它可以打印B信息),但是当我将其与Object类一起使用时,它不起作用。我该如何解决?

此代码有效:

program movehello;
uses 
    crt;
const 
    message = 'Hello, world!';

    procedure ShowMessage(x, y: integer; msg: string);
    begin
        gotoXY(x, y);
        write(msg);
    end;

    procedure HideMessage(x, y: integer; msg: string);
    var
        len, i: integer;
    begin
        len := length(msg);
        gotoXY(x, y);
        for i := 1 to len do
            write(' ');
    end;

    procedure MoveMessage(var x, y: integer; msg: string; dx, dy: integer);
    var
        len: integer;
    begin
        len := length(msg);
        HideMessage(x, y, msg);
        x := x + dx;
        y := y + dy;
        if x < 1 then
            x := ScreenWidth
        else if x > ScreenWidth then
            x := 1
        else if y < 1 then
            y := ScreenHeight - 1
        else if y > ScreenHeight - 1 then
            y := 1;
        ShowMessage(x, y, msg);
    end;

var
    curX, curY: integer;
    ch: char;
begin
    clrscr;
    curX := (ScreenWidth - length(message)) div 2;
    curY := ScreenHeight div 2;
    ShowMessage(curX, curY, message);
    while true do 
    begin
        gotoXY(ScreenWidth - 26, ScreenHeight);
        write('x: ',curX,' (',ScreenWidth,'), y: ',curY,' (',ScreenHeight,') ');
        ch := readkey;
        if ch <> #0 then
            break;
        ch := readkey;
        case ch of
            #75: MoveMessage(curX, curY, message, -1, 0); {left}
            #77: MoveMessage(curX, curY, message, 1, 0); {right}
            #72: MoveMessage(curX, curY, message, 0, -1); {up}
            #80: MoveMessage(curX, curY, message, 0, 1); {down}
        end;
    end;
    clrscr;
end.

此代码不:

Reflections reflections = new Reflections(REFLECTION_PACKAGE);
Set<Class<? extends A>> allClasses = reflections.getSubTypesOf(A.class);

System.out.println("numberOfLCasses: " + allClasses.size());
System.out.println("classes: " + allClasses.toString());

1 个答案:

答案 0 :(得分:1)

这是documented behavior

  

public SubTypesScanner()

     

创建了新的SubTypesScanner。将排除直接的Object子类型

     

public SubTypesScanner(boolean excludeObjectClass)

     

创建了新的SubTypesScanner。

     

参数:       excludeObjectClass-如果为false,则在结果中包括直接Object子类型。

以下应返回Object.class的子类型

Reflections reflections = new Reflections(REFLECTION_PACKAGE,new SubTypesScanner(false));
Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);

System.out.println("numberOfLCasses: " + allClasses.size());
System.out.println("classes: " + allClasses.toString());