Sar*_*ath 5 java reflection types class
I was searching for any ways to check whether the type of the given attribute of a class is of custom object type (for eg., Person) or Java object (for eg., String, Long, primitive types as well) type. If instanceof is used, it will be hectic checking all of the Java types. Can anyone suggest a way to check, if anything as such exists.
Java非常"基于自身".许多SDK可以被视为"自定义"或"内置",具体取决于您正在查看它的方式.
例如,Exception内置于Java但也使用Java编写.您可以自己编写类似的代码,但是根据您所说的我猜您会认为它是"Java对象".
同样,ArrayList也是用Java编写的,但因为它是一个实用类,我猜你会认为它是"自定义的",虽然我不确定,因为它仍然包含在SDK中.
不知道你在寻找什么,我可以根据你所说的话(猜测最接近的分组String,Long等)的java.lang包装.用SDK文档本身的话来说,java.lang包:
提供对Java编程语言设计至关重要的类.
您可以检查对象的类的包名是否在java.lang:
static class A {
}
public static void main (String[] args) {
String test1 = "";
A test2 = new A();
int test3 = 3;
System.out.println(isJavaLang(test1));
System.out.println(isJavaLang(test2));
System.out.println(isJavaLang(test3));
}
public static boolean isJavaLang(Object check) {
return check.getClass().getName().startsWith("java.lang");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9219 次 |
| 最近记录: |