From 28c78e309b71003917a3bc70427a249846868d4e Mon Sep 17 00:00:00 2001 From: Jan Peter Stotz Date: Thu, 7 Nov 2024 16:07:19 +0100 Subject: [PATCH] getJavaStyleName() and JavaDoc. fixes #2120 --- src/main/java/soot/SootClass.java | 26 ++++++++++-- src/test/java/soot/BasicTest.java | 66 +++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/test/java/soot/BasicTest.java diff --git a/src/main/java/soot/SootClass.java b/src/main/java/soot/SootClass.java index af20b448202..c334429f385 100644 --- a/src/main/java/soot/SootClass.java +++ b/src/main/java/soot/SootClass.java @@ -909,12 +909,17 @@ public boolean isInnerClass() { } /** - * Returns the name of this class. + * Returns the full name of this class (including package). */ public String getName() { return name; } + /** + * Returns the full name of this class (including package). + * + * Considers Dava {@link PackageNamer} fixed names. + */ public String getJavaStyleName() { if (PackageNamer.v().has_FixedNames()) { if (fixedShortName == null) { @@ -923,12 +928,17 @@ public String getJavaStyleName() { if (!PackageNamer.v().use_ShortName(getJavaPackageName(), fixedShortName)) { return getJavaPackageName() + '.' + fixedShortName; } - return fixedShortName; + return getPackageName() + '.' + fixedShortName; } else { - return shortName; + return name; } } + /** + * Returns the name of this class without package as returned by {@link Class#getSimpleName()} + * + * Considers Dava {@link PackageNamer} fixed names. + */ public String getShortJavaStyleName() { if (PackageNamer.v().has_FixedNames()) { if (fixedShortName == null) { @@ -940,6 +950,9 @@ public String getShortJavaStyleName() { } } + /** + * Returns the name of this class without package as returned by {@link Class#getSimpleName()} + */ public String getShortName() { return shortName; } @@ -951,6 +964,13 @@ public String getPackageName() { return packageName; } + /** + * Get package name of this class. + * + * Considers Dava {@link PackageNamer} fixed names. + * + * @return + */ public String getJavaPackageName() { if (PackageNamer.v().has_FixedNames()) { if (fixedPackageName == null) { diff --git a/src/test/java/soot/BasicTest.java b/src/test/java/soot/BasicTest.java new file mode 100644 index 00000000000..f467efc3726 --- /dev/null +++ b/src/test/java/soot/BasicTest.java @@ -0,0 +1,66 @@ +/*- + * #%L + * Soot - a J*va Optimization Framework + * %% + * Copyright (C) 1997 - 2014 Raja Vallee-Rai and others + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ +package soot; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class BasicTest { + + @Test + public void sootClassNamePackageTests() { + G.reset(); + + Scene.v().loadNecessaryClasses(); + + SootClass sc1 = new SootClass("Class1"); + SootClass sc2 = new SootClass("package1.Class2"); + SootClass sc3 = new SootClass("package2.Class3"); + + Scene.v().addClass(sc1); + Scene.v().addClass(sc2); + Scene.v().addClass(sc3); + + assertEquals("Class1", sc1.getName()); + assertEquals("Class1", sc1.getShortName()); + assertEquals("Class1", sc1.getJavaStyleName()); + assertEquals("Class1", sc1.getShortJavaStyleName()); + assertEquals("", sc1.getPackageName()); + assertEquals("", sc1.getJavaPackageName()); + + assertEquals("package1.Class2", sc2.getName()); + assertEquals("Class2", sc2.getShortName()); + assertEquals("package1.Class2", sc2.getJavaStyleName()); + assertEquals("Class2", sc2.getShortJavaStyleName()); + assertEquals("package1", sc2.getPackageName()); + assertEquals("package1", sc2.getJavaPackageName()); + + assertEquals("package2.Class3", sc3.getName()); + assertEquals("Class3", sc3.getShortName()); + assertEquals("package2.Class3", sc3.getJavaStyleName()); + assertEquals("Class3", sc3.getShortJavaStyleName()); + assertEquals("package2", sc3.getPackageName()); + assertEquals("package2", sc3.getJavaPackageName()); + } + +}