当从实例方法返回一个没有引用其封闭类的匿名类时,它具有对此的引用.为什么?

  • 本文关键字:引用 返回 实例方法 一个 java anonymous-class
  • 更新时间 :
  • 英文 :


当从实例方法返回不引用其封闭类的匿名类时,它具有对this的引用。为什么?

考虑以下代码:

package so;
import java.lang.reflect.Field;
public class SOExample {
    private static Object getAnonymousClassFromStaticContext() {
        return new Object() {
        };
    }
    private Object getAnonymousClassFromInstanceContext() {
        return new Object() {
        };
    }
    public static void main(String[] args) throws NoSuchFieldException, SecurityException {
        Object anonymousClassFromStaticContext = getAnonymousClassFromStaticContext();
        Object anonymousClassFromInstanceContext = new SOExample().getAnonymousClassFromInstanceContext();
        Field[] fieldsFromAnonymousClassFromStaticContext = anonymousClassFromStaticContext.getClass().getDeclaredFields();
        Field[] fieldsFromAnonymousClassFromInstanceContext = anonymousClassFromInstanceContext.getClass().getDeclaredFields();
        System.out.println("Number of fields static context: " + fieldsFromAnonymousClassFromStaticContext.length);
        System.out.println("Number of fields instance context: " + fieldsFromAnonymousClassFromInstanceContext.length);
        System.out.println("Field from instance context: " + fieldsFromAnonymousClassFromInstanceContext[0]);
    }
}

这是输出:

Number of fields static context: 0
Number of fields instance context: 1
Field from instance context: final so.SOExample so.SOExample$2.this$0

每个方法虽然看起来调用相同的代码,但执行的却是不同的操作。在我看来,实例方法返回的是嵌套类,而静态方法返回的却是静态嵌套类(作为静态成员,它显然不能引用this)。

考虑到没有引用封闭类的事实,我看不出这有什么好处。

幕后发生了什么?

匿名/内部类背后有一个设计原则:内部类的每个实例都属于外部类的一个实例

省略对内部类的引用将改变垃圾收集的行为:按照它的实现方式,只要内部类是活动的,就不能对外部类进行垃圾收集
这支持了这样一种观点,即没有外部类,内部类就不可能存在。

应用程序可能依赖于这种行为,例如创建一个临时文件并在析构函数中删除它。这样,只有当所有内部类都消失时,文件才会被删除。

这也意味着当前行为不能更改,因为更改它可能会破坏现有的应用程序。

因此,当您不需要引用时,应该始终将内部类标记为静态,因为这可能会导致一些很好的内存泄漏。

编辑:我想说的例子(对糟糕的代码质量感到抱歉):

class Ideone
{
    static Object[] objects = new Object[2];
    public static void main (String[] args) throws java.lang.Exception
    {
        M1();
        M2();
        System.gc();
    }
    static void M1() {
        objects[0] = new Foo().Bar();
    }
    static void M2() {
        objects[1] = new Foo().Baz();
    }
}
class Foo {
    static int i = 0;
    int j = i++;
    public Foo() {
        System.out.println("Constructed: " + j);
    }
    Object Bar() {
        return new Object() {
        };
    }
    static Object Baz() {
        return new Object() {
        };
    }
    protected void finalize() throws Throwable {
        System.out.println("Garbage collected " + j);
    }
}

输出:

构造:0
构造:1
垃圾收集1

正如您所看到的,第一个Foo是而不是垃圾收集的,因为仍然有一个"内部实例"处于活动状态。为了使这种行为起作用,内部类需要一个引用。

当然,它也可以以不同的方式实现。但我想说,保留引用是有目的的设计决定,这样"内部实例"就不会比其父实例更长寿。

BTW:Java语言引用非常隐晦地说明了这一点(内部类不访问外部类也不例外):

类或接口O的直接内部类C的实例i是与O的实例关联,称为立即封闭i的实例。对象的直接封闭实例,如果任何,在创建对象时确定(§15.9.2)。

我只想说:它引用了this,因为它可能需要它。

想象一下,对程序进行轻微修改:

public class SOExample
{
    private static Object getAnonymousClassFromStaticContext()
    {
        return new Object()
        {
            @Override
            public String toString()
            {
                // ERROR: 
                // "No enclosing instance of the type SOExample is accessible in scope"
                return SOExample.this.toString(); 
            }
        };
    }
    private Object getAnonymousClassFromInstanceContext()
    {
        return new Object()
        {
            @Override
            public String toString()
            {
                // Fine
                return SOExample.this.toString(); 
            }
        };
    }
}

显然,在实例上下文中创建的对象需要对this的引用,因为它必须有可能访问封闭实例的方法(或字段,如果存在的话)。

在您的原始示例中,您没有以任何方式访问封闭实例,这并不意味着默认情况下不存在此this引用。

在什么时候应该做出其他决定?编译器是否应该检查this引用是否真的是必需的,如果不是,则将其丢弃?如果您不想要this,那么创建一个静态内部类(或从静态上下文创建此实例)。对封闭实例的引用只是内部类的实现方式。


顺便说一句:与equal的比较将返回false,即使对于两个对象都是从同一"上下文"创建的,只要您没有在返回的对象中相应地实现自己的equals方法

即使我们看不到任何可见的引用,它仍然存在。请参阅下面的代码。

package jetty;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Arrays;
public class SOExample2 {
    private static Object staticField = new Object () { };
    private Object nonStaticField = new Object () { };
    private static Object getAnonStatic() {
        return new Object() { };
    }
    private Object getAnonNonStatic() {
        return new Object() { };
    }
    public static void main(String[] args) throws NoSuchFieldException, SecurityException {
        System.out.println("Started");
        class StaticMethodLocal {
        }
        System.out.println("############## Fields ##############");
        printClassInfo(staticField.getClass());
        printClassInfo(new SOExample2().nonStaticField.getClass());
        System.out.println("############## Methods ##############");
        printClassInfo(getAnonStatic().getClass());
        printClassInfo(new SOExample2().getAnonNonStatic().getClass());
        System.out.println("############## Method Local ##############");
        printClassInfo(new StaticMethodLocal().getClass());
        printClassInfo(new SOExample2().getNonStaticMethodLocal().getClass());
    }
    public static <T>void printClassInfo(Class<T> klass) {
        System.out.println("Class : " + klass);
        String prefix = "t";
        System.out.println(prefix + "Number fields : " + klass.getDeclaredFields().length);
        if(klass.getDeclaredFields().length > 0) {
            System.out.println(prefix + "fields : " + Arrays.toString(klass.getDeclaredFields()));
        } else {
            System.out.println(prefix + "no fields");
        }
        System.out.println(prefix + "modifiers : " + Modifier.toString(klass.getModifiers()));
        //Constructors
        Constructor<?>[] constructors = klass.getDeclaredConstructors();
        for(Constructor<?> constructor : constructors) {
            System.out.println(prefix + "constructor modifiers : " + Modifier.toString(constructor.getModifiers()));
            System.out.println(prefix + "constructor parameters : " + Arrays.toString(constructor.getParameterTypes()));
        }
        System.out.println("");
    }
    private Object getNonStaticMethodLocal () {
        class NonStaticMethodLocal {
        }
        return new NonStaticMethodLocal();
    }
}

输出:

Started
############## Fields ##############
Class : class jetty.SOExample2$1
    Number fields : 0
    no fields
    modifiers : 
    constructor modifiers : 
    constructor parameters : []
Class : class jetty.SOExample2$2
    Number fields : 1
    fields : [final jetty.SOExample2 jetty.SOExample2$2.this$0]
    modifiers : 
    constructor modifiers : 
    constructor parameters : [class jetty.SOExample2]
############## Methods ##############
Class : class jetty.SOExample2$3
    Number fields : 0
    no fields
    modifiers : 
    constructor modifiers : 
    constructor parameters : []
Class : class jetty.SOExample2$4
    Number fields : 1
    fields : [final jetty.SOExample2 jetty.SOExample2$4.this$0]
    modifiers : 
    constructor modifiers : 
    constructor parameters : [class jetty.SOExample2]
############## Method Local ##############
Class : class jetty.SOExample2$1StaticMethodLocal
    Number fields : 0
    no fields
    modifiers : 
    constructor modifiers : 
    constructor parameters : []
Class : class jetty.SOExample2$1NonStaticMethodLocal
    Number fields : 1
    fields : [final jetty.SOExample2 jetty.SOExample2$1NonStaticMethodLocal.this$0]
    modifiers : 
    constructor modifiers : 
    constructor parameters : [class jetty.SOExample2]

我添加了另外两种类型的匿名类作为字段值和两个方法本地类。

给定上面的输出,很明显,在非静态上下文中生成的类只有一个构造函数,并且这个构造函数有一个封闭类类型的参数。

正如输出JVM所建议的那样,在创建匿名/方法本地类时,添加一些额外的代码来保存封闭类实例引用。

这也可以在反编译器输出中看到。

    // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SOExample2.java
//Static field anonynmouse class
class SOExample2$1
{
    SOExample2$1()
    {
    }
}
//Non static field anonynmouse class
class SOExample2$2
{
    final SOExample2 this$0;
    SOExample2$2()
    {
        this$0 = SOExample2.this;
        super();
    }
}
//static method anonynmouse class
class SOExample2$3
{
    SOExample2$3()
    {
    }
}
//Non static method anonynmouse class
class SOExample2$4
{
    final SOExample2 this$0;
    SOExample2$4()
    {
        this$0 = SOExample2.this;
        super();
    }
}
//Static method local class
class SOExample2$1StaticMethodLocal
{
    SOExample2$1StaticMethodLocal()
    {
    }
}
//Non static method local class
class SOExample2$1NonStaticMethodLocal
{
    final SOExample2 this$0;
    SOExample2$1NonStaticMethodLocal()
    {
        this$0 = SOExample2.this;
        super();
    }
}

结论:

  1. 编译器在生成我们看不到的类文件时会执行一些操作。例如,将default constructor添加到类,或将默认的超类构造函数super()调用添加到不显式调用任何this()自构造函数或super()构造函数的构造函数。添加封闭类型的引用也是如此,这并没有什么神奇之处。我们可以很容易地定义这样一个类,编译器为我们做这件事只是让我们的生活更轻松
  2. 这和编写自己的字节码是一样的。因此,绕过编译器本身,我们可以做到这一点,而实际语言却不能做到
  3. 如果匿名类中没有modifier输出,则可以得出结论,它们只是方法局部类(所有类修饰符public、protected、private、abstract、static)在方法内部都失去了意义。它们只是被称为匿名类,伪装成方法本地类

最新更新