通用类的未经检查的类型转换?

时间:2015-12-09 19:33:57

标签: java generics enums

我正在尝试编写一个方法,我可以在运行时从字符串转换为枚举对象,用于通用枚举。我有一个方法签名:

public static <T extends Enum<T>> Enum<T> foo(String string, Class<T> clazz)

但是,我从一个类中调用它,它的泛型类型参数没有显式扩展Enum。即。

class bar<X> {
  private Class<X> clazz;
  if (XIsAnEnum()) {
    foo(string, clazz)
  }
}

这不能编译,因为即使我知道XIsAnEnum的逻辑X extends Enum<X>,我也没有在泛型类型参数定义中明确说明这一点,所以它不是一个有效的论点。

有没有办法从Class<X>Class<X extends Enum<X>>进行未经检查的演员表,或者我是否必须专门针对何时使用枚举制作新的bar2<X extends Enum<X>>课程?

1 个答案:

答案 0 :(得分:0)

你可以使用Class#asSubclass(Class)为你做演员表,比如

foo("value", clazz.asSubclass(Enum.class));

这涉及clazz指的是Class的实际验证,EnumActionController::RoutingError (No route matches [GET] "/index.html"): actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' web-console (2.2.1) lib/web_console/middleware.rb:39:in `call' actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app' railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call' activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged' activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged' activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged' railties (4.2.0) lib/rails/rack/logger.rb:20:in `call' actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call' rack (1.6.4) lib/rack/methodoverride.rb:22:in `call' rack (1.6.4) lib/rack/runtime.rb:18:in `call' activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call' rack (1.6.4) lib/rack/lock.rb:17:in `call' actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call' rack (1.6.4) lib/rack/sendfile.rb:113:in `call' railties (4.2.0) lib/rails/engine.rb:518:in `call' railties (4.2.0) lib/rails/application.rb:164:in `call' rack (1.6.4) lib/rack/lock.rb:17:in `call' rack (1.6.4) lib/rack/content_length.rb:15:in `call' rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service' /home/patricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service' /home/patricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run' /home/patricio/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread' Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.1ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (1.8ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (7.6ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (39.5ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.4ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.5ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (15.3ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms) Rendered /home/patricio/.rvm/gems/ruby-2.2.1@rezeta/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (29.7ms) 的子类。

你在这里抛弃了所有的通用验证。