Close

Spring MVC - Conventions Examples

Spring MVC 

org.springframework.core.Conventions provides methods to support various naming and other conventions used throughout the framework. In this example we are generating variable name by using getVariableName() method.

package com.logicbig.example;

import org.springframework.core.Conventions;

import java.util.*;

public class VariableNameTest {
public static void main (String[] args) {

Object obj = "test string";
print(obj, "\"test string\"");


obj = new String[]{"one", "two"};
print(obj, "new String[]{\"one\", \"two\"};");

obj = 5;
print(obj, "5");

obj = new Integer(5);
print(obj, "new Integer(5)");

obj = 'z';
print(obj, "'z'");

obj = Arrays.asList(1, 2, 3);
print(obj, "Arrays.asList(1, 2, 3)");

obj = new LinkedList<>(Arrays.asList(1, 2, 3));
print(obj, "new LinkedList<>(Arrays.asList(1, 2, 3))");

obj = new HashSet<>(Arrays.asList("1", "2"));
print(obj, "new HashSet<>(Arrays.asList(\"1\", \"2\"))");

obj = new HashMap<String, Integer>();
print(obj, "new HashMap<String, Integer>()");

obj = new LinkedHashMap<>();
print(obj, "new LinkedHashMap<>()");

obj = new TreeMap<>();
print(obj, "new TreeMap<>()");

obj = new MyNestedObject();
print(obj, "MyNestedObject()");

obj = new TheController();
print(obj, "new TheController()");

//a collection cannot be empty (map is ok)
// following will throw IllegalArgumentException,
/* Set<Integer> integerTreeSet = new TreeSet<>();
name = Conventions.getVariableName(integerTreeSet);
print("Set<Integer> integerTreeSet = new TreeSet<>();", name);*/

}

private static void print (Object obj, String declaration) {
String name = Conventions.getVariableName(obj);
System.out.printf("%40s: %s%n", declaration, name);
}

private static class MyNestedObject {
}
}
Original Post




See Also