私がやろうとしているのは、現在のクラス名を取得することだけです。javaは、クラス名の最後に無意味な意味のない$1を追加します。どうすればそれを取り除き、実際のクラス名のみを返すことができますか?
String className = this.getClass().getName();
試す、
String className = this.getClass().getSimpleName();
これは、静的メソッドで使用しない限り機能します。
「$1」は「役に立たないナンセンス」ではありません。クラスが匿名の場合、番号が追加されます。
クラス自体は必要ないが、その宣言クラスが必要な場合は、を使用できますgetEnclosingClass()
。例えば:
Class<?> enclosingClass = getClass().getEnclosingClass();
if (enclosingClass != null) {
System.out.println(enclosingClass.getName());
} else {
System.out.println(getClass().getName());
}
これは、静的ユーティリティメソッドで移動できます。
ただし、これは現在のクラス名ではないことに注意してください。匿名クラスは、それを囲むクラスとは異なるクラスです。内部クラスの場合も同様です。
これ
this.getClass().getCanonicalName()
またはを使用してみてくださいthis.getClass().getSimpleName()
。匿名クラスの場合は、this.getClass().getSuperclass().getName()
this.getClass().getSimpleName()
次のように使用できます。
import java.lang.reflect.Field;
public class Test {
int x;
int y;
public String getClassName() {
String className = this.getClass().getSimpleName();
System.out.println("Name:" + className);
return className;
}
public Field[] getAttributes() {
Field[] attributes = this.getClass().getDeclaredFields();
for(int i = 0; i < attributes.length; i++) {
System.out.println("Declared Fields" + attributes[i]);
}
return attributes;
}
public static void main(String args[]) {
Test t = new Test();
t.getClassName();
t.getAttributes();
}
}
両方の答えの組み合わせ。メソッド名も出力します。
Class thisClass = new Object(){}.getClass();
String className = thisClass.getEnclosingClass().getSimpleName();
String methodName = thisClass.getEnclosingMethod().getName();
Log.d("app", className + ":" + methodName);
この答えは遅いですが、匿名ハンドラークラスのコンテキストでこれを行う別の方法があると思います。
まあ言ってみれば:
class A {
void foo() {
obj.addHandler(new Handler() {
void bar() {
String className=A.this.getClass().getName();
// ...
}
});
}
}
同じ結果が得られます。さらに、すべてのクラスがコンパイル時に定義されるため、実際には非常に便利であり、動的性が損なわれることはありません。
その上で、クラスが実際にネストされている場合、つまりA
実際にで囲まれているB
場合、Bのクラスは次のように簡単に知ることができます。
B.this.getClass().getName()
あなたの例では、this
おそらく匿名のクラスインスタンスを参照しています。Javaは$number
、囲んでいるクラスの名前にaを追加することにより、これらのクラスに名前を付けます。
これはAndroidのバリアントですが、同じ原則をプレーンJavaでも使用できます。
private static final String TAG = YourClass.class.getSimpleName();
private static final String TAG = YourClass.class.getName();
これは匿名のクラスで起こっていると思います。匿名クラスを作成すると、実際には、取得した名前のクラスを拡張するクラスが作成されます。
必要な名前を取得するための「よりクリーンな」方法は次のとおりです。
クラスが匿名の内部クラスであるgetSuperClass()
場合は、それが作成されたクラスを提供する必要があります。あなたができる最善のことはgetInterfaces()
あなたに複数のインターフェースを与えるかもしれないので、あなたが一種のSOLよりもインターフェースからそれを作成した場合。
「ハッキー」な方法は、で名前を取得しgetClassName()
、正規表現を使用してを削除すること$1
です。
私の場合、次のJavaクラスを使用します。
private String getCurrentProcessName() {
String processName = "";
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == pid) {
processName = processInfo.processName;
break;
}
}
return processName;
}
これは私のコードでは機能することがわかりましたが、私のコードはforループ内の配列からクラスを取得しています。
String className="";
className = list[i].getClass().getCanonicalName();
System.out.print(className); //Use this to test it works
クラスを返すReflectionAPIはいくつかありますが、これらにアクセスできるのは、クラスがすでに直接または間接的に取得されている場合のみです。
Class.getSuperclass() Returns the super class for the given class. Class c = javax.swing.JButton.class.getSuperclass(); The super class of javax.swing.JButton is javax.swing.AbstractButton. Class.getClasses()
継承されたメンバーを含む、クラスのメンバーであるすべてのパブリッククラス、インターフェイス、および列挙型を返します。
Class<?>[] c = Character.class.getClasses();
Characterには、Character.SubsetとCharacter.UnicodeBlockの2つのメンバークラスが含まれています
。Class.getDeclaredClasses() Returns all of the classes interfaces, and enums that are explicitly declared in this class. Class<?>[] c = Character.class.getDeclaredClasses(); Character contains two public member classes Character.Subset and Character.UnicodeBlock and one private class
Character.CharacterCache。
Class.getDeclaringClass() java.lang.reflect.Field.getDeclaringClass() java.lang.reflect.Method.getDeclaringClass() java.lang.reflect.Constructor.getDeclaringClass() Returns the Class in which these members were declared. Anonymous Class Declarations will not have a declaring class but will
囲んでいるクラスがあります。
import java.lang.reflect.Field; Field f = System.class.getField("out"); Class c = f.getDeclaringClass(); The field out is declared in System. public class MyClass { static Object o = new Object() { public void m() {} }; static Class<c> = o.getClass().getEnclosingClass(); } The declaring class of the anonymous class defined by o is null. Class.getEnclosingClass() Returns the immediately enclosing class of the class. Class c = Thread.State.class().getEnclosingClass(); The enclosing class of the enum Thread.State is Thread. public class MyClass { static Object o = new Object() { public void m() {} }; static Class<c> = o.getClass().getEnclosingClass(); } The anonymous class defined by o is enclosed by MyClass.