Is it possible to get the variable name of the current instance in the object's setter?
Something like this
public class Class {
private DataType dataType;
}
public class DataType {
public void setValue(String value) {
if (variableName is 'dataType') {
this.value = value;
} else {
this.value = null;
}
}
}
If it's not possible using standard utilities then it's possible to create some kind of annotation to store there variable name and then use it in setter?
When I try to do like this - the annotation is null.
I create annotation
#Retention(RetentionPolicy.CLASS)
#Target(ElementType.FIELD)
public #interface FieldName {
String fieldName();
}
Then I add it to field
public class Class {
#FieldName(fieldName = "dataType")
private DataType dataType;
}
And when I try to get it in the getter of DataType - the annotation FieldName is null.
private String wrapGetter(String requiredFieldName, String dataTypeField) {
FieldName fieldName = this.getClass().getAnnotation(FieldName.class);
if (fieldName.fieldName().equals(requiredFieldName)) {
return dataTypeField;
} else {
return dataTypeField;
}
}
There are a few problems with what you are trying to do:
Your RetentionPolicy is set it to CLASS which means that class loader will discard it and it will not be available at runtime. You should use RetentionPolicy.RUNTIME instead.
this.getClass().getAnnotation(FieldName.class) will give you the class annotation.
In the below example the annotation is not null and you can get "example" string in setValue method:
#FieldName(fieldName = "example")
public class DataType {
public void setValue(String value) {
System.out.println(this.getClass().getAnnotation(FieldName.class));
}
}
public static void main(String[] args) {
new DataType().setValue("ignored");
}
This also requires to change the target of your annotation to #Target(ElementType.TYPE).
Variable or field name is just a reference, it points to some object in the memory. You can have multiple references to the same object. Although you can annotate fields in different classes, it will be a problem with local variables and parameters - it is difficult to say as I don't know what you are trying to achieve.
Problematic example:
public class ClassOne {
#FieldName(fieldName = "dataType")
private DataType a;
}
public class ClassTwo {
#FieldName(fieldName = "dataType")
private DataType b;
}
public class ClassThree {
public void doSomething() {
DataType c = new DataType();
}
}
public class ClassFour {
public void doSomething(DataType d) {
// ...
}
}
Generally, it all comes down to the problem that the instance of the class has no information about how it is being referred to. However, for the field, enclosing class has this information. Consider moving your method into that class. And you can handle this without any annotations:
public class DataType {
public void setValue(String value) {
// ...
}
}
public class ClassOne {
private DataType dataType;
public void setDataTypeValue(String value) {
dataType.setValue(value);
}
}
public class ClassTwo {
private DataType anyOtherFieldName;
public void setDataTypeValue(String value) {
anyOtherFieldName.setValue(null);
}
}
The setter that sets null and ignores the parameter is very misleading, your IDE should be giving you a warning about unused parameter, it's not without a reason. I think you should consider a redesign, but I cannot advice you further without knowing more details.
Instead of solving the problem, try to solve the cause of that problem.
Use #Retention(RetentionPolicy.RUNTIME)
Replace this
FieldName fieldNameAnnotation = field.getAnnotation(FieldName.class);
With this
Field field = this.getClass().getField("dataType");
FieldName fieldName = field.getAnnotation(FieldName.class);
Related
I have classes similar to DataRequest & DataWithIdRequest. DataWithIdRequest gets passed into my controller method. I want to pass the subclass object ONLY to another class for processing. However, when I try to downcast to DataRequest the extra field is still showing. How can I accomplish this?
public class DataRequest {
private String name;
public String getName() {
return name;
}
public void setFirstName(String name) {
this.name = name;
}
}
public class DataWithIdRequest extends DataRequest {
private Integer id;
public Integer getId() {
return contractKey;
}
public void setContractKey(Integer id) {
this.id = id;
}
}
//controller
processData(request);
}
//domain class
public Boolean processData(DataRequest request) {
//request here has DataWithIdRequest field
//but I only want the subclass
}
public Boolean processData(DataRequest request) { }
Because in your method processData, request's type is DataRequest. You want it to be DataWithIdRequest.
public Boolean processData(DataWithIdRequestrequest request) { }
You can only offer a part of an API (application programmer's interface), by separating the code in an interface.
public interface Identified {
Integer getId();
public void setContractKey(Integer id);
}
public class DataWithIdRequest extends DataRequest implements Identified {
private Integer id;
#Override
public Integer getId() {
return contractKey;
}
#Override
public void setContractKey(Integer id) {
this.id = id;
}
}
public Boolean processData(DataRequest request) {
if (request instanceOf Identified identified) {
identified.setContractKey(13);
}
}
Or move the problem to the caller:
public Boolean processData(Identified request) {
request.setContractKey(13);
}
By the way it more usual to use int, boolean, the primitive types.
To hide the information completely, you need to create a new instance of DataRequest by DataWithIdRequest (that's why mapping library like mapstruct is useful), but not directly passing it.
Explanation:
This is how inheritance works, imagine a method takes a parameter of an interface or abstract class, by using instanceof inside the method we can check the object actual type and do something specific. e.g.
public void drawShape(Shape shape) {
if (shape instanceof Circle) {
Circle circle = (Circle) shape;
// do sth
} else if (shape instanceof Square) {
// do sth else
}
}
The above example is completely valid (although not a good programming style, that's another story).
The object inside the memory holds all the actual class details, passing it to a method doesn't change anything to the memory. The parameter (e.g. shape) is only another reference to the same memory location.
Maybe I don't understand the question fully but: as you want I it's not possible since:
public class DataWithIdRequest extends DataRequest
means DataRequest is a subset of DataWithIdRequest, it's an intersection.
You unfortunately need to find an other way
package com.pr.trio;
import java.util.List;
public class lalala {
private List<SegmentationFieldValue> segmentationFieldValues;
public static class SegmentationFieldValue {
private Integer segmentationFieldId;
private Integer segmentationFieldGroupId;
private String value;
public Integer getSegmentationFieldId() {
return segmentationFieldId;
}
public void setSegmentationFieldId(Integer segmentationFieldId) {
this.segmentationFieldId = segmentationFieldId;
}
public Integer getSegmentationFieldGroupId() {
return segmentationFieldGroupId;
}
public void setSegmentationFieldGroupId(Integer segmentationFieldGroupId) {
this.segmentationFieldGroupId = segmentationFieldGroupId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public List<SegmentationFieldValue> getSegmentationFieldValues() {
return segmentationFieldValues;
}
public void setSegmentationFieldValues(List<SegmentationFieldValue> segmentationFieldValues) {
this.segmentationFieldValues = segmentationFieldValues;
}
}
package com.pr.trio;
import java.util.Arrays;
public class kk {
public static void main(String[] args) {
lalala l1 = new lalala();
//currently passed as an empty array, want to set SegmentationFieldId & value here from inner class
l1.setSegmentationFieldValues(Arrays.asList());
//lalala.SegmentationFieldValue.this.setSegmentationFieldId(15);
System.out.println(l1.getSegmentationFieldValues());
}
}
So here, I'm not able to pass values for the segmentation field instead of the empty array, gives an error. So how can I set the values from the inner class fields & pass it to my list?
Seeing as your SegmentationFieldValue class is public, it's trivial to use it inside another class, there are basically two ways to go about this:
The first is to import the inner class:
import com.pr.trio.lalala.SegmentationFieldValue;
The second is to qualify the classname whenever you use it:
lalala.SegmentationFieldValue a = new lalala.SegmentationFieldValue();
You can then call the setters on this class, and use the objects in your call to setSegmentationFieldValues:
lalala.SegmentationFieldValue a = new lalala.SegmentationFieldValue();
a.setSegmentationFieldId(1);
a.setSegmentationFieldGroupId(1);
a.setValue("a");
lalala.SegmentationFieldValue b = new lalala.SegmentationFieldValue();
b.setSegmentationFieldId(2);
b.setSegmentationFieldGroupId(1);
b.setValue("b");
l1.setSegmentationFieldValues(Arrays.asList(a, b));
Judging from your comment code, you also seem to be looking for a shorthand way to add an element to your list. A simple implementation could look like this (in class lalala):
public void addSegmentationFieldValue(Integer id, Integer groupId, String value)
{
if (segmentationFieldValues == null)
{
segmentationFieldValues = new ArrayList<>();
}
SegmentationFieldValue result = new SegmentationFieldValue();
result.setSegmentationFieldId(id);
result.setSegmentationFieldGroupId(groupId);
result.setValue(value);
segmentationFieldValues.add(result);
}
After which you can do the following in the main method of k1:
l1.addSegmentationFieldValue(1, 1, "a");
I want to create a method, that:
Takes the type of an enum and a String as arguments
The String is the name of one specific enum instance
Returns the enum instance that fits that name.
What I have tried:
In class TestUtil.java:
public static <E extends Enum<E>> E mapToEnum(Enum<E> mappingEnum, String data) {
return mappingEnum.valueOf(E, data); // Not working, needs Class of Enum and String value
}
The enum:
public enum TestEnum {
TEST1("A"),
TEST2("B");
private String value;
private TestEnum(String value) {
this.value = value;
}
}
How it should work (For example in main method):
TestEnum x = TestUtil.mapToEnum(TestEnum.class, "TEST1"); // TEST1 is the name of the first enum instance
The problem is, that I can't figure out what I need to pass into the mapToEnum method, so that I can get the valueOf from that Enum.
If the code you provided is acceptable:
public static <E extends Enum<E>> E mapToEnum(Enum<E> mappingEnum, String data) {
return mappingEnum.valueOf(E, data); // Not working, needs Class of Enum and String value
}
Then all you have to do is fix it.
Here's the code I tested:
static <T extends Enum<T>> T mapToEnum(Class<T> mappingEnum, String data) {
return Enum.valueOf(mappingEnum, data);
}
Usage:
#Test
public void test() {
TestEnum myEnum = mapToEnum(TestEnum.class, "TEST1");
System.out.println(myEnum.value); //prints "A"
}
Strongly suggest using Apache commons-lang library for boiler plate function like this ...
TestEnum x = EnumUtils.getEnum(TestEnum.class, "TEST1");
... which is exactly the code #Fenio demonstrates but handles null or wrong input with a null instead of throwing an Exception.
If you didn't know about this then check out what the rest of the lang3 library holds. I view it as a de-facto standard, saving millions of devs from re-writing minor plumbing utilities.
This is how you can iterate the enum class value and match with the parameter you have passed in the method, please check the below-mentioned code.
enum TestEnum {
TEST1("test1"),
TEST2("test2");
private String value;
private TestEnum(String value) {
this.value = value;
}
public String getName() {
return value;
}
public static TestEnum mapToEnum(String data) {
for (TestEnum userType : TestEnum.values()) {
if (userType.getName().equals(data)) {
return userType;
}
}
return null;
}
}
I have a Spring Boot project and I am trying to recovery a value of a .properties file and use it inside an enum.
I follow the next steps.
I have a file application.properties with a value = 000. I recovery this value with:
#RestController
public class ReadProperty {
#Value("${value}")
public String value;
public String getValueProperty() {
return this.value;
}
#GetMapping(value = "/get")
public String getValue() {
System.out.println("read value is " + value);
return value;
}
#GetMapping(value = "/getEnum")
public String getValueEnum() {
String response = EnumInsideAClass.TEST.toString();
System.out.println("response value is " + response);
return response;
}
}
If I use value in a class I get the value 000 without any problem.
I create an enum inside a class like this
public class EnumTest {
#Autowired
static ReadProperty readProperty;
public final static String VAL = readProperty.getValueProperty();
public enum EnumInsideAClass {
TEST(VAL);
private String value;
private EnumInsideAClass(String value) {
this.value = value;
}
#Override
public String toString() {
return this.value;
}
}
}
The problem is that inside the enum class I need a static variable. If I use
#Value("${value}")
public static String value;
The value change to null.
So I tried to use access the value by a get method but the value is still null.
I am out of ideas, what can I do? Is posible use a propertie value inside an enum?
Thanks a lot
The problem you're describing has nothing to do with the #Value being in the enum but rather it has to do with your attempt to inject the property's value on a static variable.
Spring will let you inject values directly on non-static and not on static fields. If you want to do this in your case, you could potentially proxy the injection through a setter method e.g:
public static String value;
#Value("${value}")
public void setValue(String someValue) {
SomeClass.value = someValue;
}
But I would advise you to be very aware of what you're doing. Since this is going to be a non-final field that is also public and static you need to be aware of who's allowed to access it and also who's allowed to change it's value.
Javadoc for Class.getFields() say: "The elements in the array returned are not sorted and are not in any particular order."
Any hints on how the order actually is determined? Is it possible that when I execute this method twice, I get fields in different order? In other words, is the order stable for given compiled class, or even between compilations of the same source file?
It should be stable, and for Oracle's JVM its the order they are declared, but you should not rely on this.
You should base lookup on the field's name (and possibly declaring class) rather than position.
On my JVM, at least,
Class.getFields() returns fields in declaration order.
Class.getMethods(), on the other hand, doesn't always. It returns them in (I believe) the order the classloader sees the strings. So if two classes have the same method name, the second-loaded class will return the shared method name before its other methods.
javap confirms the compiler wrote both fields and methods in declaration order.
See the output of this code sample.
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class OrderTest {
public static void main(String[] args) {
// fields are in declaration order
for (Field field : C1.class.getDeclaredFields()) {
System.out.println(field.getName());
}
for (Field field : C2.class.getDeclaredFields()) {
System.out.println(field.getName());
}
// methods, on the other hand, are not necessarily in declaration order.
for (Method method : C1.class.getDeclaredMethods()) {
System.out.println(method.getName());
}
for (Method method : C2.class.getDeclaredMethods()) {
System.out.println(method.getName());
}
}
}
class C1 {
public int foo;
public int bar;
public int getFoo() { return foo; }
public int getBar() { return bar; }
}
class C2 {
public int bar;
public int foo;
public int getBar() { return bar; }
public int getFoo() { return foo; }
}
on my JVM (1.7.0_45, Windows) this returns
foo
bar
bar
foo
getFoo
getBar
getFoo
getBar
Create a helper method that returns a sorted list, and use that instead whenever you need the list of fields. Or lookup by name instead of index.
An natural order of properties offers the Ujorm framework with its key-value objects using the readKeys() method.
Each item of the result have got similar features like the Field including reading and writting values from/to the object. For example see the next code:
public class User extends AbstractUjo implements Serializable {
/** Factory */
private static final KeyFactory<User> f = newFactory(User.class);
/** Keys: */
public static final Key<User, Long> PID = f.newKey();
public static final Key<User, Integer> CODE = f.newKey();
public static final Key<User, String> NAME = f.newKey();
public static final Key<User, Double> CASH = f.newKey();
static {
f.lock();
}
// Setters:
public void setPid(Long pid) {
PID.setValue(this, pid);
}
public void setCode(Integer code) {
CODE.setValue(this, code);
}
public void setName(String name) {
NAME.setValue(this, name);
}
public void setCash(Double cash) {
CASH.setValue(this, cash);
}
// Getters ...
}
The natural order of keys can be iterated by:
for (Key key : new User().readKeys()) {
System.out.println("Key: " + key);
}
See the documentation for more information.