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    *      https://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.beanutils2.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<>();
37  
38          public List<String> getSomeList() {
39              return someList;
40          }
41  
42          public String getSomeList(final int i) {
43              return someList.get(i);
44          }
45  
46          public void setSomeList(final int i, final String value) {
47              someList.set(i, value);
48          }
49  
50          public void setSomeList(final List<String> someList) {
51              this.someList = someList;
52          }
53      }
54  
55      public static boolean supportsIndexedLists() throws IntrospectionException {
56          final BeanInfo beanInfo = Introspector.getBeanInfo(IndexedBean.class);
57          for (final PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
58              if (pd.getName().equals("someList")) {
59                  return pd instanceof IndexedPropertyDescriptor;
60              }
61          }
62          throw new IllegalStateException("Could not find PropertyDescriptor for 'file'");
63      }
64  
65  }