Exemple #1
0
 public Bean(String classPackage, String clazz, ClassLoaderStrategy cls, Bean topLevelBean)
     throws Exception {
   // Get the no-arg constructor and create the bean
   try {
     Class classOfBean = ObjectXml.getClassOfBean((ClassLoader) cls, classPackage + "." + clazz);
     Constructor ct = null;
     // check whether this class is an inner class
     if (classOfBean.getEnclosingClass() != null) {
       ct = classOfBean.getConstructor(new Class[] {classOfBean.getEnclosingClass()});
       beanObject = ct.newInstance(new Object[] {topLevelBean.getBeanObject()});
     } else {
       ct = classOfBean.getConstructor((Class[]) null);
       beanObject = ct.newInstance((Object[]) null);
     }
     // Get an array of property descriptors
     beanInfo = Introspector.getBeanInfo(classOfBean);
     PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
     // load property descriptors into hashtable
     propDesc = new Properties();
     for (int i = 0; i < pds.length; i++) {
       propDesc.put(pds[i].getName(), pds[i]);
     }
   } catch (Exception e) {
     System.err.println("Exception creating bean: " + e.getMessage());
     e.printStackTrace();
     throw e;
   }
 }
  private void validateClass(Class<?> source, ValidationProblemCollector problems) {
    int modifiers = source.getModifiers();

    if (Modifier.isInterface(modifiers)) {
      problems.add("Must be a class, not an interface");
    }

    if (source.getEnclosingClass() != null) {
      if (Modifier.isStatic(modifiers)) {
        if (Modifier.isPrivate(modifiers)) {
          problems.add("Class cannot be private");
        }
      } else {
        problems.add("Enclosed classes must be static and non private");
      }
    }

    Constructor<?>[] constructors = source.getDeclaredConstructors();
    for (Constructor<?> constructor : constructors) {
      if (constructor.getParameterTypes().length > 0) {
        problems.add("Cannot declare a constructor that takes arguments");
        break;
      }
    }

    Field[] fields = source.getDeclaredFields();
    for (Field field : fields) {
      int fieldModifiers = field.getModifiers();
      if (!field.isSynthetic()
          && !(Modifier.isStatic(fieldModifiers) && Modifier.isFinal(fieldModifiers))) {
        problems.add(field, "Fields must be static final.");
      }
    }
  }
Exemple #3
0
  public JavaType findType(String name) {
    if (_bindings == null) {
      _resolve();
    }
    JavaType t = _bindings.get(name);
    if (t != null) {
      return t;
    }
    if (_placeholders != null && _placeholders.contains(name)) {
      return UNBOUND;
    }
    // New with 1.7: check parent context
    if (_parentBindings != null) {
      return _parentBindings.findType(name);
    }
    // nothing found, so...
    // Should we throw an exception or just return null?

    /* [JACKSON-499] 18-Feb-2011, tatu: There are some tricky type bindings within
     *   java.util, such as HashMap$KeySet; so let's punt the problem
     *   (honestly not sure what to do -- they are unbound for good, I think)
     */
    if (_contextClass != null) {
      Class<?> enclosing = _contextClass.getEnclosingClass();
      if (enclosing != null) {
        // [JACKSON-572]: Actually, let's skip this for all non-static inner classes
        //   (which will also cover 'java.util' type cases...
        if (!Modifier.isStatic(_contextClass.getModifiers())) {
          return UNBOUND;
        }

        // ... so this piece of code should not be needed any more
        /*
        Package pkg = enclosing.getPackage();
        if (pkg != null) {
            // as per [JACKSON-533], also include "java.util.concurrent":
            if (pkg.getName().startsWith("java.util")) {
                return UNBOUND;
            }
        }
        */
      }
    }

    String className;
    if (_contextClass != null) {
      className = _contextClass.getName();
    } else if (_contextType != null) {
      className = _contextType.toString();
    } else {
      className = "UNKNOWN";
    }
    throw new IllegalArgumentException(
        "Type variable '"
            + name
            + "' can not be resolved (with context of class "
            + className
            + ")");
    // t = UNBOUND;
  }
  public static String isLocalType(Class<?> type) {
    /* As per [JACKSON-187], GAE seems to throw SecurityExceptions
     * here and there... and GAE itself has a bug, too
     * (see []). Bah.
     */
    try {
      // one more: method locals, anonymous, are not good:
      if (type.getEnclosingMethod() != null) {
        return "local/anonymous";
      }

      /* But how about non-static inner classes? Can't construct
       * easily (theoretically, we could try to check if parent
       * happens to be enclosing... but that gets convoluted)
       */
      if (type.getEnclosingClass() != null) {
        if (!Modifier.isStatic(type.getModifiers())) {
          return "non-static member class";
        }
      }
    } catch (SecurityException e) {
    } catch (NullPointerException e) {
    }
    return null;
  }