최근에 일부 Reflection을 코딩하기 시작했으며 클래스를 가져올 때이 문제가 발생합니다. 기본적으로 제목은 다음과 같이 말합니다. My IDE (IntelliJ IDEA)는 내가 작성하는 클래스를 말합니다 : Class myClass = aMethod.invoke (anObject); 은 (분명히) 클래스에 캐스트되어야합니다. 내가 그것을하고 시작하자마자, 메소드를 통해 얻은 실제 클래스가 java.lang.Class로 형변환 될 수 없다는 ClassCastException을 던졌다. 편집 : 나는 Minecraft 서버 플러그인을 만들기 위해 Bukkit을 코딩 중이며 NMS (Net Minecraft Server)에 액세스하려고 시도하고 있는데, 이는 버전 기반이지만 더 이상 반영을 통해 얻을 수있는 내부 API가 아닙니다. 당신은 그것이 하나의 버전을 위해 독특하게되기를 원합니다).Java Reflections. IDE에서는 java.lang.Class로 캐스트해야한다고하지만 일단 ClassCastException을 throw합니다.
static void setName(Player player, String name) {
try {
Method getHandle = player.getClass().getMethod("getHandle");
Object entityPlayer = getHandle.invoke(player);
Field bS = entityPlayer.getClass().getDeclaredField("bS");
Object gameProfile = bS.get(entityPlayer);
Field nameField = gameProfile.getClass().getDeclaredField("name");
nameField.set(gameProfile, name);
} catch (NoSuchMethodException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not find method: ", e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not find class: ", e);
} catch (IllegalAccessException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not access reflection: ", e);
} catch (InvocationTargetException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not invoke method: ", e);
} catch (NoSuchFieldException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not find field: ", e);
}
}
당신은 내가 직접 지정한로, 플레이어 오브젝트에 getHandle 메소드를 호출 볼 수는 버전을 기반으로하지 않습니다, 방법 : 여기에 좀 더 코드 실제로는 CraftPlayer라는 클래스에 속하지만 nms의 일부는 아니지만 버전 기반이기도합니다. 또한 코드를 통해 무의미한 시도를 볼 수도 있습니다.
Caused by: java.lang.ClassCastException: net.minecraft.server.v1_11_R1.EntityPlayer cannot be cast to java.lang.Cl
편집 : 오류는 기본적으로 이것이다 나는 주위에 오늘 좀 더 테스트를 완료하고 코드의 많은 부분을 변경했습니다,이 시간 오류 내에서는 setName 메소드가 호출 될 때마다 NoSuchFieldException가 잡힌 것입니다 필드 "bS", 나는 그것이 존재 하는지를 완전히 확신한다. 기본적으로이 필드는 실제로 EntityHuman이라는 다른 클래스에 있지만 EntityPlayer 클래스는이를 확장합니다. EntityHuman에 캐스팅 될 entityPlayer 객체를 어떻게 얻을 수 있습니까? 첫째,이 클래스 (EntityHuman)는 NMS의 일부이기도하므로 버전 기반이므로 액세스를 시도하기 위해 반사를 사용합니다. Class.forName 메서드를 통해 액세스 할 수 있지만 그 무엇을 제대로 수행할지 알 수 없습니다.
static void setName(Player player, String name) {
try {
Method getHandle = player.getClass().getMethod("getHandle");
Object entityPlayer = getHandle.invoke(player);
Field bS = entityPlayer.getClass().getDeclaredField("bS");
Object gameProfile = bS.get(entityPlayer);
Field nameField = gameProfile.getClass().getDeclaredField("name");
nameField.set(gameProfile, name);
} catch (NoSuchMethodException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not find method: ", e);
} catch (IllegalAccessException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not access reflection: ", e);
} catch (InvocationTargetException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not invoke method: ", e);
} catch (NoSuchFieldException e) {
e.printStackTrace();
Main.getInstance().getLogger().log(Level.SEVERE, "Could not find field: ", e);
}
}
글쎄, 당신이 호출하는 메소드가'Class' 객체를 반환하겠습니까? 질문을 편집 할 수 있고 호출중인 실제 클래스, 인스턴스 및 메서드를 비롯한 일부 코드를 제공 할 수 있습니까? – RealSkeptic
나는 그것을했다. 아마 당신은 통보받지 못했을 것이다. – Vert3x
오류 메시지의 의역을 게시하지 마십시오. 전체 메시지를 게시하십시오. – EJP