[JAVA] Come out with a suffix on the method

Introduction

This article is an article that releases the stress of one SE. Depending on the content, it may be useful for someone.

What are you doing?

For RDBMS that failed to normalize, use ~~ reflection to refresh the source to relieve stress. I don't think about performance.

What it really is

First of all, I would like to say that I do not want to criticize legacy technologies such as hosts. Segment at the host? It seems that you often add a suffix to. It seems that the number of host ⇛ Web downsizing cases is decreasing these days, but it is a personal stress release for the normalization failure that I recently saw in the downsizing case.

At the time of downsizing, items such as items 01 to 05 were brought into one table as the host thought, which is the root of all evil.

Example) TBL name Account management store table Column Store code (PK) Branch 01 Branch 02 Branch 03 Branch 04 Branch 05

I wanted to have a branch office hanging under the parent store that manages the account. However, it should be normalized properly to create a "branch table that hangs from the parent store code". It's very vulnerable to changes when the number of branches increases.

Although

I am also an engineer. And since I am hired, I am an adult (mid 30) who tries to swallow what cannot be changed and aims to respond well.

It looks like this

First, prepare a getter with a suffix. It's just long, so fold it up.

suffixMethods

suffixMethods.java


package suffixMethodsCall;

public class suffixMethods {


	private static String RETURN_CODE_A = "A";
	private static String RETURN_CODE_B = "B";
	private static String RETURN_CODE_C = "C";
	private static String RETURN_CODE_D = "D";
	private static String RETURN_CODE_E = "E";
	private static String RETURN_CODE_F = "F";
	private static String RETURN_CODE_G = "G";
	private static String RETURN_CODE_H = "H";
	private static String RETURN_CODE_I = "I";
	private static String RETURN_CODE_J = "J";

	public String getReturnCode01() {
		return RETURN_CODE_A;
	}

	public String getReturnCode02() {
		return RETURN_CODE_B;
	}

	public String getReturnCode03() {
		return RETURN_CODE_C;
	}

	public String getReturnCode04() {
		return RETURN_CODE_D;
	}

	public String getReturnCode05() {
		return RETURN_CODE_E;
	}

	public String getReturnCode06() {
		return RETURN_CODE_F;
	}

	public String getReturnCode07() {
		return RETURN_CODE_G;
	}

	public String getReturnCode08() {
		return RETURN_CODE_H;
	}

	public String getReturnCode09() {
		return RETURN_CODE_I;
	}

	public String getReturnCode10() {
		return RETURN_CODE_J;
	}
}

I made an execution class.

suffixMethodsCaller.java


package suffixMethodsCall;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class suffixMethodsCaller {

	/**
	 * <pre>
	 *Execute all getters with suffix included in suffixMethods,
	 *Verify that the matchTargetCode at the beginning is included.
	 * </pre>
	 */
	public static void main(String[] args) {

		//Matching string
		String matchTargetCode = "J";
		//Code list obtained from the method with suffix
		List<String> codeList = new ArrayList<String>();

        //Specify the class and method as a character string
		suffixMethods sm = new suffixMethods();
        String clazz = sm.getClass().getName();
        String baseSuffixMethod = "getReturnCode";
        int suffixPartStart = 1;
        int suffixPartEnd = 10;

        //Execute a method with suffix by using reflection
        try {
            Class<?> c = Class.forName(clazz);
            Object myObj = c.newInstance();

            //Loop the suffix part and plunge into the code list
            for ( ; suffixPartStart <= suffixPartEnd; suffixPartStart++) {
                //Execution method settings
                Method m = c.getMethod(baseSuffixMethod + String.format("%02d", suffixPartStart));
                //Stuff into code list
                codeList.add(m.invoke(myObj).toString());
            }
        } catch(ReflectiveOperationException e) {
            e.printStackTrace();
        }

        //Whether the character string to be matched is included in the execution result list of the method with suffix is output as bool
        System.out.println(codeList.contains(matchTargetCode));
	}
}

It's still a bit dirty, but I've achieved my goal for the time being.

In this case, I used 10 methods with suffix as an example, but if the number increases, performance will be a concern.

You can cut out the execution part to another method and make it more refreshing. However, if you access the reflection API or hard-code it and match it all with String.equals, which one is better?

I will verify it again.

at the end

This time I coded it for the purpose of relieving stress, but I haven't had many opportunities to use reflection so far, so it took longer than I expected.

Downsizing and migration PJs are always really annoying. You can do it honestly, but there are many things like that.

Recommended Posts

Come out with a suffix on the method
Come out with a suffix on the method 2
[Ruby] Cut out a string using the slice method
I immediately stumbled on the standard input method with AtCoder.
[Swift 5] Select a date with the IDate Picker on iOS14
Simulate the simplex method with GUI
Put a badge on the icon
Programming with ruby (on the way)
Logic to draw a circle with ASCII art on the console
Connecting to a database with Java (Part 1) Maybe the basic method
My thoughts on the equals method (Java)
Declare a method that has a Java return value with the return value data type
Create a jar file with the command
Invoke the character string passed as an argument as a method with send
A note on the libGDX Utils class
Run a DMN with the Camunda DMN Engine
Use a named format with Ruby's format method
A rudimentary note on the Fibonacci sequence
Code for solving a random number matrix with the Pivot Gauss elimination method
[Rails] When transitioning to a page with link_to, move to the specified location on the page
Hello World, a cross-platform GUI app with Groovy running on the Java platform
A quick note on using jshell with the official Docker image of the JDK
Using templates on the classpath with Apache Velocity
Publish the app made with ruby on rails
Matches annotations on the interface with Spring AOP
Create a multi-key map with the standard library
[Ruby] Exclude duplicate elements with the uniq method.
Determine the current page with Ruby on Rails
Finding pi with the Monte Carlo method? (Ruby)
I made a portfolio with Ruby On Rails
Rspec: I want to test the post-execution state when I set a method on subject
About the method
Proceed with the official Rust documentation on a Docker container (2. Program a number guessing game)
[Java] Cut out a part of the character string with Matcher and regular expression
Refactor the Decorator pattern implementation with a functional interface
Call a method with a Kotlin callback block from Java
The story of making a reverse proxy with ProxyServlet
Create a user with an empty password on CentOS7
Display the list in setDetails on the screen with spring-security
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Challenge the settings for developing with vue.js on Rails 6
Why implement with a singleton instead of a static method
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
How to reduce the load on the program even a little when combining characters with JAVA
[Rails] What to do when the view collapses when a message is displayed with the errors method