コード例 #1
0
    AtomicReferenceFieldUpdaterImpl(
        Class<T> tclass, Class<V> vclass, String fieldName, Class<?> caller) {
      Field field = null;
      Class fieldClass = null;
      int modifiers = 0;
      try {
        field = tclass.getDeclaredField(fieldName);
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(caller, tclass, null, modifiers);
        sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        fieldClass = field.getType();
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }

      if (vclass != fieldClass) throw new ClassCastException();
      if (vclass.isPrimitive()) throw new IllegalArgumentException("Must be reference type");

      if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

      this.cclass = (Modifier.isProtected(modifiers) && caller != tclass) ? caller : null;
      this.tclass = tclass;
      if (vclass == Object.class) this.vclass = null;
      else this.vclass = vclass;
      offset = unsafe.objectFieldOffset(field);
    }
 public void beanClass(Class<?> beanClass) {
   if ((beanClass == null) ? this.beanClass != null : !beanClass.equals(this.beanClass)) {
     ReflectUtil.checkPackageAccess(beanClass);
     this.beanClass = beanClass;
     this.descriptor = null;
   }
 }
コード例 #3
0
  /**
   * The value of <code>get(uidClassID)</code> must be the <code>String</code> name of a class that
   * implements the corresponding <code>ComponentUI</code> class. If the class hasn't been loaded
   * before, this method looks up the class with <code>uiClassLoader.loadClass()</code> if a non
   * <code>null</code> class loader is provided, <code>classForName()</code> otherwise.
   *
   * <p>If a mapping for <code>uiClassID</code> exists or if the specified class can't be found,
   * return <code>null</code>.
   *
   * <p>This method is used by <code>getUI</code>, it's usually not necessary to call it directly.
   *
   * @param uiClassID a string containing the class ID
   * @param uiClassLoader the object which will load the class
   * @return the value of <code>Class.forName(get(uidClassID))</code>
   * @see #getUI
   */
  public Class<? extends ComponentUI> getUIClass(String uiClassID, ClassLoader uiClassLoader) {
    try {
      String className = (String) get(uiClassID);
      if (className != null) {
        ReflectUtil.checkPackageAccess(className);

        Class cls = (Class) get(className);
        if (cls == null) {
          if (uiClassLoader == null) {
            cls = SwingUtilities.loadSystemClass(className);
          } else {
            cls = uiClassLoader.loadClass(className);
          }
          if (cls != null) {
            // Save lookup for future use, as forName is slow.
            put(className, cls);
          }
        }
        return cls;
      }
    } catch (ClassNotFoundException e) {
      return null;
    } catch (ClassCastException e) {
      return null;
    }
    return null;
  }
コード例 #4
0
  /*
   * Parse a field value from the XML produced by toXMLString().
   * Given a descriptor XML containing <field name="nnn" value="vvv">,
   * the argument to this method will be "vvv" (a string including the
   * containing quote characters).  If vvv begins and ends with parentheses,
   * then it may contain:
   * - the characters "null", in which case the result is null;
   * - a value of the form "some.class.name/xxx", in which case the
   * result is equivalent to `new some.class.name("xxx")';
   * - some other string, in which case the result is that string,
   * without the parentheses.
   */
  private static Object parseQuotedFieldValue(String s) throws XMLParseException {
    s = unquote(s);
    if (s.equalsIgnoreCase("(null)")) return null;
    if (!s.startsWith("(") || !s.endsWith(")")) return s;
    final int slash = s.indexOf('/');
    if (slash < 0) {
      // compatibility: old code didn't include class name
      return s.substring(1, s.length() - 1);
    }
    final String className = s.substring(1, slash);

    final Constructor<?> constr;
    try {
      ReflectUtil.checkPackageAccess(className);
      final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
      final Class<?> c = Class.forName(className, false, contextClassLoader);
      constr = c.getConstructor(new Class<?>[] {String.class});
    } catch (Exception e) {
      throw new XMLParseException(e, "Cannot parse value: <" + s + ">");
    }
    final String arg = s.substring(slash + 1, s.length() - 1);
    try {
      return constr.newInstance(new Object[] {arg});
    } catch (Exception e) {
      final String msg = "Cannot construct instance of " + className + " with arg: <" + s + ">";
      throw new XMLParseException(e, msg);
    }
  }
コード例 #5
0
  /**
   * Constructs an instance of a property editor using the current property editor class.
   *
   * <p>If the property editor class has a public constructor that takes an Object argument then it
   * will be invoked using the bean parameter as the argument. Otherwise, the default constructor
   * will be invoked.
   *
   * @param bean the source object
   * @return a property editor instance or null if a property editor has not been defined or cannot
   *     be created
   * @since 1.5
   */
  public PropertyEditor createPropertyEditor(Object bean) {
    Object editor = null;

    final Class<?> cls = getPropertyEditorClass();
    if (cls != null
        && PropertyEditor.class.isAssignableFrom(cls)
        && ReflectUtil.isPackageAccessible(cls)) {
      Constructor<?> ctor = null;
      if (bean != null) {
        try {
          ctor = cls.getConstructor(new Class<?>[] {Object.class});
        } catch (Exception ex) {
          // Fall through
        }
      }
      try {
        if (ctor == null) {
          editor = cls.newInstance();
        } else {
          editor = ctor.newInstance(new Object[] {bean});
        }
      } catch (Exception ex) {
        // Fall through
      }
    }
    return (PropertyEditor) editor;
  }
コード例 #6
0
ファイル: AquaUtils.java プロジェクト: susotajuraj/jdk8u-jdk
 @Override
 T getInstance() {
   try {
     ReflectUtil.checkPackageAccess(clazz);
     return clazz.newInstance();
   } catch (InstantiationException | IllegalAccessException ignored) {
   }
   return null;
 }
 public void bean(Object bean) {
   this.bean = bean;
   if (bean != null) {
     Class<?> newClass = bean.getClass();
     if ((beanClass == null) || !beanClass.isAssignableFrom(newClass)) {
       ReflectUtil.checkPackageAccess(newClass);
       this.beanClass = bean.getClass();
       this.descriptor = null;
     }
   }
 }
コード例 #8
0
    LockedUpdater(Class<T> tclass, String fieldName, Class<?> caller) {
      Field field = null;
      int modifiers = 0;
      try {
        field = tclass.getDeclaredField(fieldName);
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(caller, tclass, null, modifiers);
        sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }

      Class fieldt = field.getType();
      if (fieldt != long.class) throw new IllegalArgumentException("Must be long type");

      if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

      this.cclass = (Modifier.isProtected(modifiers) && caller != tclass) ? caller : null;
      this.tclass = tclass;
      offset = unsafe.objectFieldOffset(field);
    }
コード例 #9
0
  private static <T> T convertFromString(String s, OpenType<T> openType) {
    Class<T> c;
    try {
      ReflectUtil.checkPackageAccess(openType.safeGetClassName());
      c = cast(Class.forName(openType.safeGetClassName()));
    } catch (ClassNotFoundException e) {
      throw new NoClassDefFoundError(e.toString()); // can't happen
    }

    // Look for: public static T valueOf(String)
    Method valueOf;
    try {
      // It is safe to call this plain Class.getMethod because the class "c"
      // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName());
      valueOf = c.getMethod("valueOf", String.class);
      if (!Modifier.isStatic(valueOf.getModifiers()) || valueOf.getReturnType() != c)
        valueOf = null;
    } catch (NoSuchMethodException e) {
      valueOf = null;
    }
    if (valueOf != null) {
      try {
        return c.cast(MethodUtil.invoke(valueOf, null, new Object[] {s}));
      } catch (Exception e) {
        final String msg = "Could not convert \"" + s + "\" using method: " + valueOf;
        throw new IllegalArgumentException(msg, e);
      }
    }

    // Look for: public T(String)
    Constructor<T> con;
    try {
      // It is safe to call this plain Class.getConstructor because the class "c"
      // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName());
      con = c.getConstructor(String.class);
    } catch (NoSuchMethodException e) {
      con = null;
    }
    if (con != null) {
      try {
        return con.newInstance(s);
      } catch (Exception e) {
        final String msg = "Could not convert \"" + s + "\" using constructor: " + con;
        throw new IllegalArgumentException(msg, e);
      }
    }

    throw new IllegalArgumentException(
        "Don't know how to convert " + "string to " + openType.getTypeName());
  }
コード例 #10
0
 <T> MBeanSupport(T resource, Class<T> mbeanInterfaceType) throws NotCompliantMBeanException {
   if (mbeanInterfaceType == null) throw new NotCompliantMBeanException("Null MBean interface");
   if (!mbeanInterfaceType.isInstance(resource)) {
     final String msg =
         "Resource class "
             + resource.getClass().getName()
             + " is not an instance of "
             + mbeanInterfaceType.getName();
     throw new NotCompliantMBeanException(msg);
   }
   ReflectUtil.checkPackageAccess(mbeanInterfaceType);
   this.resource = resource;
   MBeanIntrospector<M> introspector = getMBeanIntrospector();
   this.perInterface = introspector.getPerInterface(mbeanInterfaceType);
   this.mbeanInfo = introspector.getMBeanInfo(resource, perInterface);
 }