简体   繁体   English

通过运行时反射确定不在标准库(Java或Scala)中的基类

[英]Determine base classes not in standard lib (Java or Scala) with runtime reflection

I'm generating GraphQL definitions from Scala types but can't find anything in the runtime reflection API to only get base classes that are "user defined" ie not part of the Scala or Java standard library. 我正在从Scala类型生成GraphQL定义,但在运行时反射API中找不到任何内容,只能获取“用户定义”的基类,即不属于Scala或Java标准库的一部分。

As of right now I just have a set of the ones I know pops up eg Any , Object and about 8 others. 截至目前,我只弹出一组我知道的Object ,例如AnyObject和大约8个其他Object Those are only in the list because they're what showed up when I was writing unit tests. 这些只是在列表中,因为它们是我编写单元测试时显示的内容。 Ideally I don't want a manual list, seems brittle as new types are added it'll probably break. 理想情况下,我不需要手动列表,由于添加了新类型,它似乎很脆弱,可能会损坏。

I don't know if this is possible with macros but that's not an option. 我不知道宏是否有可能,但这不是一个选择。 After experimenting with macros there were too many bugs around it, including it causing the compiler to crash and the fix for that bug is suppose to be in the latest zinc but I can't use it because I'm using Gradle which can only use the 0.xx zinc series due to namespace changes in the 1.x series. 在尝试了宏之后,周围的错误太多了,包括导致编译器崩溃的错误,并且该错误的修复应该是在最新的zinc但我无法使用它,因为我使用的Gradle只能使用由于1.x系列中的名称空间更改,导致0.xx锌系列。

So all that being said, does anyone know if there's a means of determining baseclasses of a type that are not from the standard libraries? 这样说来,有没有人知道是否有一种方法可以确定标准库中没有的类型的基类?

I don't think such a feature exist and even should exist. 我认为这样的功能不存在,甚至不应该存在。 Particularly I don't think that the term "standard library" is really defined well enough in the context of your problem. 特别是我认为在您的问题中“标准库”一词的定义不够好。 I mean why exactly it should exclude classes from the standard library but not from some other popular libraries you depend on? 我的意思是为什么它确切地应该从标准库中排除类,而不从您依赖的其他一些流行库中排除类? And there are some quite complicated types in the standard library as well (eg java.text.DateFormat or org.w3c.dom.Document ). 在标准库中也有一些非常复杂的类型(例如java.text.DateFormatorg.w3c.dom.Document )。 Yes, you probably don't expose them right now but this is only for now. 是的,您可能现在不公开它们,但这仅是目前。

I think that the proper way is to have an explicit white list of all base classes and/or packages that you want to expose as a part of your API contract and use it for filtering. 我认为正确的方法是为您要作为API合同的一部分公开的所有基类和/或程序包提供明确的白名单,并将其用于过滤。 You might also try to use a black list (like java.* , javax.* , scala.* ) to filter out the "standard library" and it might work for some time but I still don't think this is a good idea. 您可能还会尝试使用黑名单(例如java.*javax.*scala.* )来过滤“标准库”,它可能会工作一段时间,但我仍然认为这不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM