Search in sources :

Example 86 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project spring-framework by spring-projects.

the class SimplePropertyDescriptorTests method indexedEquality.

@Test
public void indexedEquality() throws IntrospectionException, SecurityException, NoSuchMethodException {
    Object pd1 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, null);
    assertThat(pd1, equalTo(pd1));
    Object pd2 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, null);
    assertThat(pd1, equalTo(pd2));
    assertThat(pd2, equalTo(pd1));
    @SuppressWarnings("unused")
    class C {

        public Object setFoo(int i, String foo) {
            return null;
        }

        public String getFoo(int i) {
            return null;
        }
    }
    Method wm1 = C.class.getMethod("setFoo", int.class, String.class);
    Object pd3 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, wm1);
    assertThat(pd1, not(equalTo(pd3)));
    assertThat(pd3, not(equalTo(pd1)));
    Method rm1 = C.class.getMethod("getFoo", int.class);
    Object pd4 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, rm1, null);
    assertThat(pd1, not(equalTo(pd4)));
    assertThat(pd4, not(equalTo(pd1)));
    Object pd5 = new IndexedPropertyDescriptor("foo", null, null, null, null);
    assertThat(pd1, equalTo(pd5));
    assertThat(pd5, equalTo(pd1));
    Object pd6 = "not a PD";
    assertThat(pd1, not(equalTo(pd6)));
    assertThat(pd6, not(equalTo(pd1)));
    Object pd7 = null;
    assertThat(pd1, not(equalTo(pd7)));
    assertThat(pd7, not(equalTo(pd1)));
}
Also used : IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 87 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project tomcat by apache.

the class ConnectorStoreAppender method getPropertyKeys.

/**
     * Get all properties from Connector and current ProtocolHandler.
     *
     * @param bean The connector
     * @return List of Connector property names
     * @throws IntrospectionException Error introspecting connector
     */
protected List<String> getPropertyKeys(Connector bean) throws IntrospectionException {
    ArrayList<String> propertyKeys = new ArrayList<>();
    // Acquire the list of properties for this bean
    ProtocolHandler protocolHandler = bean.getProtocolHandler();
    // Acquire the list of properties for this bean
    PropertyDescriptor[] descriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor instanceof IndexedPropertyDescriptor) {
            // Indexed properties are not persisted
            continue;
        }
        if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) || (descriptor.getWriteMethod() == null)) {
            // Must be a read-write primitive or String
            continue;
        }
        if ("protocol".equals(descriptor.getName()) || "protocolHandlerClassName".equals(descriptor.getName()))
            continue;
        propertyKeys.add(descriptor.getName());
    }
    // Add the properties of the protocol handler
    descriptors = Introspector.getBeanInfo(protocolHandler.getClass()).getPropertyDescriptors();
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor instanceof IndexedPropertyDescriptor) {
            // Indexed properties are not persisted
            continue;
        }
        if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) || (descriptor.getWriteMethod() == null)) {
            // Must be a read-write primitive or String
            continue;
        }
        String key = descriptor.getName();
        if (!Connector.INTERNAL_EXECUTOR_NAME.equals(bean.getExecutorName()) && internalExecutorAttributes.contains(key)) {
            continue;
        }
        if (replacements.get(key) != null) {
            key = replacements.get(key);
        }
        if (!propertyKeys.contains(key)) {
            propertyKeys.add(key);
        }
    }
    // Add the properties for the socket
    final String socketName = "socket.";
    descriptors = Introspector.getBeanInfo(SocketProperties.class).getPropertyDescriptors();
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor instanceof IndexedPropertyDescriptor) {
            // Indexed properties are not persisted
            continue;
        }
        if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) || (descriptor.getWriteMethod() == null)) {
            // Must be a read-write primitive or String
            continue;
        }
        String key = descriptor.getName();
        if (replacements.get(key) != null) {
            key = replacements.get(key);
        }
        if (!propertyKeys.contains(key)) {
            // Add socket.[original name] if this is not a property
            // that could be set elsewhere
            propertyKeys.add(socketName + descriptor.getName());
        }
    }
    return propertyKeys;
}
Also used : ProtocolHandler(org.apache.coyote.ProtocolHandler) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor)

Example 88 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project tomcat by apache.

the class StoreAppender method printAttributes.

/**
     * Store the relevant attributes of the specified JavaBean.
     *
     * @param writer PrintWriter to which we are storing
     * @param indent Indentation level
     * @param include
     *            Should we include a <code>className</code> attribute?
     * @param bean
     *            Bean whose properties are to be rendered as attributes,
     * @param desc
     *            RegistryDescriptor from this bean
     *
     * @exception Exception
     *                if an exception occurs while storing
     */
public void printAttributes(PrintWriter writer, int indent, boolean include, Object bean, StoreDescription desc) throws Exception {
    // Render a className attribute if requested
    if (include && desc != null && !desc.isStandard()) {
        writer.print(" className=\"");
        writer.print(bean.getClass().getName());
        writer.print("\"");
    }
    // Acquire the list of properties for this bean
    PropertyDescriptor[] descriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }
    // Create blank instance
    Object bean2 = defaultInstance(bean);
    for (int i = 0; i < descriptors.length; i++) {
        if (descriptors[i] instanceof IndexedPropertyDescriptor) {
            // Indexed properties are not persisted
            continue;
        }
        if (!isPersistable(descriptors[i].getPropertyType()) || (descriptors[i].getReadMethod() == null) || (descriptors[i].getWriteMethod() == null)) {
            // Must be a read-write primitive or String
            continue;
        }
        if (desc.isTransientAttribute(descriptors[i].getName())) {
            // Skip the specified exceptions
            continue;
        }
        Object value = IntrospectionUtils.getProperty(bean, descriptors[i].getName());
        if (value == null) {
            // Null values are not persisted
            continue;
        }
        Object value2 = IntrospectionUtils.getProperty(bean2, descriptors[i].getName());
        if (value.equals(value2)) {
            // The property has its default value
            continue;
        }
        printAttribute(writer, indent, bean, desc, descriptors[i].getName(), bean2, value);
    }
    if (bean instanceof ResourceBase) {
        ResourceBase resource = (ResourceBase) bean;
        for (Iterator<String> iter = resource.listProperties(); iter.hasNext(); ) {
            String name = iter.next();
            Object value = resource.getProperty(name);
            if (!isPersistable(value.getClass())) {
                continue;
            }
            if (desc.isTransientAttribute(name)) {
                // Skip the specified exceptions
                continue;
            }
            printValue(writer, indent, name, value);
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) ResourceBase(org.apache.tomcat.util.descriptor.web.ResourceBase) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor)

Example 89 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project ACS by ACS-Community.

the class BeanNode method computeProperties.

/** 
   * Computes a descriptor for properties from a bean info.
   * @param bean bean to create properties for
   * @param info about the bean
   * @param ignoreHiddenProperties true if hidden property should be ignored completely
   * @param propertyInfo extra information of some properties (possibly null)
   * @return three property lists
   */
private static Descriptor computeProperties(Object bean, BeanInfo info, boolean ignoreHiddenProperties, Boolean nodePropertiesCacheable, PropertyInfo[] propertyInfo) {
    java.util.List property = null;
    java.util.List expert = null;
    java.util.List hidden = null;
    PropertyDescriptor[] propertyDescriptor = info.getPropertyDescriptors();
    int k = propertyDescriptor.length;
    for (int i = 0; i < k; i++) {
        if (propertyDescriptor[i].getPropertyType() == null)
            continue;
        String propName = propertyDescriptor[i].getName();
        // we first update the PropertyDescriptor with the PropertyInfo
        // that update may make non hidden a property that was hidden
        PropertyInfo propInfo = findPropertyInfoByName(propertyInfo, propName);
        if (propInfo != null)
            propInfo.updatePropertyDescriptor(propertyDescriptor[i]);
        // after the update we can test whether the property is hidden or not
        if (ignoreHiddenProperties && propertyDescriptor[i].isHidden()) {
            continue;
        }
        CachingStrategy strategy = createCachingStrategy(nodePropertiesCacheable, BeanTagger.isCacheable(propertyDescriptor[i]));
        Node.Property prop;
        if (propertyDescriptor[i] instanceof IndexedPropertyDescriptor) {
            prop = createIndexedNodeProperty(bean, (IndexedPropertyDescriptor) propertyDescriptor[i], strategy);
        } else {
            prop = createNodeProperty(bean, propertyDescriptor[i], strategy);
        }
        if (prop == null)
            continue;
        if (propertyDescriptor[i].isHidden()) {
            if (hidden == null)
                hidden = new java.util.ArrayList();
            hidden.add(prop);
        } else if (propertyDescriptor[i].isExpert()) {
            if (expert == null)
                expert = new java.util.ArrayList();
            expert.add(prop);
        } else {
            if (property == null)
                property = new java.util.ArrayList();
            property.add(prop);
        }
    }
    // for    
    return new Descriptor(property, expert, hidden);
}
Also used : IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) Node(org.openide.nodes.Node) CachingStrategy(cern.gp.nodes.cache.CachingStrategy) TimeLimitedCachingStrategy(cern.gp.nodes.cache.TimeLimitedCachingStrategy) StickyCachingStrategy(cern.gp.nodes.cache.StickyCachingStrategy) NoCachingStrategy(cern.gp.nodes.cache.NoCachingStrategy) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyInfo(cern.gp.beans.PropertyInfo)

Example 90 with IndexedPropertyDescriptor

use of java.beans.IndexedPropertyDescriptor in project jdk8u_jdk by JetBrains.

the class Test7172865 method main.

public static void main(String[] args) throws Exception {
    int errors = 0;
    MethodDescriptor md = new MethodDescriptor(Test7172865.class.getMethod("getGood"));
    errors += test(PropertyDescriptor.class, "good", true);
    PropertyDescriptor pdGoodString = new PropertyDescriptor("good", Test7172865.class, "getGood", "setGood");
    PropertyDescriptor pdGoodMethod = new PropertyDescriptor("good", Test7172865.class.getMethod("getGood"), Test7172865.class.getMethod("setGood", args.getClass()));
    errors += test(PropertyDescriptor.class, "bad", false);
    PropertyDescriptor pdBadString = new PropertyDescriptor("bad", Test7172865.class, "getBad", null);
    PropertyDescriptor pdBadMethod = new PropertyDescriptor("bad", Test7172865.class.getMethod("getBad"), Test7172865.class.getMethod("setBad", args.getClass()));
    errors += test(IndexedPropertyDescriptor.class, "good", true);
    IndexedPropertyDescriptor ipdGoodString = new IndexedPropertyDescriptor("good", Test7172865.class, "getGood", "setGood", "getGood", "setGood");
    IndexedPropertyDescriptor ipdGoodMethod = new IndexedPropertyDescriptor("good", Test7172865.class.getMethod("getGood"), Test7172865.class.getMethod("setGood", args.getClass()), Test7172865.class.getMethod("getGood", Integer.TYPE), Test7172865.class.getMethod("setGood", Integer.TYPE, String.class));
    errors += test(IndexedPropertyDescriptor.class, "bad", false);
    IndexedPropertyDescriptor ipdBadString = new IndexedPropertyDescriptor("bad", Test7172865.class, "getBad", null, "getBad", null);
    IndexedPropertyDescriptor ipdBadMethod = new IndexedPropertyDescriptor("bad", Test7172865.class.getMethod("getBad"), Test7172865.class.getMethod("setBad", args.getClass()), Test7172865.class.getMethod("getBad", Integer.TYPE), Test7172865.class.getMethod("setBad", Integer.TYPE, String.class));
    for (int i = 1; i <= 2; i++) {
        System.out.println("STEP: " + i);
        errors += test("md", null != md.getMethod());
        errors += test("pdGoodString", pdGoodString, true, true);
        errors += test("pdGoodMethod", pdGoodMethod, true, true);
        errors += test("pdBadString", pdBadString, true, false);
        errors += test("pdBadMethod", pdBadMethod, true, true);
        errors += test("ipdGoodString", ipdGoodString, true, true, true, true);
        errors += test("ipdGoodMethod", ipdGoodMethod, true, true, true, true);
        errors += test("ipdBadString", ipdBadString, true, false, true, false);
        errors += test("ipdBadMethod", ipdBadMethod, true, true, true, true);
        try {
            int[] array = new int[1024];
            while (true) {
                array = new int[array.length << 1];
            }
        } catch (OutOfMemoryError error) {
            System.gc();
        }
    }
    if (errors > 0) {
        throw new Error("found " + errors + " errors");
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) MethodDescriptor(java.beans.MethodDescriptor)

Aggregations

IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)197 Method (java.lang.reflect.Method)167 PropertyDescriptor (java.beans.PropertyDescriptor)142 BeanInfo (java.beans.BeanInfo)126 SimpleBeanInfo (java.beans.SimpleBeanInfo)126 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)126 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)49 IntrospectionException (java.beans.IntrospectionException)17 EventSetDescriptor (java.beans.EventSetDescriptor)3 MethodDescriptor (java.beans.MethodDescriptor)2 PropertyInfo (cern.gp.beans.PropertyInfo)1 CachingStrategy (cern.gp.nodes.cache.CachingStrategy)1 NoCachingStrategy (cern.gp.nodes.cache.NoCachingStrategy)1 StickyCachingStrategy (cern.gp.nodes.cache.StickyCachingStrategy)1 TimeLimitedCachingStrategy (cern.gp.nodes.cache.TimeLimitedCachingStrategy)1 KeyEvent (java.awt.event.KeyEvent)1 KeyListener (java.awt.event.KeyListener)1 BeanDescriptor (java.beans.BeanDescriptor)1 FeatureDescriptor (java.beans.FeatureDescriptor)1 IndexedPropertyChangeEvent (java.beans.IndexedPropertyChangeEvent)1