Java Tutorial/Reflection/Name

Материал из Java эксперт
Перейти к: навигация, поиск

Get full class name

public class Main {
  public static String getFullClassName(Class c) {
    return c.getName();
  }

  public static void main(String[] args) {
    System.out.println(getFullClassName(java.awt.Frame.class));
  }
}





Get the class name in a static method

public class Main{
  public static void main(java.lang.String[] args) {
    System.out.println("in " + new CurrentClassGetter().getClassName() + " class");
  }
}
 class CurrentClassGetter extends SecurityManager {
  public String getClassName() {
    return getClassContext()[1].getName();
  }
}





Get the fully-qualified name of a class

public class Main {
  public static void main(String[] argv) throws Exception {
    Class cls = java.lang.String.class;
    String name = cls.getName(); 
    System.out.println(name);
  }
}





Get the fully-qualified name of a inner class

public class Main {
  public static void main(String[] argv) throws Exception {
    Class cls = java.util.Map.Entry.class;
    String name = cls.getName(); 
    System.out.println(name);
  }
}





Get the name of a class

import java.util.Calendar;
import java.math.BigDecimal;
public class Main {
  public static void main(String[] args) {
    // Get the name of the classes below.
    Class clazz = String.class;
    System.out.println("Class Name: " + clazz.getName());
    clazz = Calendar.class;
    System.out.println("Class Name: " + clazz.getName());
    clazz = BigDecimal.class;
    System.out.println("Class Name: " + clazz.getName());
  }
}





Get the name of an array

public class Main {
  public static void main(String[] argv) throws Exception {
    String name = boolean[].class.getName();
    System.out.println(name);
    name = byte[].class.getName();
    System.out.println(name);
    name = char[].class.getName();
    System.out.println(name);
    name = short[].class.getName();
    System.out.println(name);
    name = int[].class.getName();
    System.out.println(name);
    name = long[].class.getName();
    System.out.println(name);
    name = float[].class.getName();
    System.out.println(name);
    name = double[].class.getName();
    System.out.println(name);
    name = String[].class.getName();
    System.out.println(name);
    name = int[][].class.getName();
    System.out.println(name);
  }
}





Get the name of void

public class Main {
  public static void main(String[] argv) throws Exception {
    Class cls = Void.TYPE;
    String name = cls.getName(); // void
  }
}





Get the unqualified name of a class

public class Main {
  public static void main(String[] argv) throws Exception {
    Class cls = java.util.Map.Entry.class;
    String name = cls.getName();
    if (name.lastIndexOf(".") > 0) {
      name = name.substring(name.lastIndexOf(".") + 1); // Map$Entry
      name = name.replace("$", ".");      // Map.Entry
    }
  }
}





Getting the Name of a Member Object

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Main {
  public static void main(String[] argv) throws Exception {
    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;
    name = cls.getName();
    System.out.println(name);
    name = cls.getName() + "." + field.getName();
    System.out.println(name);
    name = constructor.getName();
    System.out.println(name);
    name = cls.getName() + "." + method.getName();
    System.out.println(name);
  }
}
/*
java.lang.String
java.lang.String.CASE_INSENSITIVE_ORDER
java.lang.String
java.lang.String.hashCode
*/





Unqualified names

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Main {
  public static void main(String[] argv) throws Exception {
    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;
    name = cls.getName().substring(cls.getPackage().getName().length()+1);  
    System.out.println(name);
    name = field.getName();  
    System.out.println(name);
    name = constructor.getName().substring(cls.getPackage().getName().length()+1);
    System.out.println(name);
    name = method.getName(); 
    System.out.println(name);
  }
}
/*String
CASE_INSENSITIVE_ORDER
String
hashCode
*/





Use Class.forName to load a class

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Main{
  public static void main(String args[]) throws Exception {
    Class c = Class.forName("MyClass");
  }
}
class MyClass {
  private int count;
  MyClass(int c) {
    count = c;
  }
  MyClass() {
    count = 0;
  }
  void setCount(int c) {
    count = c;
  }
  int getCount() {
    return count;
  }
  void showcount() {
    System.out.println("count is " + count);
  }
}