Java instance creation with object as parameter - garbage-collection

When i create an object of a class which accepts object as a parameter
for Example
GarbageCollected gc= new GarbageCollected();
WeakReference<GarbageCollected> reference = new WeakReference<GarbageCollected>()(gc);
So here how does the reference 'gc' works internally while one creates the object of 'WeakReference' class with "gc' as the argument.

Related

How to use EncodedObjectAsID?

I'm trying to understand get_instance_id()
and I came across this line in the documentation:
This ID can be saved in EncodedObjectAsID, and can be used to retrieve
the object instance with #GDScript.instance_from_id.
I can't seem to understand what this statement means exaclty and how to use EncodedObjectAsID, could someone please provide a working example?
The EncodedObjectAsID follows a pattern called Boxing. Boxing is where you put a primitive value, like an int, into an object. This boxed primitive can now be used in an object oriented way. For example, you can pass the boxed int to a function that only takes objects (i.e. it applies Polymorphism):
func only_takes_object(obj: Object)
only_takes_object(123) # Error
var box = EncodedObjectAsID.new()
box.object_id = 123
only_takes_object(box) # Valid
This is how parts of the editor use the EncodedObjectAsId object.
In marshalls.cpp we can see that an encoded Object may be an integer ID or the whole object. When it is flagged as only an integer ID a EncodedObjectAsID object is created. This object is then converted to a Variant.
When adding a stack variable in editor_debugger_inspector.cpp a variant with a type of object is assumed to be and converted to an EncodedObjectAsID to fetch the referenced object's id.
Here's two more links that follow a similar pattern:
array_property_edit.cpp
scene_debugger.cpp
Note that Variant can be implicitly converted to an Object and Object::cast_to() only takes Objects.
This ID can be saved in EncodedObjectAsID, and can be used to retrieve the object instance with #GDScript.instance_from_id.
This sentence should be split into two independent clauses. It should read as
"The instance ID can be saved in an EncodedObjectAsID."
"The instance ID can be used to retrieve the object instance with #GDScript.instance_from_id()."
Note: You should not store an object's id in storage memory. There is no guarantee that an object's id will remain the same after restart.

In Groovy, casting between collection implementations copies source instead

The following code
void testReference() {
List<String> source = new ArrayList<>()
source.add("element")
List reference = (ArrayList)source // all ok, creates reference as types match
assertSame(source,reference)
List copyNotReference = (LinkedList)source // should fail on GroovyCastException, creates copy instead
assertNotSame(source,copyNotReference) // this works, copy is a different object
copyNotReference.add("second element")
println source
println copyNotReference
}
only works in Groovy. In Java it fails on attempt to cast ArrayList to LinkedList.
In Groovy it creates a LinkedList instance, calling constructor
public LinkedList(Collection<? extends E> c)
and copying source data to the new instance.
The test outputs
[element]
[element, second element]
That behaviour only occurs when casting types that are subtypes of collections.
Question
What Groovy mechanism is responsible for this unexpected behaviour?
Groovy allows coercion of objects via casting them (asType). This is implemented for collections.
See the source
Converts the given collection to another type. A default concrete
type is used for List, Set, or SortedSet. If the given type has
a constructor taking a collection, that is used. Otherwise, the
call is deferred to {#link #asType(Object,Class)}. If this
collection is already of the given type, the same instance is
returned.

I want to clear the concept about object and instance using below condition

I have few questions in ref. of below two lines
Dim coll As Collection
Set coll = New Collection
which is the object in 2nd line coll or collection
What New & Set key word is doing in second line
What is instance
(1) coll is object, more precisely: pointer/reference to some object in memory.
(2) New and Set are keywords reserved for object. Set is syntatically required when assigning value to an reference type variable (which point to an object in memory and the value is just reference to some address in memory).
New keyword is used to initialize object, i.e. allocate memory.
(3) Instance is an instance of a class. Class can have properties, methods, etc. But it's just a class, you cannot do much with it. To access properties or use methods, you have to instantiate that class, like you do with collection. When you have an instance of a class, you can use its methods, etc.

newObjectIDForEntity in NSIncrementalStore

I have started using NSIncrementalStore. When you process a fetch request, you first have to process the predicate to get your internal reference objects. Then you convert these to objectID's and then you ask the context to get the corresponding managedObjects. At least that is my interpretation of the available documentation.
let fetchedReferences : [Int] = Array(names.keys) //names represent my backingstore
var fetchedObjectIDs : [NSManagedObjectID] = []
for reference in fetchedReferences
{
fetchedObjectIDs.append(self.newObjectIDForEntity(request.entity, referenceObject: reference))
}
var fetchedObjects : [NSManagedObject] = []
for objectID in fetchedObjectIDs
{
fetchedObjects.append(context.objectWithID(objectID))
}
"newObjectIDForEntity" is also used to obtain permanent objectID's (see obtainPermanentIDsForObjects)
I want to know what "newObjectIDForEntity" does. Does it make a new instance for the same object or does it each time internally create a new object? What I mean is this: if I create a new managed object and then fetch the object, I will have called "newObjectIDForEntity" twice for the same object. Does core data now think there are 1 or 2 objects?
Does it make a new instance for the same object or does it each time internally create a new object?
newObjectIDForEntity:referenceObject: is one of two utility methods for mapping between the store's internal representation of a managed object snapshot and an NSManagedObjectID. It's inverse is referenceObjectForObjectID:. As you might guess from the name, newObjectIDForEntity:referenceObject: returns an object considered to have a retain count of 1. newObjectIDForEntity:referenceObject: calls an internal factory method for generating an NSManagedObjectID that is unique to that reference object in this persistent store. Once that has been done, referenceObjectForObjectID: can look up that NSManagedObjectID and return the reference object it represents.
What I mean is this: if I create a new managed object and then fetch the object, I will have called "newObjectIDForEntity" twice for the same object. Does core data now think there are 1 or 2 objects?
I assume you mean an NSManagedObjectContext that is using your store creates the managed object. You can call newObjectIDForEntity:referenceObject: as many times as you want, the NSManagedObjectID instance may be different, but the data it represents is unchanged. It will know that it points to the same reference object as an earlier call with the same reference data and entity description.

how to expand #137b866 in soap ui using groovy?

in soap ui we usually get values like
"com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep#137b866"
what does it mean and how can we expand it?
That means you have an instance of WsdlGroovyScriptTestStep
So you should be able to just call the methods in the documentation I linked to, ie:
obj.description
(which will call getDescription())
This means that you have an object which is an instance of this class, and probably you're invoking toString() method on this object, by default if you don't override toString() method on specific object you get objectClassName#hashcodenumber.
If you want to see the WsdlGroovyScriptTestStep methods you can take a look on the API. However if you want to see dynamically a list of all methods for an specific object with groovy, you can do it in a java way using reflection. For example if you have an object instance of some class you can get the class obj.getClass()an invoking obj.getClass().getMethods() or obj.getClass().getDeclaredMethods() in this object you get a list of all of its methods. See the example below:
def obj = 'sample test'
// Returns an array of Method objects reflecting all the methods declared by the class
// or interface represented by this Class object. (from java API)
def declaredMethods = obj.getClass().getDeclaredMethods()
// Returns an array containing Method objects reflecting all the public member methods
// of the class or interface represented by this Class object, including those declared
// by the class or interface and those inherited from superclasses and superinterfaces.
def methods = obj.getClass().getMethods()
log.info "DECLARED METHODS"
// print the method names
for(declaredMethod in declaredMethods){
log.info declaredMethod
}
log.info "METHODS"
// print more method names
for(method in methods){
log.info method
}
// i.e invoke indexOf(int) using reflection
def classArray = new Class[1]
classArray[0] = String.class
def indexOfMethod = obj.getClass().getDeclaredMethod("indexOf", classArray)
def result = indexOfMethod.invoke(obj,'test')
log.info "using reflection: 'sample test'.indexOf('test') =" + result
log.info "normal invocation: 'sample test'.indexOf('test') =" + obj.indexOf('test')
(I use log.info in the sample because I executed in soapui groovy testStep)
Hope this helps,

Resources