如何识别 Java 中的不可变对象 [英] How do I identify immutable objects in Java

查看:17
本文介绍了如何识别 Java 中的不可变对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我正在创建一个对象集合,这些对象将被各种线程以一种只有在对象不可变的情况下才是安全的方式访问.当尝试将新对象插入到我的集合中时,我想测试它是否是不可变的(如果不是,我将抛出异常).

In my code, I am creating a collection of objects which will be accessed by various threads in a fashion that is only safe if the objects are immutable. When an attempt is made to insert a new object into my collection, I want to test to see if it is immutable (if not, I'll throw an exception).

我可以做的一件事是检查一些众所周知的不可变类型:

One thing I can do is to check a few well-known immutable types:

private static final Set<Class> knownImmutables = new HashSet<Class>(Arrays.asList(
        String.class, Byte.class, Short.class, Integer.class, Long.class,
        Float.class, Double.class, Boolean.class, BigInteger.class, BigDecimal.class
));

...

public static boolean isImmutable(Object o) {
    return knownImmutables.contains(o.getClass());
}

这实际上让我成功了 90%,但有时我的用户会想要创建他们自己的简单的不可变类型:

This actually gets me 90% of the way, but sometimes my users will want to create simple immutable types of their own:

public class ImmutableRectangle {
    private final int width;
    private final int height;
    public ImmutableRectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }
    public int getWidth() { return width; }
    public int getHeight() { return height; }
}

有什么方法(也许使用反射)可以可靠地检测一个类是否不可变?误报(认为它不可变时认为它是不可变的)是不可接受的,但假阴性(认为它不可变时认为它是可变的)是.

Is there some way (perhaps using reflection) that I could reliably detect whether a class is immutable? False positives (thinking it's immutable when it isn't) are not acceptable but false negatives (thinking it's mutable when it isn't) are.

编辑添加:感谢您提供有见地和有用的答案.正如一些答案指出的那样,我忽略了定义我的安全目标.这里的威胁是无知的开发人员——这是一段框架代码,将被大量对线程几乎一无所知并且不会阅读文档的人使用.我不需要防御恶意开发者——任何聪明到可以变异String 或执行其他恶作剧也将足够聪明,知道在这种情况下这是不安全的.代码库的静态分析是一种选择,只要它是自动化的,但不能指望代码审查,因为不能保证每个审查都会有精通线程的审查员.

Edited to add: Thanks for the insightful and helpful answers. As some of the answers pointed out, I neglected to define my security objectives. The threat here is clueless developers -- this is a piece of framework code that will be used by large numbers of people who know next-to-nothing about threading and won't be reading the documentation. I do NOT need to defend against malicious developers -- anyone clever enough to mutate a String or perform other shenanigans will also be smart enough to know it's not safe in this case. Static analysis of the codebase IS an option, so long as it is automated, but code reviews cannot be counted on because there is no guarantee every review will have threading-savvy reviewers.

推荐答案

没有可靠的方法来检测类是否不可变.这是因为一个类的属性可以通过多种方式改变,而您无法通过反射检测到所有这些.

There is no reliable way to detect if a class is immutable. This is because there are so many ways a property of a class might be altered and you can't detect all of them via reflection.

接近这一点的唯一方法是:

The only way to get close to this is:

  • 只允许不可变类型的最终属性(您知道是不可变的原始类型和类),
  • 要求类本身是最终的
  • 要求它们从您提供的基类继承(保证不可变)

然后您可以使用以下代码检查您拥有的对象是否不可变:

Then you can check with the following code if the object you have is immutable:

static boolean isImmutable(Object obj) {
    Class<?> objClass = obj.getClass();

    // Class of the object must be a direct child class of the required class
    Class<?> superClass = objClass.getSuperclass();
    if (!Immutable.class.equals(superClass)) {
        return false;
    }

    // Class must be final
    if (!Modifier.isFinal(objClass.getModifiers())) {
        return false;
    }

    // Check all fields defined in the class for type and if they are final
    Field[] objFields = objClass.getDeclaredFields();
    for (int i = 0; i < objFields.length; i++) {
        if (!Modifier.isFinal(objFields[i].getModifiers())
                || !isValidFieldType(objFields[i].getType())) {
            return false;
        }
    }

    // Lets hope we didn't forget something
    return true;
}

static boolean isValidFieldType(Class<?> type) {
    // Check for all allowed property types...
    return type.isPrimitive() || String.class.equals(type);
}

更新:正如评论中所建议的,它可以扩展为在超类上递归,而不是检查某个类.还建议在 isValidFieldType 方法中递归使用 isImmutable.这可能会奏效,我也做了一些测试.但这并非微不足道.您不能只通过调用 isImmutable 来检查所有字段类型,因为 String 已经无法通过此测试(它的字段 hash 不是最终的!).此外,您很容易陷入无限递归,导致 StackOverflowErrors ;) 其他问题可能是由泛型引起的,您还必须检查它们的类型是否不可变.

Update: As suggested in the comments, it could be extended to recurse on the superclass instead of checking for a certain class. It was also suggested to recursively use isImmutable in the isValidFieldType Method. This could probably work and I have also done some testing. But this is not trivial. You can't just check all field types with a call to isImmutable, because String already fails this test (its field hash is not final!). Also you are easily running into endless recursions, causing StackOverflowErrors ;) Other problems might be caused by generics, where you also have to check their types for immutablity.

我认为通过一些工作,这些潜在的问题可能会以某种方式得到解决.但是,你必须先问问自己这是否真的值得(也是性能方面的).

I think with some work, these potential problems might be solved somehow. But then, you have to ask yourself first if it really is worth it (also performance wise).

这篇关于如何识别 Java 中的不可变对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆