Package java.lang.reflect

Examples of java.lang.reflect.Field


                String mname = "set" + buff.toString();
                String altname = mname + "Text";
               
                // first try to find a match for alternate method name, with text value and possibly unmarshaller
                Method method = null;
                Field field = null;
                String tname = null;
                Class clas = obj.getClass();
                outer: while (!clas.getName().equals("java.lang.Object")) {
                    Method[] methods = clas.getDeclaredMethods();
                    for (int i = 0; i < methods.length; i++) {
                        Method match = methods[i];
                        if (altname.equals(match.getName())) {
                            Class[] types = match.getParameterTypes();
                            if (types.length == 1 || (types.length == 2 && types[1] == IUnmarshallingContext.class)) {
                                if (types[0] == String.class) {
                                    tname = String.class.getName();
                                    try {
                                        match.setAccessible(true);
                                    } catch (SecurityException e) { /* deliberately empty */
                                    }
                                    method = match;
                                    break outer;
                                }
                            }
                        }
                    }
                    clas = clas.getSuperclass();
                }
                if (method == null) {
                   
                    // no match on alternate method name, next check for regular method
                    clas = obj.getClass();
                    outer: while (!clas.getName().equals("java.lang.Object")) {
                        Method[] methods = clas.getDeclaredMethods();
                        for (int i = 0; i < methods.length; i++) {
                            Method match = methods[i];
                            if (mname.equals(match.getName())) {
                                Class[] types = match.getParameterTypes();
                                if (types.length == 1) {
                                    tname = types[0].getName();
                                    try {
                                        match.setAccessible(true);
                                    } catch (SecurityException e) { /* deliberately empty */
                                    }
                                    method = match;
                                    break outer;
                                }
                            }
                        }
                        clas = clas.getSuperclass();
                    }
                    if (method == null) {
                       
                        // still no match, finally try to find as a field
                        clas = obj.getClass();
                        while (!clas.getName().equals("java.lang.Object")) {
                            try {
                                field = clas.getDeclaredField(fname);
                                try {
                                    field.setAccessible(true);
                                } catch (SecurityException e) { /* deliberately empty */
                                }
                                tname = field.getType().getName();
                                break;
                            } catch (NoSuchFieldException e) {
                                clas = clas.getSuperclass();
                            }
                        }
                        if (field == null) {
                            throw new IllegalArgumentException("Parameter " + key + " not found");
                        }
                    }
                }
                if ("boolean".equals(tname) || "java.lang.Boolean".equals(tname)) {
                   
                    // convert text to boolean value
                    if ("true".equals(value) || "1".equals(value)) {
                        value = Boolean.TRUE;
                    } else if ("false".equals(value) || "0".equals(value)) {
                        value = Boolean.FALSE;
                    } else {
                        throw new IllegalArgumentException("Unknown value '" + value
                            + "' for boolean parameter " + key);
                    }
                   
                } else if ("[Ljava.lang.String;".equals(tname)) {
                   
                    // deserialize token list to string array
                    try {
                        value = org.jibx.runtime.Utility.deserializeTokenList((String)value);
                    } catch (JiBXException e) {
                        throw new IllegalArgumentException("Error processing list value + '" + value +
                            "': " + e.getMessage());
                    }
                   
                } else if ("int".equals(tname) || "java.lang.Integer".equals(tname)) {
                   
                    // convert text to int value
                    try {
                        value = new Integer(Utility.parseInt((String)value));
                    } catch (JiBXException e) {
                        throw new IllegalArgumentException("Error processing int value + '" + value +
                            "': " + e.getMessage());
                    }
                   
                } else if ("long".equals(tname) || "java.lang.Long".equals(tname)) {
                   
                    // convert text to long value
                    try {
                        value = new Long(Utility.parseLong((String)value));
                    } catch (JiBXException e) {
                        throw new IllegalArgumentException("Error processing long value + '" + value +
                            "': " + e.getMessage());
                    }
                   
                   
                } else if ("float".equals(tname) || "java.lang.Float".equals(tname)) {
                   
                    // convert text to float value
                    try {
                        value = new Float(Utility.parseFloat((String)value));
                    } catch (JiBXException e) {
                        throw new IllegalArgumentException("Error processing float value + '" + value +
                            "': " + e.getMessage());
                    }
                   
                } else if ("double".equals(tname) || "java.lang.Double".equals(tname)) {
                   
                    // convert text to float value
                    try {
                        value = new Double(Utility.parseDouble((String)value));
                    } catch (JiBXException e) {
                        throw new IllegalArgumentException("Error processing double value + '" + value +
                            "': " + e.getMessage());
                    }
                   
                } else if (!"java.lang.String".equals(tname)) {
                    throw new IllegalArgumentException("Cannot handle value of type " + tname);
                }
                if (method != null) {
                   
                    // set value using method
                    if (method.getParameterTypes().length == 1) {
                        method.invoke(obj, new Object[] { value });
                    } else {
                        method.invoke(obj, new Object[] { value, null });
                    }
                   
                } else {
                   
                    // set value using field
                    field.set(obj, value);
                   
                }
                fail = false;
               
            } catch (IllegalAccessException e) {
View Full Code Here


        in.close();
    }

    static VarPattern[] lookup(String name) {
        try {
            Field f = PatternCompiler.class.getDeclaredField(name);
            return (VarPattern[]) f.get(null);
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
View Full Code Here

    }

  static String[] constantsFromClass(Class cl, int cnt) {
    String[] names;
    names = new String[cnt];
    Field f[] = cl.getDeclaredFields();
    for (int i = 0; i < f.length; i++)
        try {
            names[f[i].getInt(null)] = f[i].getName();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
View Full Code Here

        }
    return names;
  }
   
    public static int errorCode(String s) throws NoSuchFieldException {
        Field f;
        try {
            f = ViolationCodes.class.getDeclaredField(s);
            return f.getInt(null);
        } catch (SecurityException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
View Full Code Here

         return statusMessages;
      }
     
      protected List<StatusMessage> getRegisteredMessages()
      {
         Field field = Reflections.getField(statusMessages.getClass(), "messages");
         if (!field.isAccessible())
         {
            field.setAccessible(true);
         }
         return (List<StatusMessage>) Reflections.getAndWrap(field, statusMessages);
      }
View Full Code Here

    if (!natives_loaded) {
      return;
    }

    try {
      Field field = ClassLoader.class.getDeclaredField("loadedLibraryNames");
      field.setAccessible(true);
      Vector libs = (Vector) field.get(getClass().getClassLoader());

      String path = new File(nativePath).getCanonicalPath();

      for (int i = 0; i < libs.size(); i++) {
        String s = (String) libs.get(i);
View Full Code Here

        if (validator != null)
        {
            String field = fieldName.substring(fieldName.lastIndexOf(".") + 1);
            String capitalizedField = StringUtils.capitalize(field);
            Object formBackingObject = clazz.newInstance();
            Field f = clazz.getDeclaredField(field);
            Object value = this.getValueObject(f.getType(), fieldValue);
            f.setAccessible(true);
            f.set(formBackingObject, value);
            Errors errors = validateObject(formBackingObject);
            FieldError error = errors.getFieldError(field);
            if (error != null) message = error.getCode();
        }
        return message;
View Full Code Here

      @Override
      public boolean apply(Object source, String name, Object value) {
        if(value == null)
          return true;
        try {
          Field field = source.getClass().getDeclaredField(name);
          if(field != null && field.isAnnotationPresent(JsonExclude.class))
            return true;
          if(ignoreLazyField(field))
            return true;
        } catch (Exception e) {
        }
View Full Code Here

    List<FieldType> fieldTypes = new ArrayList<FieldType>();
    for (DatabaseFieldConfig fieldConfig : fieldConfigs) {
      FieldType fieldType = null;
      // walk up the classes until we find the field
      for (Class<?> classWalk = dataClass; classWalk != null; classWalk = classWalk.getSuperclass()) {
        Field field;
        try {
          field = classWalk.getDeclaredField(fieldConfig.getFieldName());
        } catch (NoSuchFieldException e) {
          // we ignore this and just loop hopefully finding it in a upper class
          continue;
View Full Code Here

        try {
            boolean noerror = true;
            Class ec = Class.forName("org.chaidb.db.exception.ErrorCode");
            Field[] eclist = ec.getFields();
            for (int i = 0; i < eclist.length; i++) {
                Field field = eclist[i];
                Integer f = (Integer) field.get(null);
                String msg = ErrorCode.getMessage(f.intValue());
                if (msg == null || msg.length() == 0) {
                    System.out.println("No message for:" + f.intValue());
                    if (noerror) {
                        noerror = false;
View Full Code Here

TOP

Related Classes of java.lang.reflect.Field

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.