2015-04-19 66 views
-4

所以我在這段代碼中遇到了一些錯誤,錯誤是關鍵首先是 ,但代碼錯誤在哪裏,它是說Syntax錯誤的令牌「(」類型的預期後,這個令牌,之前我有字符串沒有<>,然後鍵加下劃線,錯誤是不同的...所以不知道該怎麼辦...語法錯誤令牌「(」,類型應該在這個令牌後

這裏是語法錯誤是:

for (<String> key : keys) { 
     Object out = map.get(key); 
     out = saveObject(out, field, cs, path + "." + key, depth); 
     subCS.set(key, out); 

整個文件:

package me.kalo121; 

import java.lang.reflect.Field; 
import java.lang.reflect.Modifier; 
import java.lang.reflect.ParameterizedType; 
import java.lang.reflect.Type; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 

import org.bukkit.Bukkit; 
import org.bukkit.Location; 
import org.bukkit.World; 
import org.bukkit.configuration.ConfigurationSection; 
import org.bukkit.util.Vector; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 

public abstract class ConfigObject 
{ 
    protected void onLoad(ConfigurationSection cs) 
    throws Exception 
    { 
    for (Field field : getClass().getDeclaredFields()) { 
     String path = field.getName().replaceAll("_", "."); 
     if (!doSkip(field)) 
     { 
     if (cs.isSet(path)) 
      field.set(this, loadObject(field, cs, path)); 
     else 
      cs.set(path, saveObject(field.get(this), field, cs, path)); 
     } 
    } 
    } 

    protected void onSave(ConfigurationSection cs) throws Exception { 
    for (Field field : getClass().getDeclaredFields()) { 
     String path = field.getName().replaceAll("_", "."); 
     if (!doSkip(field)) 
     { 
     cs.set(path, saveObject(field.get(this), field, cs, path)); 
     } 
    } 
    } 

    protected Object loadObject(Field field, ConfigurationSection cs, String path) throws Exception { 
    return loadObject(field, cs, path, 0); 
    } 

    protected Object saveObject(Object obj, Field field, ConfigurationSection cs, String path) throws Exception { 
    return saveObject(obj, field, cs, path, 0); 
    } 

    protected Object loadObject(Field field, ConfigurationSection cs, String path, int depth) throws Exception 
    { 
    @SuppressWarnings("rawtypes") 
    Class clazz = getClassAtDepth(field.getGenericType(), depth); 
    if ((ConfigObject.class.isAssignableFrom(clazz)) && (isConfigurationSection(cs.get(path)))) 
     return getConfigObject(clazz, cs.getConfigurationSection(path)); 
    if ((Location.class.isAssignableFrom(clazz)) && (isJSON(cs.get(path)))) 
     return getLocation((String)cs.get(path)); 
    if ((Vector.class.isAssignableFrom(clazz)) && (isJSON(cs.get(path)))) 
     return getVector((String)cs.get(path)); 
    if ((Map.class.isAssignableFrom(clazz)) && (isConfigurationSection(cs.get(path)))) 
     return getMap(field, cs.getConfigurationSection(path), path, depth); 
    if ((clazz.isEnum()) && (isString(cs.get(path)))) 
     return getEnum(clazz, (String)cs.get(path)); 
    if ((List.class.isAssignableFrom(clazz)) && (isConfigurationSection(cs.get(path)))) { 
     @SuppressWarnings("rawtypes") 
    Class subClazz = getClassAtDepth(field.getGenericType(), depth + 1); 
     if ((ConfigObject.class.isAssignableFrom(subClazz)) || (Location.class.isAssignableFrom(subClazz)) || (Vector.class.isAssignableFrom(subClazz)) || 
     (Map.class.isAssignableFrom(subClazz)) || (List.class.isAssignableFrom(subClazz)) || (subClazz.isEnum())) { 
     return getList(field, cs.getConfigurationSection(path), path, depth); 
     } 
     return cs.get(path); 
    } 

    return cs.get(path); 
    } 

    @SuppressWarnings("rawtypes") 
protected Object saveObject(Object obj, Field field, ConfigurationSection cs, String path, int depth) 
    throws Exception 
    { 
    @SuppressWarnings("rawtypes") 
    Class clazz = getClassAtDepth(field.getGenericType(), depth); 
    if ((ConfigObject.class.isAssignableFrom(clazz)) && (isConfigObject(obj))) 
     return getConfigObject((ConfigObject)obj, path, cs); 
    if ((Location.class.isAssignableFrom(clazz)) && (isLocation(obj))) 
     return getLocation((Location)obj); 
    if ((Vector.class.isAssignableFrom(clazz)) && (isVector(obj))) 
     return getVector((Vector)obj); 
    if ((Map.class.isAssignableFrom(clazz)) && (isMap(obj))) 
     return getMap((Map)obj, field, cs, path, depth); 
    if ((clazz.isEnum()) && (isEnum(clazz, obj))) 
     return getEnum((Enum)obj); 
    if ((List.class.isAssignableFrom(clazz)) && (isList(obj))) { 
     Class subClazz = getClassAtDepth(field.getGenericType(), depth + 1); 
     if ((ConfigObject.class.isAssignableFrom(subClazz)) || (Location.class.isAssignableFrom(subClazz)) || (Vector.class.isAssignableFrom(subClazz)) || 
     (Map.class.isAssignableFrom(subClazz)) || (List.class.isAssignableFrom(subClazz)) || (subClazz.isEnum())) { 
     return getList((List)obj, field, cs, path, depth); 
     } 
     return obj; 
    } 

    return obj; 
    } 

    @SuppressWarnings("rawtypes") 
protected Class getClassAtDepth(Type type, int depth) 
    throws Exception 
    { 
    if (depth <= 0) { 
     String className = type.toString(); 
     if ((className.length() >= 6) && (className.substring(0, 6).equalsIgnoreCase("class "))) { 
     className = className.substring(6); 
     } 
     if (className.indexOf("<") >= 0) 
     className = className.substring(0, className.indexOf("<")); 
     try 
     { 
     return Class.forName(className); 
     } 
     catch (ClassNotFoundException ex) { 
     if (className.equalsIgnoreCase("byte")) return Byte.class; 
     if (className.equalsIgnoreCase("short")) return Short.class; 
     if (className.equalsIgnoreCase("int")) return Integer.class; 
     if (className.equalsIgnoreCase("long")) return Long.class; 
     if (className.equalsIgnoreCase("float")) return Float.class; 
     if (className.equalsIgnoreCase("double")) return Double.class; 
     if (className.equalsIgnoreCase("char")) return Character.class; 
     if (className.equalsIgnoreCase("boolean")) return Boolean.class; 
     throw ex; 
     } 
    } 
    depth--; 
    ParameterizedType pType = (ParameterizedType)type; 
    Type[] typeArgs = pType.getActualTypeArguments(); 
    return getClassAtDepth(typeArgs[(typeArgs.length - 1)], depth); 
    } 

    protected boolean isString(Object obj) { 
    if ((obj instanceof String)) { 
     return true; 
    } 
    return false; 
    } 

    protected boolean isConfigurationSection(Object o) { 
    try { 
     return (ConfigurationSection)o != null; } catch (Exception e) { 
    } 
    return false; 
    } 

    protected boolean isJSON(Object obj) 
    { 
    try { 
     if ((obj instanceof String)) { 
     String str = (String)obj; 
     if (str.startsWith("{")) { 
      return new JSONParser().parse(str) != null; 
     } 
     } 
     return false; } catch (Exception e) { 
    } 
    return false; 
    } 

    protected boolean isConfigObject(Object obj) 
    { 
    try { 
     return (ConfigObject)obj != null; } catch (Exception e) { 
    } 
    return false; 
    } 

    protected boolean isLocation(Object obj) 
    { 
    try { 
     return (Location)obj != null; } catch (Exception e) { 
    } 
    return false; 
    } 

    protected boolean isVector(Object obj) 
    { 
    try { 
     return (Vector)obj != null; } catch (Exception e) { 
    } 
    return false; 
    } 

    @SuppressWarnings("rawtypes") 
protected boolean isMap(Object obj) 
    { 
    try 
    { 
     return (Map)obj != null; } catch (Exception e) { 
    } 
    return false; 
    } 

    @SuppressWarnings("rawtypes") 
protected boolean isList(Object obj) 
    { 
    try 
    { 
     return (List)obj != null; } catch (Exception e) { 
    } 
    return false; 
    } 

    protected boolean isEnum(@SuppressWarnings("rawtypes") Class clazz, Object obj) 
    { 
    if (!clazz.isEnum()) return false; 
    for (Object constant : clazz.getEnumConstants()) { 
     if (constant.equals(obj)) { 
     return true; 
     } 
    } 
    return false; 
    } 

    protected ConfigObject getConfigObject(@SuppressWarnings("rawtypes") Class clazz, ConfigurationSection cs) 
    throws Exception 
    { 
    ConfigObject obj = (ConfigObject)clazz.newInstance(); 
    obj.onLoad(cs); 
    return obj; 
    } 

    protected Location getLocation(String json) throws Exception { 
    JSONObject data = (JSONObject)new JSONParser().parse(json); 

    World world = Bukkit.getWorld((String)data.get("world")); 

    double x = Double.parseDouble((String)data.get("x")); 
    double y = Double.parseDouble((String)data.get("y")); 
    double z = Double.parseDouble((String)data.get("z")); 

    float pitch = Float.parseFloat((String)data.get("pitch")); 
    float yaw = Float.parseFloat((String)data.get("yaw")); 

    Location loc = new Location(world, x, y, z); 
    loc.setPitch(pitch); 
    loc.setYaw(yaw); 
    return loc; 
    } 

    protected Vector getVector(String json) throws Exception { 
    JSONObject data = (JSONObject)new JSONParser().parse(json); 

    double x = Double.parseDouble((String)data.get("x")); 
    double y = Double.parseDouble((String)data.get("y")); 
    double z = Double.parseDouble((String)data.get("z")); 

    return new Vector(x, y, z); 
    } 

    @SuppressWarnings({ "rawtypes", "unchecked" }) 
protected Map getMap(Field field, ConfigurationSection cs, String path, int depth) throws Exception 
    { 
    depth++; 
    Set keys = cs.getKeys(false); 
    Map map = new HashMap(); 
    if ((keys != null) && (keys.size() > 0)) { 
     for (<String> key : keys) { 
     Object in = cs.get(key); 
     in = loadObject(field, cs, key, depth); 
     map.put(key, in); 
     } 
    } 
    return map; 
    } 

    protected List<String> getList(Field field, ConfigurationSection cs, String path, int depth) throws Exception 
    { 
    depth++; 
    int listSize = cs.getKeys(false).size(); 
    String key = path; 
    if (key.lastIndexOf(".") >= 0) { 
     key = key.substring(key.lastIndexOf(".")); 
    } 
    List<String> list = new ArrayList<String>(); 
    if (listSize > 0) { 
     int loaded = 0; 
     int i = 0; 
     while (loaded < listSize) { 
     if (cs.isSet(key + i)) { 
      Object in = cs.get(key + i); 
      in = loadObject(field, cs, key + i, depth); 
      list.add((String) in); 
      loaded++; 
     } 
     i++; 

     if (i > listSize * 3) loaded = listSize; 
     } 
    } 
    return list; 
    } 

    @SuppressWarnings("rawtypes") 
protected Enum getEnum(Class clazz, String string) throws Exception 
    { 
    if (!clazz.isEnum()) throw new Exception("Class " + clazz.getName() + " is not an enum."); 
    for (Object constant : clazz.getEnumConstants()) { 
     if (((Enum)constant).toString().equals(string)) { 
     return (Enum)constant; 
     } 
    } 
    throw new Exception("String " + string + " not a valid enum constant for " + clazz.getName()); 
    } 

    protected ConfigurationSection getConfigObject(ConfigObject obj, String path, ConfigurationSection cs) 
    throws Exception 
    { 
    ConfigurationSection subCS = cs.createSection(path); 
    obj.onSave(subCS); 
    return subCS; 
    } 

    protected String getLocation(Location loc) { 
    String ret = "{"; 
    ret = ret + "\"world\":\"" + loc.getWorld().getName() + "\""; 
    ret = ret + ",\"x\":\"" + loc.getX() + "\""; 
    ret = ret + ",\"y\":\"" + loc.getY() + "\""; 
    ret = ret + ",\"z\":\"" + loc.getZ() + "\""; 
    ret = ret + ",\"pitch\":\"" + loc.getPitch() + "\""; 
    ret = ret + ",\"yaw\":\"" + loc.getYaw() + "\""; 
    ret = ret + "}"; 
    if (!isJSON(ret)) return getLocationJSON(loc); try 
    { 
     getLocation(ret); 
    } catch (Exception ex) { 
     return getLocationJSON(loc); 
    } 
    return ret; 
    } 

    @SuppressWarnings("unchecked") 
protected String getLocationJSON(Location loc) 
    { 
    JSONObject data = new JSONObject(); 

    data.put("world", loc.getWorld().getName()); 

    data.put("x", String.valueOf(loc.getX())); 
    data.put("y", String.valueOf(loc.getY())); 
    data.put("z", String.valueOf(loc.getZ())); 

    data.put("pitch", String.valueOf(loc.getPitch())); 
    data.put("yaw", String.valueOf(loc.getYaw())); 
    return data.toJSONString(); 
    } 

    protected String getVector(Vector vec) { 
    String ret = "{"; 
    ret = ret + "\"x\":\"" + vec.getX() + "\""; 
    ret = ret + ",\"y\":\"" + vec.getY() + "\""; 
    ret = ret + ",\"z\":\"" + vec.getZ() + "\""; 
    ret = ret + "}"; 
    if (!isJSON(ret)) return getVectorJSON(vec); try 
    { 
     getVector(ret); 
    } catch (Exception ex) { 
     return getVectorJSON(vec); 
    } 
    return ret; 
    } 

    @SuppressWarnings("unchecked") 
protected String getVectorJSON(Vector vec) 
    { 
    JSONObject data = new JSONObject(); 

    data.put("x", String.valueOf(vec.getX())); 
    data.put("y", String.valueOf(vec.getY())); 
    data.put("z", String.valueOf(vec.getZ())); 
    return data.toJSONString(); 
    } 

    protected ConfigurationSection getMap(@SuppressWarnings("rawtypes") Map map, Field field, ConfigurationSection cs, String path, int depth) throws Exception 
    { 
    depth++; 
    ConfigurationSection subCS = cs.createSection(path); 
    @SuppressWarnings("rawtypes") 
    Set keys = map.keySet(); 
    if ((keys != null) && (keys.size() > 0)) { 
     for (<String> key : keys) { 
     Object out = map.get(key); 
     out = saveObject(out, field, cs, path + "." + key, depth); 
     subCS.set(key, out); 
     } 
    } 
    return subCS; 
    } 

    protected ConfigurationSection getList(@SuppressWarnings("rawtypes") List list, Field field, ConfigurationSection cs, String path, int depth) throws Exception 
    { 
    depth++; 
    ConfigurationSection subCS = cs.createSection(path); 
    String key = path; 
    if (key.lastIndexOf(".") >= 0) { 
     key = key.substring(key.lastIndexOf(".")); 
    } 
    if ((list != null) && (list.size() > 0)) { 
     for (int i = 0; i < list.size(); i++) { 
     Object out = list.get(i); 
     out = saveObject(out, field, cs, path + "." + key + (i + 1), depth); 
     subCS.set(key + (i + 1), out); 
     } 
    } 
    return subCS; 
    } 

    protected String getEnum(@SuppressWarnings("rawtypes") Enum enumObj) 
    { 
    return enumObj.toString(); 
    } 

    protected boolean doSkip(Field field) 
    { 
    return (Modifier.isTransient(field.getModifiers())) || (Modifier.isStatic(field.getModifiers())) || (Modifier.isFinal(field.getModifiers())) || 
     (Modifier.isPrivate(field.getModifiers())); 
    } 
} 
+0

語法錯誤是題外話這裏。使用您的IDE並找到問題。 –

+0

您發佈了一段代碼,很難確定問題所在的位置。 –

+0

簡單的只求求幫助對不起,只是希望1個人會給我所有的東西 – Noongrboy

回答

1

for (<String> key : keys) {是無效的語法。原因for (String key : keys) {也沒有工作或者keys變量的類型是原始Set,這意味着您只能用for (Object key : keys) {迭代它。

如果更改Set keys = cs.getKeys(false);Set<String> keys = cs.getKeys(false);(假設這Set只包含String S),該工作具有如下:

for (String key : keys) { 
+0

非常感謝一位有幫助的人,但是在這個鍵位上也出現了一個錯誤,只是想知道這個修復程序是什麼? – Noongrboy

+0

保護ConfigurationSection getMap(@SuppressWarnings(「rawtypes」)映射圖,字段字段,ConfigurationSection cs,字符串路徑,int深度)拋出異常 { depth ++; ConfigurationSection subCS = cs.createSection(path); (「rawtypes」) \t Set keys = map.keySet(); (key:keys){ Object(from gamasutra)Object out = map.get(key);如果((key!= null)&&(keys.size()> 0) out = saveObject(out,field,cs,path +「。」+ key,depth); subCS.set(key,out); – Noongrboy

+0

我不知道如何使用這個網站一點點大聲笑所以Idk如何做到這一點更容易代碼,但如果你檢查主代碼,並從底部轉到第4個「受保護」標記它有@Eran – Noongrboy

相關問題