View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.beanutils.bugs.other;
18  
19  import java.beans.BeanInfo;
20  import java.beans.IndexedPropertyDescriptor;
21  import java.beans.IntrospectionException;
22  import java.beans.Introspector;
23  import java.beans.PropertyDescriptor;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * Test if BeanInfo supports index properties for java.util.List
29   * <p>
30   * This was supported by Java until Java 8 (BEANUTILS-492).
31   *
32   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-492">BEANUTILS-492</a>
33   */
34  public class Jira492IndexedListsSupport {
35      public static class IndexedBean {
36          private List<String> someList = new ArrayList<String>();
37          public List<String> getSomeList() {
38              return someList;
39          }
40          public void setSomeList(List<String> someList) {
41              this.someList = someList;
42          }
43          public void setSomeList(int i, String value) {
44              someList.set(i, value);
45          }
46          public String getSomeList(int i) {
47              return someList.get(i);
48          }
49      }
50  
51      public static boolean supportsIndexedLists() throws IntrospectionException {
52          BeanInfo beanInfo = Introspector.getBeanInfo(IndexedBean.class);
53          for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
54              if (pd.getName().equals("someList")) {
55                  return pd instanceof IndexedPropertyDescriptor;
56              }
57          }
58          throw new IllegalStateException("Could not find PropertyDescriptor for 'file'");
59      }
60  
61  }