java type to class_将类型符号转换为java.lang.Class实例(Converting a type symbol to a java.lang.Class instance)...

将类型符号转换为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

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值