有没有办法检查给定的类类型是属于 Java 对象还是自定义对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18967958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:57:42  来源:igfitidea点击:

Is there any way to check whether a given class type belongs to Java object or custom object

javaclassreflectiontypes

提问by Sarath

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.

我正在寻找任何方法来检查类的给定属性的类型是自定义对象类型(例如,Person)还是 Java 对象(例如,String、Long、原始类型)类型。如果使用 instanceof,检查所有 Java 类型将会很忙。任何人都可以建议一种检查方法,如果存在这样的东西。

采纳答案by Nicole

Java is very much "built on itself". Much of the SDK could be considered both "custom" or "built-in" depending on which way you are looking at it.

Java 非常“建立在自身之上”。SDK 的大部分内容都可以被视为“自定义”或“内置”,具体取决于您查看它的方式。

For instance, Exceptionis built in to Java but also written in Java. You could write similar code yourself, however based on what you've said I am guessing you would consider it a "Java object".

例如,Exception内置于 Java 中,但也是用 Java 编写的。您可以自己编写类似的代码,但是根据您所说的,我猜您会认为它是“Java 对象”。

Similarly, ArrayListis also written in Java, but because it's a utility class, I'm guessing you would consider it "custom", though I'm not sure, since it is still included in the SDK.

同样,ArrayList它也是用 Java 编写的,但因为它是一个实用程序类,我猜你会认为它是“自定义”的,尽管我不确定,因为它仍然包含在 SDK 中。

Without knowing exactly what you are looking for, the closest grouping that I can guess based on what you've said (String, Long, etc.) is the java.langpackage. In the words of the SDK documentationitself, the java.langpackage:

不知道你在寻找什么,最接近的分组,我可以根据你所说的(猜测StringLong等)的java.lang包装。用 SDK 文档本身的话来说java.lang包:

Provides classes that are fundamental to the design of the Java programming language.

提供对 Java 编程语言设计至关重要的类。

You can check if the package name of the class for the object is in java.lang:

您可以检查对象的类的包名称是否在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");
}

Working example

工作示例

回答by ppeterka

I'm not entirely sure I got your question right...

我不完全确定我问对了你的问题......

The java.lang.Classclass features some tools you can use.

java.lang.Class中的类具有一些可以使用的工具。

The isPrimitive()function

所述isPrimitive()函数

Determines if the specified Class object represents a primitive type. There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double.

These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true.

确定指定的 Class 对象是否表示原始类型。有九个预定义的 Class 对象来表示八种基本类型和 void。它们由 Java 虚拟机创建,并与它们所代表的原始类型具有相同的名称,即 boolean、byte、char、short、int、long、float 和 double。

这些对象只能通过以下公共静态最终变量访问,并且是此方法为其返回 true 的唯一 Class 对象。

As for the other classes, there is no such thing as a "Java object" - every object is a Java object... But you could for example check if the name of the class in question begins with java.lang.prefix, but the java.lang packagecontains a lot different things too, not only the basic datatypes you'd like to find.

至于其他类,没有“Java 对象”这样的东西——每个对象都是一个 Java 对象……但是你可以例如检查有问题的类的名称是否以java.lang.前缀开头,但是java.lang package 也包含很多不同的东西,不仅仅是你想要找到的基本数据类型。

回答by M. Abbas

Suppose that your own classes aren't located within packages whose names start with java*:

假设您自己的类不在名称以 开头的包中java*

public static <T> boolean isJDKClass(T t) {
     return t.getClass().getPackage().getName().startsWith("java");
}

回答by splungebob

I think you want something like:

我想你想要这样的东西:

public static boolean isJavaObject(Object obj)
{
  return (! obj.getClass().getName().startsWith("my.package"));
}

where "my.package"is the head of your package heirarchy.

"my.package"包层次结构的头部在哪里。

回答by LexLythius

There's no fail-safe method to do this. As others said, Java builds a lot on regular classes.

没有万无一失的方法可以做到这一点。正如其他人所说,Java 在常规类上构建了很多。

I guess that, appart from primitives, it all boils down to your own definition of "JDK's classes" vs "my classes" vs "3rd party classes".

我想,从原语来看,这一切都归结为您自己对“JDK 的类”与“我的类”与“第 3 方类”的定义。

@splungebob's solution works for your own, single tree-branch classes.

@splungebob 的解决方案适用于您自己的单个树分支类。

I would think that classes from the following packages can be safely, if minimallistically, regarded as JDK's:

我认为以下包中的类可以安全地(如果从最低限度来看)视为 JDK 的:

java.lang.*
java.*
javax.*
com.sun.*
com.oracle.*

You might also add some common 3rd parties like org.apache.*

您还可以添加一些常见的 3rd 方,例如 org.apache.*

回答by Pshemo

Try maybe using implementation vendortitle of package that object is coming from. It seems that standard Java packages have this value set to "Oracle Corporation". "Java Runtime Environment".

尝试使用该对象来自的包的实现供应商标题。似乎标准 Java 包将此值设置为"Oracle Corporation". "Java Runtime Environment".

public static boolean isJavaLang(Object check) {
    if (check == null)// lets say that null comes from JRE
        return true;

    return isJavaLang(check.getClass());
}

public static boolean isJavaLang(Class<?> check) {

    Package p = check.getClass().getPackage();

    if (p == null) // default package is package for users classes
        return false;

    String title = p.getImplementationTitle();
    if (title == null)// no title -> class not from Oracle
        return false;

    // System.out.println(p.getImplementationVendor());
    // System.out.println(p.getImplementationTitle());
    return title.equals("Java Runtime Environment");
}