author | malenkov |
Fri, 15 Jun 2012 21:01:55 +0400 | |
changeset 13601 | a620e6a185a4 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
1844 | 1 |
/* |
13601
a620e6a185a4
7162473: ConstructorFinder/FieldFinder/MethodFinder gives access to restricted classes
malenkov
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. |
1844 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
1844 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
1844 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
1844 | 24 |
*/ |
25 |
package com.sun.beans.finder; |
|
26 |
||
27 |
import java.lang.reflect.Field; |
|
28 |
import java.lang.reflect.Modifier; |
|
29 |
||
13601
a620e6a185a4
7162473: ConstructorFinder/FieldFinder/MethodFinder gives access to restricted classes
malenkov
parents:
5506
diff
changeset
|
30 |
import static sun.reflect.misc.ReflectUtil.isPackageAccessible; |
a620e6a185a4
7162473: ConstructorFinder/FieldFinder/MethodFinder gives access to restricted classes
malenkov
parents:
5506
diff
changeset
|
31 |
|
1844 | 32 |
/** |
33 |
* This utility class provides {@code static} methods |
|
34 |
* to find a public field with specified name |
|
35 |
* in specified class. |
|
36 |
* |
|
37 |
* @since 1.7 |
|
38 |
* |
|
39 |
* @author Sergey A. Malenkov |
|
40 |
*/ |
|
41 |
public final class FieldFinder { |
|
42 |
||
43 |
/** |
|
44 |
* Finds public field (static or non-static) |
|
45 |
* that is declared in public class. |
|
46 |
* |
|
47 |
* @param type the class that can have field |
|
48 |
* @param name the name of field to find |
|
49 |
* @return object that represents found field |
|
50 |
* @throws NoSuchFieldException if field is not found |
|
51 |
* @see Class#getField |
|
52 |
*/ |
|
53 |
public static Field findField(Class<?> type, String name) throws NoSuchFieldException { |
|
54 |
if (name == null) { |
|
55 |
throw new IllegalArgumentException("Field name is not set"); |
|
56 |
} |
|
57 |
Field field = type.getField(name); |
|
58 |
if (!Modifier.isPublic(field.getModifiers())) { |
|
59 |
throw new NoSuchFieldException("Field '" + name + "' is not public"); |
|
60 |
} |
|
13601
a620e6a185a4
7162473: ConstructorFinder/FieldFinder/MethodFinder gives access to restricted classes
malenkov
parents:
5506
diff
changeset
|
61 |
type = field.getDeclaringClass(); |
a620e6a185a4
7162473: ConstructorFinder/FieldFinder/MethodFinder gives access to restricted classes
malenkov
parents:
5506
diff
changeset
|
62 |
if (!Modifier.isPublic(type.getModifiers()) || !isPackageAccessible(type)) { |
1844 | 63 |
throw new NoSuchFieldException("Field '" + name + "' is not accessible"); |
64 |
} |
|
65 |
return field; |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Finds public non-static field |
|
70 |
* that is declared in public class. |
|
71 |
* |
|
72 |
* @param type the class that can have field |
|
73 |
* @param name the name of field to find |
|
74 |
* @return object that represents found field |
|
75 |
* @throws NoSuchFieldException if field is not found |
|
76 |
* @see Class#getField |
|
77 |
*/ |
|
78 |
public static Field findInstanceField(Class<?> type, String name) throws NoSuchFieldException { |
|
79 |
Field field = findField(type, name); |
|
80 |
if (Modifier.isStatic(field.getModifiers())) { |
|
81 |
throw new NoSuchFieldException("Field '" + name + "' is static"); |
|
82 |
} |
|
83 |
return field; |
|
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Finds public static field |
|
88 |
* that is declared in public class. |
|
89 |
* |
|
90 |
* @param type the class that can have field |
|
91 |
* @param name the name of field to find |
|
92 |
* @return object that represents found field |
|
93 |
* @throws NoSuchFieldException if field is not found |
|
94 |
* @see Class#getField |
|
95 |
*/ |
|
96 |
public static Field findStaticField(Class<?> type, String name) throws NoSuchFieldException { |
|
97 |
Field field = findField(type, name); |
|
98 |
if (!Modifier.isStatic(field.getModifiers())) { |
|
99 |
throw new NoSuchFieldException("Field '" + name + "' is not static"); |
|
100 |
} |
|
101 |
return field; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Disable instantiation. |
|
106 |
*/ |
|
107 |
private FieldFinder() { |
|
108 |
} |
|
109 |
} |