Make a reflection utility ②

This time, it is a method to fill the field of the argument object with an appropriate value. In JUnit, you often say, "I want to fill the field for a certain object for the time being, but it's too annoying to call the setter one by one!" In such a case, I created a method that fills the fields appropriately.

What I made last time → The one that executes private methods

Created utility method

ReflectionUtil.java


public static void fillAllFields(Object obj, int offset) throws IllegalArgumentException, IllegalAccessException {
	Assertions.assertThat(offset >= 0);
	boolean b = true;
	char c=(char) (65 + offset); // A+
	
	Field[] fieldList = obj.getClass().getDeclaredFields();
	for (int i = 0; i < fieldList.length; i++) {
		int num = i + offset;
		Field f = fieldList[i];
		f.setAccessible(true);
		String name = f.getName();
		Class<?> clazz = f.getType();

		if (Modifier.isFinal(f.getModifiers())) {
			continue;
		}
		if (clazz == String.class) {
			f.set(obj, name + num);
		} else if (clazz == Date.class) {
			Calendar calendar = Calendar.getInstance();
			int d = num % 28;
			calendar.set(2019, Calendar.MARCH, d);
			f.set(obj, calendar.getTime());
		} else if (clazz == Timestamp.class) {
			Calendar calendar = Calendar.getInstance();
			int d = num % 28;
			calendar.set(2019, Calendar.MARCH, d);
			f.set(obj, new Timestamp(calendar.getTimeInMillis()));
		} else if(clazz==boolean.class ||clazz==Boolean.class ){
			f.set(obj, b);
			b=!b;
		} else if(clazz==BigDecimal.class){
			f.set(obj, new BigDecimal(num));
		} else if(clazz==int.class||clazz==Integer.class){
			f.set(obj, num);
		} else if(clazz==long.class||clazz==Long.class){
			f.set(obj, ((Integer) num).longValue());
		} else if(clazz==float.class||clazz==Float.class){
			f.set(obj, ((Integer) num).floatValue());
		} else if(clazz==double.class||clazz==Double.class){
			f.set(obj, ((Integer) num).doubleValue());
		} else if(clazz==short.class||clazz==Short.class){
			f.set(obj, ((Integer) num).shortValue());
		} else if(clazz==byte.class||clazz==Byte.class){
			f.set(obj, ((Integer) num).byteValue());
		} else if(clazz==char.class||clazz==Character.class){
			if (c < 65 || 90 < c) c = 65;
			f.set(obj, ((char) c));
			c++;
		}
	}
}

Try to move

Prepare a suitable POJO

EntityA.java


@Data
public class EntityA {
	private String str;
	private Date date;
	private Timestamp ts;
	private Boolean bool;
	private BigDecimal bd;
	private Integer i;
	private long l;
	private Float f;
	private double d;
	private Short s;
	private byte b;
	private Character c;
}

Test method

Tests.java


@Test
public void test1() {
	EntityA entity= new EntityA();
	try {
		ReflectionUtil.fillAllFields(entity, 25);
		System.out.println(entity);
	} catch (IllegalArgumentException | IllegalAccessException e) {
		e.printStackTrace();
		fail();
	}
}

Execution result

console


EntityA(str=str25, date=Tue Mar 26 02:05:01 JST 2019, ts=2019-03-27 02:05:01.786, bool=true, bd=29, i=30, l=31, f=32.0, d=33.0, s=34, b=35, c=Z)

Afterword

What do you do with miso in the case of char? This method focuses on the uppercase alphabets A-Z. Other classes will be added as needed. I'm sorry if there is a bug because I have not verified the boundary value.

It's become a legacy Java method, but I really wanted to incorporate Java 8 as much as possible.

Or is it likely that such a utility is in some library in the first place? ??

Recommended Posts

Make a reflection utility ②
Make a reflection utility ③
Make a reflection utility ①
[Java] Make it a constant
[Rails] Make a breadcrumb trail
Make a rhombus using Java
Make a digging maze with Ruby2D
Make a language! (Making a simple calculator ②)
Try to make a simple callback
Make a slideshow tool with JavaFX
How to make a Java container
Make a Christmas tree with swift
How to make a JDBC driver
Priority Queue max Make it a queue
Make a language! (JavaCC environment construction)
Make a garbage reminder with line-bot-sdk-java
Make a list map with LazyMap
A murmur about the utility class
Make a language! (Making a simple calculator ①)
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
Make a SOAP call in C #
Try to make a peepable iterator
How to make a Java array
Make a typing game with ruby
How to make a Java calendar Summary
Make a note of Ruby keyword arguments
Let's make a Christmas card with Processing!
Make a family todo list with Sinatra
How to make a Discord bot (Java)
Make a family todo list with Sinatra
Let's make a smart home with Ruby!
[docker] [nginx] Make a simple ALB with nginx
Make a site template easily with Rails
[Java] Let's make a DB access library!