Generic way to replace an object in it's own method
Well, the upcase!
method doesn't change the object identity, it only changes its internal structure (s.object_id == s.upcase!.object_id
).
On the other hand, numbers are immutable objects and therefore, you can't change their value without changing their identity. AFAIK, there's no way for an object to self-change its identity, but, of course, you may implement positify!
method that changes properties of its object - and this would be an analogue of what upcase! does for strings.
Convert java legacy code to generic - how to replace Object with type?
That's indeed not possible. You'll need to pass the "concrete" T
somehow as method argument so that the actual type is known during runtime. Commonly used approach is passing it as Class<T>
, so that you can make use of Class#cast()
:
<T> T getCacheValue(String name, Class<T> type) {
return type.cast(getServletContext().getAttribute(name));
}
You can use it as follows:
Double value = getCacheValue("x", Double.class);
How to change my method to a generic method?
private static <T> T getJSONObjectData(JSONObject result, String key, Class<T> type)
{
Object value = result.get(key);
return type.cast(value);
}
What you must be aware of:
- A
JSONException
will bubble up ifkey
doesn't exist inresult
- A
ClassCastException
will bubble up iftype
doesn't match the real type ofvalue
Feel free to handle these a level above if necessary.
Replace an element of an object by one of its own sub-elements
You could use Array#forEach
and change the object in situ, because you need not to return a new array, while you already mutate the original object of the array.
let list = [{ a: { b: 'foo' } }, { a: { b: 'bar' } }];
list.forEach(d => d.a = d.a.b);
console.log(list);
Is there a clean way to replace all attribute values of an object in Java with values from another object of the same type?
For mapping between two Java objects you can use something like MapStruct or you can write your own mappers, essentially extracting the boilerplate code into a separate class/service.
If you decide to create your own mapper, you can use reflection or you can do it by using setter
methods or builder pattern (also Lombok's @Builder
can help avoiding to write the boilerplate for builder pattern).
...now lets hypothetically say our project grew a lot and we have to add 20 more attributes to our Student class. Suddenly we have to update this method manually as well as any other method that does a similar thing.
Certainly your entities and consequently your databases can grow in the number of fields/columns, but this in general should not be as big of a deal if you extract all the mappings and transformations in different classes. In most of the cases you will have to update a subset of the fields anyway.
Write a generic method to replace a family of legacy API methods
Seems to me you don't need a generic method at all, just a wrapper method for each supported argument type:
public IntExp GetExpression(int value)
{
return GetIntExp(value);
}
public StringExp GetExpression(string value)
{
return GetStringExp(value);
}
and so on (assuming your goal is to use the same name for all the ways of getting an expression).
Generic Extension Method for change value of any variable or object
What you want is impossible, as an extension-method allways works on a specific instance of a type. As every other method as well, a method can not change the instance to be something different (which would mean reference another instance), it can only modify that instance by calling any of its members.
If you want to know why it is forbidden to use ref
on an extension-method, look this similar question: Impossible to use ref and out for first ("this") parameter in Extension methods?.
On the other hand you can do that with normal methods using the ref
-keyword, though.
public static void Set<T>(ref T instance)
{
instance = new Whatever(...);
}
Now you may be able to use this:
var n = 0;
TheClass.Set(ref n);
However if all you want to do in that method is replacing one reference by another one, why not just use this instead?
var n = 0;
n = 1;
Related Topics
Net-Ssh and Remote Environment
Accessing the Child Instance in a Rabl Template
What's the Difference Between the Ruby Irb Prompt Modes
Routing Error No Route Matches [Get] "/Static_Pages/Home", Tutorial
Get All Keys in Hash with Same Value
Why Does the Rand() Return Always the Same Number
In Rspec, Using Let Variable Inside Before :All Block
Is There a Groovy Equivalent of the Ruby Timeout Module
Activerecord_Postgis_Adapter: Undefined Method 'Point' for Nil:Nilclass
Rails 7 Signup Form Doesn't Show Error Messages
How to Include Ё in [А-Я] Regexp Char Interval
Building a Simple Search Form in Rails
Can't Setup Ruby Environment - Installing Fii Gem Error
Rails - Multi Tenant Application with Customization Framework
Rails Activeadmin Drop Down Menu on New and Edit Forms