Example usage for java.lang Object finalize

List of usage examples for java.lang Object finalize

Introduction

In this page you can find the example usage for java.lang Object finalize.

Prototype

@Deprecated(since = "9")
protected void finalize() throws Throwable 

Source Link

Document

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Usage

From source file:io.github.grahambell.taco.ClientTest.java

@Test
public void testOoClient() throws TacoException {
    DummyTransport xp = (DummyTransport) this.xp;

    xp.setResponse(//from w ww  . jav  a 2 s.co m
            new JSONObject().put("action", "result").put("result", new JSONObject().put("_Taco_Object_", 58)));

    Object obj = constructObject("SomeClass", Arrays.asList("juliette", "alpha"),
            new HashMapC().putc("victor", "alpha"));

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "construct_object").put("class", "SomeClass")
                    .put("args", new JSONArray(new String[] { "juliette", "alpha" }))
                    .put("kwargs", new JSONObject().put("victor", "alpha"))));

    assertEquals("<Taco object 58>", obj.toString());

    xp.setResponse(new JSONObject().put("action", "result").put("result", JSONObject.NULL));

    obj.callMethod("someMethod", null, null, null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_method").put("name", "someMethod")
                    .put("number", 58).put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)
                    .put("context", JSONObject.NULL)));

    obj.callMethod("someOtherMethod", null, null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_method").put("name", "someOtherMethod")
                    .put("number", 58).put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)
                    .put("context", JSONObject.NULL)));

    obj.getAttribute("someAttribute");

    assertThat(xp.getMessage(), matchesJson(
            new JSONObject().put("action", "get_attribute").put("name", "someAttribute").put("number", 58)));

    obj.setAttribute("someAttribute", "some value");

    assertThat(xp.getMessage(), matchesJson(new JSONObject().put("action", "set_attribute")
            .put("name", "someAttribute").put("number", 58).put("value", "some value")));

    callFunction("someFunction", Arrays.asList(obj), null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "someFunction")
                    .put("args",
                            new JSONArray(new java.lang.Object[] { new JSONObject().put("_Taco_Object_", 58) }))
                    .put("kwargs", JSONObject.NULL).put("context", JSONObject.NULL)));

    obj.finalize();

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "destroy_object").put("number", 58)));
}