Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not fail on getDeclaredXXX when user has used a SecurityManager #79

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
Expand All @@ -92,8 +92,8 @@
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
Expand Down
20 changes: 17 additions & 3 deletions src/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,12 @@ public static Map getMethods(Class targetClass, boolean staticMethods)
}

private static void collectMethods(Class c, Map result, boolean staticMethods) {
Method[] ma = c.getDeclaredMethods();
Method[] ma;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is loop processing involved below, there will probably be extra overhead for processing the try..catch multiple times within the loop. Since we're attempting the getDeclaredMethods vs. getMethods checks here it should be possible to set a boolean flag that determines which methodology the current SecurityManager allows.

      boolean smSupportsGetDeclaredMethods = true;
      try {
            ma = c.getDeclaredMethods();
        } catch (SecurityException ignored) {
            smSupportsGetDeclaredMethods = false;
            ma = c.getMethods();
        }

try {
ma = c.getDeclaredMethods();
} catch (SecurityException ignored) {
ma = c.getMethods();
}
for (int i = 0, icount = ma.length; i < icount; i++)
{
if (c.isInterface())
Expand Down Expand Up @@ -1966,7 +1971,11 @@ public static Map getFields(Class targetClass)
Field fa[];

result = new HashMap(23);
fa = targetClass.getDeclaredFields();
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then in the loop here the try..catch code could be replaced by something like:

     smSupportsGetDeclaredMethods ? fa = targetClass.getDeclaredFields() : fa = targetClass.getFields();

which limits the overhead of looping over a try..catch when the determination of which mechanism to use is known (someone would have to look at the bytecode to be certain, but a boolean check should be more efficient, especially over multiple iterations).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I got your idea but they aren't in same scope (method). Their call order isn't guaranteed. Furthermore, I'm not going to use a global flag because user might remove security manager at runtime after some calls. At bottom, according to my googling and experience, java security checks aren't expensive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @yasserzamani . I know this PR has already been merged, but I wanted to respond to make sure I understood your reply.

Ooops ... I missed that the two code blocks were in separate methods, and interpreted them as being in the same block scope with a loop involved. 😥 Since there's not actually any looping over a try..catch involved the new logic shouldn't introduce any noticeable overhead (especially since post-computation results are cached).

Thanks for clarifying and pointing out my mis-interpretation. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Frankly I had overlooked the performance. Thanks for your acknowledgement.

fa = targetClass.getDeclaredFields();
} catch (SecurityException ignored) {
fa = targetClass.getFields();
}
for (int i = 0; i < fa.length; i++) {
result.put(fa[i].getName(), fa[i]);
}
Expand Down Expand Up @@ -2264,7 +2273,12 @@ public static List getDeclaredMethods(Class targetClass, String propertyName, bo

private static void collectAccessors(Class c, String baseName, List result, boolean findSets)
{
final Method[] methods = c.getDeclaredMethods();
Method[] methods;
try {
methods = c.getDeclaredMethods();
} catch (SecurityException ignored) {
methods = c.getMethods();
}
for (int i = 0; i < methods.length; i++) {
if (c.isInterface()) {
if (isDefaultMethod(methods[i])) {
Expand Down