JavaQualifiedName implements QualifiedName { @Deprecated public static JavaQualifiedName ofString(String name) { return QualifiedNameFactory.ofString(name); }  @Override abstract JavaTypeQualifiedName getClassName(); @Deprecated boolean isLocalClass(); @Deprecated String getClassSimpleName(); @Deprecated boolean isUnnamedPackage(); @Deprecated String getOperation(); @Deprecated String[] getPackages(); @Deprecated String[] getClasses(); @Override final boolean equals(Object o); @Override final int hashCode(); @Override final String toString(); @Deprecated static JavaQualifiedName ofString(String name); @Deprecated static JavaQualifiedName ofClass(Class<?> clazz);  }
@Test public void testParseClass() { JavaTypeQualifiedName outer = (JavaTypeQualifiedName) QualifiedNameFactory.ofString("foo.bar.Bzaz"); JavaTypeQualifiedName nested = (JavaTypeQualifiedName) QualifiedNameFactory.ofString("foo.bar.Bzaz$Bolg"); assertEquals(1, outer.getClassList().size()); assertEquals("Bzaz", outer.getClassList().get(0)); assertEquals(2, nested.getClassList().size()); assertEquals("Bzaz", nested.getClassList().get(0)); assertEquals("Bolg", nested.getClassList().get(1)); }
@Test public void testParsePackages() { JavaTypeQualifiedName packs = (JavaTypeQualifiedName) QualifiedNameFactory.ofString("foo.bar.Bzaz$Bolg"); JavaTypeQualifiedName nopacks = (JavaTypeQualifiedName) QualifiedNameFactory.ofString("Bzaz"); assertNotNull(packs.getPackageList()); assertEquals("foo", packs.getPackageList().get(0)); assertEquals("bar", packs.getPackageList().get(1)); assertTrue(nopacks.getPackageList().isEmpty()); }
@Test public void testParseLambdaInEnumConstant() { final String LAMBA_IN_ENUM_CONSTANT = "package foo; import java.util.function.Function; enum Bar { CONST(e -> e); Bar(Function<Object,Object> o) {} }"; final String QNAME = "foo.Bar#lambda$static$0"; ASTLambdaExpression node = getNodes(ASTLambdaExpression.class, LAMBA_IN_ENUM_CONSTANT).get(0); assertNotNull(node); assertEquals(QualifiedNameFactory.ofString(QNAME), node.getQualifiedName()); }
@Test public void testParseMalformed() { assertNull(QualifiedNameFactory.ofString(".foo.bar.Bzaz")); assertNull(QualifiedNameFactory.ofString("foo.bar.")); assertNull(QualifiedNameFactory.ofString("foo.bar.Bzaz#foo")); assertNull(QualifiedNameFactory.ofString("foo.bar.Bzaz()")); assertNull(QualifiedNameFactory.ofString("foo.bar.Bzaz#foo(String,)")); assertNull(QualifiedNameFactory.ofString("foo.bar.Bzaz#foo(String , int)")); assertNull(QualifiedNameFactory.ofString("foo.bar.Bzaz#lambda$static$23(String)")); assertNull(QualifiedNameFactory.ofString("foo.bar.Bzaz#lambda$static$")); }
@Test public void testSimpleLocalClass() { final String TEST = "package bar; class Boron { public void foo(String j) { class Local {} } }"; List<ASTClassOrInterfaceDeclaration> classes = getNodes(ASTClassOrInterfaceDeclaration.class, TEST); JavaQualifiedName qname = QualifiedNameFactory.ofString("bar.Boron$1Local"); assertEquals(qname, classes.get(1).getQualifiedName()); }
@Test public void testLocalClassNameClash() { final String TEST = "package bar; class Bzaz{ void foo() { class Local {} } { List<ASTClassOrInterfaceDeclaration> classes = getNodes(ASTClassOrInterfaceDeclaration.class, TEST); assertNotEquals(classes.get(1).getQualifiedName(), classes.get(2).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("bar.Bzaz$1Local"), classes.get(1).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("bar.Bzaz$2Local"), classes.get(2).getQualifiedName()); }
@Test public void testLocalClassDeepNesting() { final String TEST = "class Bzaz{ void foo() { " + " class Local { " + " class Nested {" + " {" + " class InnerLocal{}" + " }" + " }" + " }" + "}}"; List<ASTClassOrInterfaceDeclaration> classes = getNodes(ASTClassOrInterfaceDeclaration.class, TEST); assertNotEquals(classes.get(1).getQualifiedName(), classes.get(2).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz$1Local"), classes.get(1).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz$1Local$Nested"), classes.get(2).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz$1Local$Nested$1InnerLocal"), classes.get(3).getQualifiedName()); }
@Test public void testMultipleAnonymousClasses() { final String TEST = "class Bzaz{ void foo() { " + " new Runnable() {" + " public void run() {}" + " };" + " new Runnable() {" + " public void run() {}" + " };" + "}}"; List<ASTAllocationExpression> classes = getNodes(ASTAllocationExpression.class, TEST); assertNotEquals(classes.get(0), classes.get(1)); assertEquals(QualifiedNameFactory.ofString("Bzaz$1"), classes.get(0).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz$2"), classes.get(1).getQualifiedName()); }
@Test public void testNestedAnonymousClass() { final String TEST = "class Bzaz{ void foo() {" + " new Runnable() {" + " public void run() {" + " new Runnable() {" + " public void run() {}" + " };" + " }" + " };" + "}}"; List<ASTAllocationExpression> classes = getNodes(ASTAllocationExpression.class, TEST); assertNotEquals(classes.get(0), classes.get(1)); assertEquals(QualifiedNameFactory.ofString("Bzaz$1"), classes.get(0).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz$1$1"), classes.get(1).getQualifiedName()); }
@Test public void testLocalInAnonymousClass() { final String TEST = "class Bzaz{ void foo() {" + " new Runnable() {" + " public void run() {" + " class FooRunnable {}" + " }" + " };" + "}}"; List<ASTClassOrInterfaceDeclaration> classes = getNodes(ASTClassOrInterfaceDeclaration.class, TEST); assertTrue(classes.get(1).isLocal()); assertEquals(QualifiedNameFactory.ofString("Bzaz$1$1FooRunnable"), classes.get(1).getQualifiedName()); }
@Test public void testLambdaInStaticInitializer() { final String TEST = "import java.util.function.*;" + "class Bzaz{ " + " static {" + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " l.accept(\"foo\");" + " }" + "}"; List<ASTLambdaExpression> lambdas = getNodes(ASTLambdaExpression.class, TEST); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$static$0"), lambdas.get(0).getQualifiedName()); }
@Test public void testLambdaInInitializerAndConstructor() { final String TEST = "import java.util.function.*;" + "class Bzaz{ " + " {" + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " l.accept(\"foo\");" + " }" + " public Bzaz() {" + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " l.accept(\"foo\");" + " }" + "}"; List<ASTLambdaExpression> lambdas = getNodes(ASTLambdaExpression.class, TEST); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$new$0"), lambdas.get(0).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$new$1"), lambdas.get(1).getQualifiedName()); }
@Test public void testLambdaField() { final String TEST = "import java.util.function.*;" + "public class Bzaz { " + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " public static Consumer<String> k = s -> {" + " System.out.println(s);" + " };" + "}"; List<ASTLambdaExpression> lambdas = getNodes(ASTLambdaExpression.class, TEST); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$new$0"), lambdas.get(0).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$static$1"), lambdas.get(1).getQualifiedName()); }
@Test public void testLambdaInterfaceField() { final String TEST = "import java.util.function.*;" + "public interface Bzaz { " + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " public static Consumer<String> k = s -> {" + " System.out.println(s);" + " };" + "}"; List<ASTLambdaExpression> lambdas = getNodes(ASTLambdaExpression.class, TEST); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$static$0"), lambdas.get(0).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$static$1"), lambdas.get(1).getQualifiedName()); }
@Test public void testLambdaLocalClassField() { final String TEST = "import java.util.function.*;" + "public class Bzaz { " + " public void boo() {" + " class Local {" + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " }" + " }" + "}"; List<ASTLambdaExpression> lambdas = getNodes(ASTLambdaExpression.class, TEST); assertEquals(QualifiedNameFactory.ofString("Bzaz$1Local#lambda$Local$0"), lambdas.get(0).getQualifiedName()); }
@Test public void testLambdasInMethod() { final String TEST = "import java.util.function.*;" + "class Bzaz{ " + " public void bar() {" + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " l.accept(\"foo\");" + " }" + " public void fooBar() {" + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " l.accept(\"foo\");" + " }" + " public void gollum() {" + " Consumer<String> l = s -> {" + " System.out.println(s);" + " };" + " l.accept(\"foo\");" + " }" + "}"; List<ASTLambdaExpression> lambdas = getNodes(ASTLambdaExpression.class, TEST); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$bar$0"), lambdas.get(0).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$fooBar$1"), lambdas.get(1).getQualifiedName()); assertEquals(QualifiedNameFactory.ofString("Bzaz#lambda$gollum$2"), lambdas.get(2).getQualifiedName()); }