将类型符号转换为java.lang.Class实例(Converting a type symbol to a java.lang.Class instance)
这是我的问题:假设我需要clojure.reflect :as r ,然后,例如,做
(->> (r/reflect java.lang.String)
:members
(filter #(= (:name %) 'getBytes))
first
:return-type)
这将评估为符号的byte<> 。 我该如何将这个符号映射到一个Java类,也就是说,我怎样才能编写一个函数type-symbol-to-class
(assert
(= (class (byte-array [1 2 3]))
(type-symbol-to-class 'byte<>)))
不抛出异常? 如果该函数适用于更多符号而不仅仅是byte<> ,那将是一件好事。
Here is my problem: Suppose I require clojure.reflect :as r and then, for example, do
(->> (r/reflect java.lang.String)
:members
(filter #(= (:name %) 'getBytes))
first
:return-type)
This will evaluate to byte<> which is a symbol. How can I map that symbol to a Java class, that is, how can I write a function type-symbol-to-class such that
(assert
(= (class (byte-array [1 2 3]))
(type-symbol-to-class 'byte<>)))
does not throw an exception? It is good if that function works for more symbols than just byte<>.
原文:https://stackoverflow.com/questions/50623419
更新时间:2019-10-10 12:34
最满意答案
Clojure反射库做了很多工作( 1,2,3 )来优化输出(包括参数/返回类型名称),并且执行反向变换不是一个简单的任务。
如果你需要一个Class对象,你可以使用Java的反射工具而不是clojure.reflect :
(= (->> java.lang.String
.getDeclaredMethods
(filter #(= (.getName %) "getBytes"))
first
.getReturnType)
(class (byte-array [1 2 3]))) # -> true
Clojure reflection library does a lot of work (1, 2, 3) to prettify the output (including the argument / return type names) and it is not a straightforward task to do a reverse transform.
If you need a Class object, you can just use Java's reflection tools instead of clojure.reflect:
(= (->> java.lang.String
.getDeclaredMethods
(filter #(= (.getName %) "getBytes"))
first
.getReturnType)
(class (byte-array [1 2 3]))) # -> true
2018-05-31
相关问答
Clojure反射库做了很多工作( 1,2,3 )来优化输出(包括参数/返回类型名称),并且执行反向变换不是一个简单的任务。 如果你需要一个Class对象,你可以使用Java的反射工具而不是clojure.reflect : (= (->> java.lang.String
.getDeclaredMethods
(filter #(= (.getName %) "getBytes"))
first
.getReturnType)
...
我有我的工作方式,希望它有帮助 private void createDynamicColumns(){
//split column templates
String[] columnKeys = columnTemplate.split(" ");
//set first list to null
//this should be a List with no type specified
...
我认为这是一个消除问题: if (!c.isEnum() && !c.isInterface() && !c.isArray() && !c.isAnnotation() && !c.isPrimitive()) {
// It's a class
}
...这不是很令人满意,因为在将新功能添加到Java时(例如枚举,注释等),您必须重新访问该定义。 I think it's a matter of elimination: if (!c.isEnum() && !c.isInterfa
...
我在RTFM之后发现了它。 我只是错过了“toInstance”方法的存在: bind(new TypeLiteral(Class extends MyInterface>)(){}).toInstance(MyImplementation.class)
希望这可以帮助遇到类似问题的其他人! I figured it out after RTFM. I just missed the existence of the "toInstance" method: bind(new TypeLit
...
看起来这就是你想要的。 private static Class>[] toPrimitiveTypeArray(
// vvvvvvvv
Class>[] objects, boolean log) {
...
for (int i = 0; i < types.length; i++) {
...
// vvvvvvvvvv
types[i] =
...
默认情况下,Proguard会删除一些类型信息: 如何阻止Proguard删除类型参数? 添加以下行可以解决问题: -keepattributes Signature
可能是整个魔术线会起作用,即: -keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
By default Proguard removes som
...
根据这个声明, ... at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ... PowerMock(使用Objenesis库)尝试实例化java.lang.Class ,它只能由JVM实例化。 来自docs : 类没有公共构造函数。 相反,类对象由Java虚拟机在加载类时自动构造,并通过调用类加载器中的defineClass方法。 不久,我几乎可以肯定,手动生成java.lang.Class实例是不可能的。 如果
...
Class和T之间的区别在于Class Class表示关于T元数据。 您可以查找T的方法和字段,还可以通过调用newInstance方法创建新实例。 就创建Class对象而言,最简单的方法是使用MyClass.class语法,例如 Class tr = TermlistRequest.class;
也可以通过动态解析类来获取Class (但不是Class )对象。 The difference between Class an
...