Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java type name, simple name and canonical name example
package pakage.subpackage.evensubberpackage;
import org.junit.Assert;
import org.junit.Test;
public class WhatIsMyName {
@Test
public void classHasName() {
final Class<?> klass = WhatIsMyName.class;
final String simpleNameExpected = "WhatIsMyName";
Assert.assertEquals(simpleNameExpected, klass.getSimpleName());
final String nameExpected = "pakage.subpackage.evensubberpackage.WhatIsMyName";
Assert.assertEquals(nameExpected, klass.getName());
Assert.assertEquals(nameExpected, klass.getCanonicalName());
}
@Test
public void arrayHasName() {
final Class<?> klass = WhatIsMyName[].class;
final String simpleNameExpected = "WhatIsMyName[]";
Assert.assertEquals(simpleNameExpected, klass.getSimpleName());
final String nameExpected = "[Lpakage.subpackage.evensubberpackage.WhatIsMyName;";
Assert.assertEquals(nameExpected, klass.getName());
final String canonicalNameExpected = "pakage.subpackage.evensubberpackage.WhatIsMyName[]";
Assert.assertEquals(canonicalNameExpected, klass.getCanonicalName());
}
class NestedClass {
}
@Test
public void nestedClassHasName() {
final Class<?> klass = NestedClass.class;
final String simpleNameExpected = "NestedClass";
Assert.assertEquals(simpleNameExpected, klass.getSimpleName());
final String nameExpected = "pakage.subpackage.evensubberpackage.WhatIsMyName$NestedClass";
Assert.assertEquals(nameExpected, klass.getName());
final String canonicalNameExpected = "pakage.subpackage.evensubberpackage.WhatIsMyName.NestedClass";
Assert.assertEquals(canonicalNameExpected, klass.getCanonicalName());
}
@Test
public void methodClassHasName() {
class MethodClass{};
final Class<?> klass = MethodClass.class;
final String simpleNameExpected = "MethodClass";
Assert.assertEquals(simpleNameExpected, klass.getSimpleName());
final String nameExpected = "pakage.subpackage.evensubberpackage.WhatIsMyName$1MethodClass";
Assert.assertEquals(nameExpected, klass.getName());
final String canonicalNameExpected = null;
Assert.assertEquals(canonicalNameExpected, klass.getCanonicalName());
}
@Test
public void anonymousClassHasName() {
final Class<?> klass = new Object(){}.getClass();
final String simpleNameExpected = "";
Assert.assertEquals(simpleNameExpected, klass.getSimpleName());
final String nameExpected = "pakage.subpackage.evensubberpackage.WhatIsMyName$1";
Assert.assertEquals(nameExpected, klass.getName());
final String canonicalNameExpected = null;
Assert.assertEquals(canonicalNameExpected, klass.getCanonicalName());
}
@Test
public void intClassHasName() {
final Class<?> klass = int.class;
final String intNameExpected = "int";
Assert.assertEquals(intNameExpected, klass.getSimpleName());
Assert.assertEquals(intNameExpected, klass.getName());
Assert.assertEquals(intNameExpected, klass.getCanonicalName());
}
@Test
public void intArrayClassHasName() {
final Class<?> klass = int[].class;
final String simpleNameExpected = "int[]";
Assert.assertEquals(simpleNameExpected, klass.getSimpleName());
final String nameExpected = "[I";
Assert.assertEquals(nameExpected, klass.getName());
final String canonicalNameExpected = "int[]";
Assert.assertEquals(canonicalNameExpected, klass.getCanonicalName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.