Author: mcucchiara Date: Fri Oct 28 16:42:27 2011 New Revision: 1190434 URL: http://svn.apache.org/viewvc?rev=1190434&view=rev Log: OGNL-31 - Some CPD fixes around ASTMethod and ASTStaticMethod (contributed by Adrian Cumiskey)
Added: commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethodUtil.java Modified: commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethod.java commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTStaticMethod.java commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/EvaluationPool.java commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/OgnlRuntime.java commons/proper/ognl/trunk/src/test/java/org/apache/commons/ognl/TestOgnlRuntime.java Modified: commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethod.java URL: http://svn.apache.org/viewvc/commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethod.java?rev=1190434&r1=1190433&r2=1190434&view=diff ============================================================================== --- commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethod.java (original) +++ commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethod.java Fri Oct 28 16:42:27 2011 @@ -19,38 +19,8 @@ package org.apache.commons.ognl; * under the License. */ -//-------------------------------------------------------------------------- -//Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard -//All rights reserved. - -//Redistribution and use in source and binary forms, with or without -//modification, are permitted provided that the following conditions are -//met: - -//Redistributions of source code must retain the above copyright notice, -//this list of conditions and the following disclaimer. -//Redistributions in binary form must reproduce the above copyright -//notice, this list of conditions and the following disclaimer in the -//documentation and/or other materials provided with the distribution. -//Neither the name of the Drew Davidson nor the names of its contributors -//may be used to endorse or promote products derived from this software -//without specific prior written permission. - -//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -//COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS -//OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED -//AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -//THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH -//DAMAGE. -//-------------------------------------------------------------------------- - import org.apache.commons.ognl.enhance.ExpressionCompiler; +import org.apache.commons.ognl.enhance.OgnlExpressionCompiler; import org.apache.commons.ognl.enhance.OrderedReturn; import org.apache.commons.ognl.enhance.UnsupportedCompilationException; @@ -58,6 +28,7 @@ import java.lang.reflect.Method; /** * $Id$ + * * @author Luke Blanshard (blans...@netscape.net) * @author Drew Davidson (d...@ognl.org) */ @@ -85,8 +56,9 @@ public class ASTMethod } /** - * Called from parser action. - * @param methodName sets the name of the method + * Called from parser action. + * + * @param methodName sets the name of the method */ public void setMethodName( String methodName ) { @@ -95,6 +67,7 @@ public class ASTMethod /** * Returns the method name that this node will call. + * * @return the method name */ public String getMethodName() @@ -152,6 +125,7 @@ public class ASTMethod { return getterClass; } + public String toGetSourceString( OgnlContext context, Object target ) { /* @@ -162,43 +136,42 @@ public class ASTMethod { throw new UnsupportedCompilationException( "Target object is null." ); } - + String post = ""; String result; - Method m; + Method method; + OgnlExpressionCompiler compiler = OgnlRuntime.getCompiler( context ); try { - m = - OgnlRuntime.getMethod( context, - context.getCurrentType() != null ? context.getCurrentType() : target.getClass(), - methodName, _children, false ); - if ( m == null ) + method = OgnlRuntime.getMethod( context, context.getCurrentType() != null + ? context.getCurrentType() + : target.getClass(), methodName, _children, false ); + if ( method == null ) { - m = OgnlRuntime - .getReadMethod( target.getClass(), methodName, _children != null ? _children.length : -1 ); + method = OgnlRuntime.getReadMethod( target.getClass(), methodName, + _children != null ? _children.length : -1 ); } - - if ( m == null ) + + if ( method == null ) { - m = - OgnlRuntime.getWriteMethod( target.getClass(), methodName, _children != null ? _children.length - : -1 ); + method = OgnlRuntime.getWriteMethod( target.getClass(), methodName, + _children != null ? _children.length : -1 ); - if ( m != null ) + if ( method != null ) { - context.setCurrentType( m.getReturnType() ); + context.setCurrentType( method.getReturnType() ); context.setCurrentAccessor( - OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, m.getDeclaringClass() ) ); + compiler.getSuperOrInterfaceClass( method, method.getDeclaringClass() ) ); coreExpression = toSetSourceString( context, target ); if ( coreExpression == null || coreExpression.length() < 1 ) { throw new UnsupportedCompilationException( "can't find suitable getter method" ); } - + coreExpression += ";"; lastExpression = "null"; @@ -210,12 +183,12 @@ public class ASTMethod else { - getterClass = m.getReturnType(); + getterClass = method.getReturnType(); } // TODO: This is a hacky workaround until javassist supports varargs method invocations - boolean varArgs = OgnlRuntime.isJdk15() && m.isVarArgs(); + boolean varArgs = OgnlRuntime.isJdk15() && method.isVarArgs(); if ( varArgs ) { @@ -223,11 +196,11 @@ public class ASTMethod "Javassist does not currently support varargs method calls" ); } - result = "." + m.getName() + "("; + result = "." + method.getName() + "("; if ( ( _children != null ) && ( _children.length > 0 ) ) { - Class[] parms = m.getParameterTypes(); + Class[] parms = method.getParameterTypes(); String prevCast = (String) context.remove( ExpressionCompiler.PRE_CAST ); /* * System.out.println("before children methodName is " + methodName + " for target " + target + @@ -244,95 +217,22 @@ public class ASTMethod Class prevType = context.getCurrentType(); - context.setCurrentObject( context.getRoot() ); - context.setCurrentType( context.getRoot() != null ? context.getRoot().getClass() : null ); + Object root = context.getRoot(); + context.setCurrentObject( root ); + context.setCurrentType( root != null ? root.getClass() : null ); context.setCurrentAccessor( null ); context.setPreviousType( null ); - Object value = _children[i].getValue( context, context.getRoot() ); - String parmString = _children[i].toGetSourceString( context, context.getRoot() ); + Node child = _children[i]; - if ( parmString == null || parmString.trim().length() < 1 ) - { - parmString = "null"; - } - // to undo type setting of constants when used as method parameters - if ( ASTConst.class.isInstance( _children[i] ) ) - { - context.setCurrentType( prevType ); - } + String parmString = ASTMethodUtil.getParmString( context, root, child, prevType ); - parmString = - ExpressionCompiler.getRootExpression( _children[i], context.getRoot(), context ) + parmString; + Class valueClass = ASTMethodUtil.getValueClass( context, root, child ); - String cast = ""; - if ( ExpressionCompiler.shouldCast( _children[i] ) ) - { - cast = (String) context.remove( ExpressionCompiler.PRE_CAST ); - } - if ( cast == null ) - { - cast = ""; - } - - if ( !ASTConst.class.isInstance( _children[i] ) ) - { - parmString = cast + parmString; - } - - Class valueClass = value != null ? value.getClass() : null; - if ( NodeType.class.isAssignableFrom( _children[i].getClass() ) ) - { - valueClass = ( (NodeType) _children[i] ).getGetterClass(); - } - if ( ( !varArgs || varArgs && ( i + 1 ) < parms.length ) && valueClass != parms[i] ) { - if ( parms[i].isArray() ) - { - - parmString = - OgnlRuntime - .getCompiler( context ) - .createLocalReference( context, "(" + ExpressionCompiler.getCastString( parms[i] ) - + ")org.apache.commons.ognl.OgnlOps#toArray(" + parmString + ", " - + parms[i].getComponentType().getName() + ".class, true)", parms[i] ); - - } - else if ( parms[i].isPrimitive() ) - { - - Class wrapClass = OgnlRuntime.getPrimitiveWrapperClass( parms[i] ); - - parmString = - OgnlRuntime - .getCompiler( context ) - .createLocalReference( context, "((" + wrapClass.getName() - + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," - + wrapClass.getName() + ".class, true))." + OgnlRuntime.getNumericValueGetter( - wrapClass ), parms[i] ); - - } - else if ( parms[i] != Object.class ) - { - parmString = - OgnlRuntime - .getCompiler( context ) - .createLocalReference( context, "(" + parms[i].getName() - + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," - + parms[i].getName() + ".class)", parms[i] ); - } - else if ( ( NodeType.class.isInstance( _children[i] ) - && ( (NodeType) _children[i] ).getGetterClass() != null - && Number.class.isAssignableFrom( ( (NodeType) _children[i] ).getGetterClass() ) ) - || ( valueClass != null && valueClass.isPrimitive() ) ) - { - parmString = " ($w) " + parmString; - } - else if ( valueClass != null && valueClass.isPrimitive() ) - { - parmString = "($w) " + parmString; - } + parmString = ASTMethodUtil.getParmString( context, parms[i], parmString, child, valueClass, + ".class, true)" ); } result += parmString; @@ -362,14 +262,14 @@ public class ASTMethod result += ")" + post; - if ( m.getReturnType() == void.class ) + if ( method.getReturnType() == void.class ) { coreExpression = result + ";"; lastExpression = "null"; } - context.setCurrentType( m.getReturnType() ); - context.setCurrentAccessor( OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, m.getDeclaringClass() ) ); + context.setCurrentType( method.getReturnType() ); + context.setCurrentAccessor( compiler.getSuperOrInterfaceClass( method, method.getDeclaringClass() ) ); return result; } @@ -385,15 +285,15 @@ public class ASTMethod methodName, _children != null ? _children.length : -1 ); if ( m == null ) { - throw new UnsupportedCompilationException( "Unable to determine setter method generation for " - + methodName ); + throw new UnsupportedCompilationException( + "Unable to determine setter method generation for " + methodName ); } String post = ""; String result = "." + m.getName() + "("; - if ( m.getReturnType() != void.class && m.getReturnType().isPrimitive() - && ( _parent == null || !ASTTest.class.isInstance( _parent ) ) ) + if ( m.getReturnType() != void.class && m.getReturnType().isPrimitive() && ( _parent == null + || !ASTTest.class.isInstance( _parent ) ) ) { Class wrapper = OgnlRuntime.getPrimitiveWrapperClass( m.getReturnType() ); @@ -409,6 +309,7 @@ public class ASTMethod throw new UnsupportedCompilationException( "Javassist does not currently support varargs method calls" ); } + OgnlExpressionCompiler compiler = OgnlRuntime.getCompiler( context ); try { /* @@ -435,19 +336,19 @@ public class ASTMethod context.setCurrentAccessor( null ); context.setPreviousType( null ); - Object value = _children[i].getValue( context, context.getRoot() ); - String parmString = _children[i].toSetSourceString( context, context.getRoot() ); + Node child = _children[i]; + Object value = child.getValue( context, context.getRoot() ); + String parmString = child.toSetSourceString( context, context.getRoot() ); if ( context.getCurrentType() == Void.TYPE || context.getCurrentType() == void.class ) { throw new UnsupportedCompilationException( "Method argument can't be a void type." ); } - + if ( parmString == null || parmString.trim().length() < 1 ) { - if ( ASTProperty.class.isInstance( _children[i] ) || ASTMethod.class.isInstance( _children[i] ) - || ASTStaticMethod.class.isInstance( _children[i] ) - || ASTChain.class.isInstance( _children[i] ) ) + if ( ASTProperty.class.isInstance( child ) || ASTMethod.class.isInstance( child ) + || ASTStaticMethod.class.isInstance( child ) || ASTChain.class.isInstance( child ) ) { throw new UnsupportedCompilationException( "ASTMethod setter child returned null from a sub property expression." ); @@ -456,16 +357,15 @@ public class ASTMethod } // to undo type setting of constants when used as method parameters - if ( ASTConst.class.isInstance( _children[i] ) ) + if ( ASTConst.class.isInstance( child ) ) { context.setCurrentType( prevType ); } - parmString = - ExpressionCompiler.getRootExpression( _children[i], context.getRoot(), context ) + parmString; + parmString = ExpressionCompiler.getRootExpression( child, context.getRoot(), context ) + parmString; String cast = ""; - if ( ExpressionCompiler.shouldCast( _children[i] ) ) + if ( ExpressionCompiler.shouldCast( child ) ) { cast = (String) context.remove( ExpressionCompiler.PRE_CAST ); } @@ -474,61 +374,19 @@ public class ASTMethod { cast = ""; } - + parmString = cast + parmString; Class valueClass = value != null ? value.getClass() : null; - if ( NodeType.class.isAssignableFrom( _children[i].getClass() ) ) + if ( NodeType.class.isAssignableFrom( child.getClass() ) ) { - valueClass = ( (NodeType) _children[i] ).getGetterClass(); + valueClass = ( (NodeType) child ).getGetterClass(); } - + if ( valueClass != parms[i] ) { - if ( parms[i].isArray() ) - { - parmString = - OgnlRuntime - .getCompiler( context ) - .createLocalReference( context, "(" + ExpressionCompiler.getCastString( parms[i] ) - + ")org.apache.commons.ognl.OgnlOps#toArray(" + parmString + ", " - + parms[i].getComponentType().getName() + ".class)", parms[i] ); - - } - else if ( parms[i].isPrimitive() ) - { - Class wrapClass = OgnlRuntime.getPrimitiveWrapperClass( parms[i] ); - - parmString = - OgnlRuntime - .getCompiler( context ) - .createLocalReference( context, "((" + wrapClass.getName() - + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," - + wrapClass.getName() + ".class, true))." + OgnlRuntime.getNumericValueGetter( - wrapClass ), parms[i] ); - - } - else if ( parms[i] != Object.class ) - { - parmString = - OgnlRuntime - .getCompiler( context ) - .createLocalReference( context, "(" + parms[i].getName() - + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," - + parms[i].getName() + ".class)", parms[i] ); - - } - else if ( ( NodeType.class.isInstance( _children[i] ) - && ( (NodeType) _children[i] ).getGetterClass() != null - && Number.class.isAssignableFrom( ( (NodeType) _children[i] ).getGetterClass() ) ) - || ( valueClass != null && valueClass.isPrimitive() ) ) - { - parmString = " ($w) " + parmString; - } - else if ( valueClass != null && valueClass.isPrimitive() ) - { - parmString = "($w) " + parmString; - } + parmString = + ASTMethodUtil.getParmString( context, parms[i], parmString, child, valueClass, ".class)" ); } result += parmString; @@ -557,11 +415,11 @@ public class ASTMethod } context.setCurrentType( m.getReturnType() ); - context.setCurrentAccessor( OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, m.getDeclaringClass() ) ); + context.setCurrentAccessor( compiler.getSuperOrInterfaceClass( m, m.getDeclaringClass() ) ); return result + ")" + post; } - + public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data ) throws OgnlException { Added: commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethodUtil.java URL: http://svn.apache.org/viewvc/commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethodUtil.java?rev=1190434&view=auto ============================================================================== --- commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethodUtil.java (added) +++ commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTMethodUtil.java Fri Oct 28 16:42:27 2011 @@ -0,0 +1,122 @@ +package org.apache.commons.ognl; + +import org.apache.commons.ognl.enhance.ExpressionCompiler; +import org.apache.commons.ognl.enhance.OgnlExpressionCompiler; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * $Id: $ + */ +class ASTMethodUtil +{ + + private ASTMethodUtil() + { + } + + static String getParmString( OgnlContext context, Object root, Node child, Class prevType ) + throws OgnlException + { + String parmString = child.toGetSourceString( context, root ); + + if ( parmString == null || parmString.trim().length() < 1 ) + { + parmString = "null"; + } + + // to undo type setting of constants when used as method parameters + if ( ASTConst.class.isInstance( child ) ) + { + context.setCurrentType( prevType ); + } + + parmString = ExpressionCompiler.getRootExpression( child, root, context ) + parmString; + + String cast = ""; + if ( ExpressionCompiler.shouldCast( child ) ) + { + cast = (String) context.remove( ExpressionCompiler.PRE_CAST ); + } + + if ( cast == null ) + { + cast = ""; + } + + if ( !ASTConst.class.isInstance( child ) ) + { + parmString = cast + parmString; + } + return parmString; + } + + static Class getValueClass( OgnlContext context, Object root, Node child ) + throws OgnlException + { + Object value = child.getValue( context, root ); + Class valueClass = value != null ? value.getClass() : null; + if ( NodeType.class.isAssignableFrom( child.getClass() ) ) + { + valueClass = ( (NodeType) child ).getGetterClass(); + } + return valueClass; + } + + static String getParmString( OgnlContext context, Class parm, String parmString, Node child, Class valueClass, + String endParam ) + { + OgnlExpressionCompiler compiler = OgnlRuntime.getCompiler( context ); + if ( parm.isArray() ) + { + parmString = compiler.createLocalReference( context, "(" + ExpressionCompiler.getCastString( parm ) + + ")org.apache.commons.ognl.OgnlOps#toArray(" + parmString + ", " + parm.getComponentType().getName() + + endParam, parm ); + + } + else if ( parm.isPrimitive() ) + { + Class wrapClass = OgnlRuntime.getPrimitiveWrapperClass( parm ); + + parmString = compiler.createLocalReference( context, "((" + wrapClass.getName() + + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," + wrapClass.getName() + + ".class, true))." + OgnlRuntime.getNumericValueGetter( wrapClass ), parm ); + + } + else if ( parm != Object.class ) + { + parmString = compiler.createLocalReference( context, "(" + parm.getName() + + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," + parm.getName() + ".class)", + parm ); + + } + else if ( ( NodeType.class.isInstance( child ) && ( (NodeType) child ).getGetterClass() != null + && Number.class.isAssignableFrom( ( (NodeType) child ).getGetterClass() ) ) || ( valueClass != null + && valueClass.isPrimitive() ) ) + { + parmString = " ($w) " + parmString; + } + else if ( valueClass != null && valueClass.isPrimitive() ) + { + parmString = "($w) " + parmString; + } + return parmString; + } +} Modified: commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTStaticMethod.java URL: http://svn.apache.org/viewvc/commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTStaticMethod.java?rev=1190434&r1=1190433&r2=1190434&view=diff ============================================================================== --- commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTStaticMethod.java (original) +++ commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/ASTStaticMethod.java Fri Oct 28 16:42:27 2011 @@ -20,11 +20,14 @@ package org.apache.commons.ognl; */ import org.apache.commons.ognl.enhance.ExpressionCompiler; +import org.apache.commons.ognl.enhance.OgnlExpressionCompiler; import org.apache.commons.ognl.enhance.UnsupportedCompilationException; + import java.lang.reflect.Method; /** * $Id$ + * * @author Luke Blanshard (blans...@netscape.net) * @author Drew Davidson (d...@ognl.org) */ @@ -49,7 +52,9 @@ public class ASTStaticMethod super( p, id ); } - /** Called from parser action. */ + /** + * Called from parser action. + */ void init( String className, String methodName ) { this.className = className; @@ -59,7 +64,8 @@ public class ASTStaticMethod protected Object getValueBody( OgnlContext context, Object source ) throws OgnlException { - Object[] args = OgnlRuntime.getObjectArrayPool().create( jjtGetNumChildren() ); + ObjectArrayPool objectArrayPool = OgnlRuntime.getObjectArrayPool(); + Object[] args = objectArrayPool.create( jjtGetNumChildren() ); Object root = context.getRoot(); try @@ -68,12 +74,12 @@ public class ASTStaticMethod { args[i] = _children[i].getValue( context, root ); } - + return OgnlRuntime.callStaticMethod( context, className, methodName, args ); } finally { - OgnlRuntime.getObjectArrayPool().recycle( args ); + objectArrayPool.recycle( args ); } } @@ -98,18 +104,18 @@ public class ASTStaticMethod if ( clazz == null || m == null ) { - throw new UnsupportedCompilationException( "Unable to find class/method combo " + className + " / " - + methodName ); + throw new UnsupportedCompilationException( + "Unable to find class/method combo " + className + " / " + methodName ); } - + if ( !context.getMemberAccess().isAccessible( context, clazz, m, methodName ) ) { throw new UnsupportedCompilationException( - "Method is not accessible, check your jvm runtime security settings. " - + "For static class method " + className + " / " - + methodName ); + "Method is not accessible, check your jvm runtime security settings. " + "For static class method " + + className + " / " + methodName ); } + OgnlExpressionCompiler compiler = OgnlRuntime.getCompiler( context ); if ( ( _children != null ) && ( _children.length > 0 ) ) { Class[] parms = m.getParameterTypes(); @@ -123,91 +129,40 @@ public class ASTStaticMethod Class prevType = context.getCurrentType(); - Object value = _children[i].getValue( context, context.getRoot() ); - String parmString = _children[i].toGetSourceString( context, context.getRoot() ); - - if ( parmString == null || parmString.trim().length() < 1 ) - { - parmString = "null"; - } - - // to undo type setting of constants when used as method parameters - if ( ASTConst.class.isInstance( _children[i] ) ) - { - context.setCurrentType( prevType ); - } + Node child = _children[i]; + Object root = context.getRoot(); - parmString = - ExpressionCompiler.getRootExpression( _children[i], context.getRoot(), context ) + parmString; + String parmString = ASTMethodUtil.getParmString( context, root, child, prevType ); - String cast = ""; - if ( ExpressionCompiler.shouldCast( _children[i] ) ) - { - cast = (String) context.remove( ExpressionCompiler.PRE_CAST ); - } + Class valueClass = ASTMethodUtil.getValueClass( context, root, child ); - if ( cast == null ) - { - cast = ""; - } - - if ( !ASTConst.class.isInstance( _children[i] ) ) - { - parmString = cast + parmString; - } - - Class valueClass = value != null ? value.getClass() : null; - if ( NodeType.class.isAssignableFrom( _children[i].getClass() ) ) - { - valueClass = ( (NodeType) _children[i] ).getGetterClass(); - } - if ( valueClass != parms[i] ) { if ( parms[i].isArray() ) { - parmString = - OgnlRuntime.getCompiler( context ).createLocalReference( context, - "(" - + ExpressionCompiler.getCastString( parms[i] ) - + ")org.apache.commons.ognl.OgnlOps.toArray(" - + parmString - + ", " - + parms[i].getComponentType().getName() - + ".class, true)", parms[i] ); + parmString = compiler.createLocalReference( context, "(" + ExpressionCompiler.getCastString( + parms[i] ) + ")org.apache.commons.ognl.OgnlOps.toArray(" + parmString + ", " + + parms[i].getComponentType().getName() + ".class, true)", parms[i] ); } else if ( parms[i].isPrimitive() ) { Class wrapClass = OgnlRuntime.getPrimitiveWrapperClass( parms[i] ); - parmString = - OgnlRuntime.getCompiler( context ).createLocalReference( context, - "((" - + wrapClass.getName() - + ")org.apache.commons.ognl.OgnlOps.convertValue(" - + parmString - + "," - + wrapClass.getName() - + ".class, true))." - + OgnlRuntime.getNumericValueGetter( wrapClass ), - parms[i] ); + parmString = compiler.createLocalReference( context, "((" + wrapClass.getName() + + ")org.apache.commons.ognl.OgnlOps.convertValue(" + parmString + "," + + wrapClass.getName() + ".class, true))." + OgnlRuntime.getNumericValueGetter( + wrapClass ), parms[i] ); } else if ( parms[i] != Object.class ) { - parmString = - OgnlRuntime.getCompiler( context ).createLocalReference( context, - "(" - + parms[i].getName() - + ")org.apache.commons.ognl.OgnlOps.convertValue(" - + parmString + "," - + parms[i].getName() + ".class)", - parms[i] ); + parmString = compiler.createLocalReference( context, "(" + parms[i].getName() + + ")org.apache.commons.ognl.OgnlOps.convertValue(" + parmString + "," + + parms[i].getName() + ".class)", parms[i] ); } - else if ( ( NodeType.class.isInstance( _children[i] ) - && ( (NodeType) _children[i] ).getGetterClass() != null - && Number.class.isAssignableFrom( ( (NodeType) _children[i] ).getGetterClass() ) ) + else if ( ( NodeType.class.isInstance( child ) && ( (NodeType) child ).getGetterClass() != null + && Number.class.isAssignableFrom( ( (NodeType) child ).getGetterClass() ) ) || valueClass.isPrimitive() ) { parmString = " ($w) " + parmString; @@ -239,8 +194,7 @@ public class ASTStaticMethod getterClass = m.getReturnType(); context.setCurrentType( m.getReturnType() ); - context.setCurrentAccessor( - OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, m.getDeclaringClass() ) ); + context.setCurrentAccessor( compiler.getSuperOrInterfaceClass( m, m.getDeclaringClass() ) ); } } @@ -256,7 +210,7 @@ public class ASTStaticMethod { return toGetSourceString( context, target ); } - + public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data ) throws OgnlException { Modified: commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/EvaluationPool.java URL: http://svn.apache.org/viewvc/commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/EvaluationPool.java?rev=1190434&r1=1190433&r2=1190434&view=diff ============================================================================== --- commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/EvaluationPool.java (original) +++ commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/EvaluationPool.java Fri Oct 28 16:42:27 2011 @@ -31,6 +31,10 @@ public final class EvaluationPool this( 0 ); } + /* + * @deprecated evaluation-pool now relies on the jvm garbage collection + * therefore providing an initialSize is unnecessary + */ public EvaluationPool( int initialSize ) { super(); Modified: commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/OgnlRuntime.java URL: http://svn.apache.org/viewvc/commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/OgnlRuntime.java?rev=1190434&r1=1190433&r2=1190434&view=diff ============================================================================== --- commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/OgnlRuntime.java (original) +++ commons/proper/ognl/trunk/src/main/java/org/apache/commons/ognl/OgnlRuntime.java Fri Oct 28 16:42:27 2011 @@ -230,9 +230,6 @@ public class OgnlRuntime static final Cache<Method, Boolean> _methodPermCache = cacheFactory.createCache( methodPermCacheEntryFactory ); - static final Map<OgnlContext, OgnlExpressionCompiler> expressionCompilerMap = - new HashMap<OgnlContext, OgnlExpressionCompiler>(); - static ClassCacheInspector _cacheInspector; /** @@ -240,23 +237,6 @@ public class OgnlRuntime */ private static OgnlExpressionCompiler _compiler; - /** - * Lazy loading of Javassist library - */ - static - { - try - { - Class.forName( "javassist.ClassPool" ); - _compiler = new ExpressionCompiler(); - } - catch ( ClassNotFoundException e ) - { - throw new IllegalArgumentException( - "Javassist library is missing in classpath! Please add missed dependency!", e ); - } - } - private static Map<Class<?>, Class<?>> PRIMITIVE_WRAPPER_CLASSES = new IdentityHashMap<Class<?>, Class<?>>(); /** @@ -516,28 +496,25 @@ public class OgnlRuntime */ public static OgnlExpressionCompiler getCompiler() { - return _compiler != null ? _compiler : getCompiler( null ); + return getCompiler( null ); } public static OgnlExpressionCompiler getCompiler( OgnlContext ognlContext ) { - OgnlExpressionCompiler ognlExpressionCompiler = expressionCompilerMap.get( ognlContext ); - if ( ognlExpressionCompiler == null ) + if ( _compiler == null ) { try { OgnlRuntime.classForName( ognlContext, "javassist.ClassPool" ); - ognlExpressionCompiler = new ExpressionCompiler(); - expressionCompilerMap.put( ognlContext, ognlExpressionCompiler ); + _compiler = new ExpressionCompiler(); } catch ( ClassNotFoundException e ) { throw new IllegalArgumentException( - "Javassist library is missing in classpath! Please add missed dependency!", - e ); + "Javassist library is missing in classpath! Please add missed dependency!", e ); } } - return ognlExpressionCompiler; + return _compiler; } public static void compileExpression( OgnlContext context, Node expression, Object root ) Modified: commons/proper/ognl/trunk/src/test/java/org/apache/commons/ognl/TestOgnlRuntime.java URL: http://svn.apache.org/viewvc/commons/proper/ognl/trunk/src/test/java/org/apache/commons/ognl/TestOgnlRuntime.java?rev=1190434&r1=1190433&r2=1190434&view=diff ============================================================================== --- commons/proper/ognl/trunk/src/test/java/org/apache/commons/ognl/TestOgnlRuntime.java (original) +++ commons/proper/ognl/trunk/src/test/java/org/apache/commons/ognl/TestOgnlRuntime.java Fri Oct 28 16:42:27 2011 @@ -19,6 +19,7 @@ */ package org.apache.commons.ognl; +import org.apache.commons.ognl.enhance.OgnlExpressionCompiler; import org.apache.commons.ognl.internal.CacheException; import org.apache.commons.ognl.test.objects.BaseGeneric; import org.apache.commons.ognl.test.objects.Bean1; @@ -57,117 +58,120 @@ public class TestOgnlRuntime { @Test - public void test_Get_Super_Or_Interface_Class( ) + public void test_Get_Super_Or_Interface_Class() throws Exception { - ListSource list = new ListSourceImpl( ); + ListSource list = new ListSourceImpl(); - Method m = OgnlRuntime.getReadMethod( list.getClass( ), "total" ); + Method m = OgnlRuntime.getReadMethod( list.getClass(), "total" ); assertNotNull( m ); OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); - assertEquals( ListSource.class, OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, list.getClass() ) ); + assertEquals( ListSource.class, + OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, list.getClass() ) ); } @Test - public void test_Get_Private_Class( ) + public void test_Get_Private_Class() throws Exception { List list = Arrays.asList( "hello", "world" ); - Method m = OgnlRuntime.getReadMethod( list.getClass( ), "iterator" ); + Method m = OgnlRuntime.getReadMethod( list.getClass(), "iterator" ); assertNotNull( m ); OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); - assertEquals( Iterable.class, OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, list.getClass() ) ); + assertEquals( Iterable.class, + OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, list.getClass() ) ); } @Test - public void test_Complicated_Inheritance( ) + public void test_Complicated_Inheritance() throws Exception { - IForm form = new FormImpl( ); + IForm form = new FormImpl(); - Method m = OgnlRuntime.getWriteMethod( form.getClass( ), "clientId" ); + Method m = OgnlRuntime.getWriteMethod( form.getClass(), "clientId" ); assertNotNull( m ); OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); - assertEquals( IComponent.class, OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, form.getClass() ) ); + assertEquals( IComponent.class, + OgnlRuntime.getCompiler( context ).getSuperOrInterfaceClass( m, form.getClass() ) ); } @Test - public void test_Get_Read_Method( ) + public void test_Get_Read_Method() throws Exception { Method m = OgnlRuntime.getReadMethod( Bean2.class, "pageBreakAfter" ); assertNotNull( m ); - assertEquals( "isPageBreakAfter", m.getName( ) ); + assertEquals( "isPageBreakAfter", m.getName() ); } class TestGetters { - public boolean isEditorDisabled( ) + public boolean isEditorDisabled() { return false; } - public boolean isDisabled( ) + public boolean isDisabled() { return true; } - public boolean isNotAvailable( ) + public boolean isNotAvailable() { return false; } - public boolean isAvailable( ) + public boolean isAvailable() { return true; } } @Test - public void test_Get_Read_Method_Multiple( ) + public void test_Get_Read_Method_Multiple() throws Exception { Method m = OgnlRuntime.getReadMethod( TestGetters.class, "disabled" ); assertNotNull( m ); - assertEquals( "isDisabled", m.getName( ) ); + assertEquals( "isDisabled", m.getName() ); } @Test - public void test_Get_Read_Method_Multiple_Boolean_Getters( ) + public void test_Get_Read_Method_Multiple_Boolean_Getters() throws Exception { Method m = OgnlRuntime.getReadMethod( TestGetters.class, "available" ); assertNotNull( m ); - assertEquals( "isAvailable", m.getName( ) ); + assertEquals( "isAvailable", m.getName() ); m = OgnlRuntime.getReadMethod( TestGetters.class, "notAvailable" ); assertNotNull( m ); - assertEquals( "isNotAvailable", m.getName( ) ); + assertEquals( "isNotAvailable", m.getName() ); } @Test - public void test_Find_Method_Mixed_Boolean_Getters( ) + public void test_Find_Method_Mixed_Boolean_Getters() throws Exception { Method m = OgnlRuntime.getReadMethod( GetterMethods.class, "allowDisplay" ); assertNotNull( m ); - assertEquals( "getAllowDisplay", m.getName( ) ); + assertEquals( "getAllowDisplay", m.getName() ); } @Test - public void test_Get_Appropriate_Method( ) + public void test_Get_Appropriate_Method() throws Exception { - ListSource list = new ListSourceImpl( ); + ListSource list = new ListSourceImpl(); OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); Object ret = OgnlRuntime.callMethod( context, list, "addValue", new String[]{ null } ); @@ -176,7 +180,7 @@ public class TestOgnlRuntime } @Test - public void test_Call_Static_Method_Invalid_Class( ) + public void test_Call_Static_Method_Invalid_Class() { try @@ -191,18 +195,18 @@ public class TestOgnlRuntime { assertTrue( MethodFailedException.class.isInstance( et ) ); - assertTrue( et.getMessage( ).contains( "made.up.Name" ) ); + assertTrue( et.getMessage().contains( "made.up.Name" ) ); } } @Test - public void test_Setter_Returns( ) + public void test_Setter_Returns() throws Exception { OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); - SetterReturns root = new SetterReturns( ); + SetterReturns root = new SetterReturns(); - Method m = OgnlRuntime.getWriteMethod( root.getClass( ), "value" ); + Method m = OgnlRuntime.getWriteMethod( root.getClass(), "value" ); assertTrue( m != null ); Ognl.setValue( "value", context, root, "12__" ); @@ -210,47 +214,47 @@ public class TestOgnlRuntime } @Test - public void test_Call_Method_VarArgs( ) + public void test_Call_Method_VarArgs() throws Exception { OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); - GenericService service = new GenericServiceImpl( ); + GenericService service = new GenericServiceImpl(); - GameGenericObject argument = new GameGenericObject( ); + GameGenericObject argument = new GameGenericObject(); - Object[] args = OgnlRuntime.getObjectArrayPool( ).create( 2 ); + Object[] args = OgnlRuntime.getObjectArrayPool().create( 2 ); args[0] = argument; assertEquals( "Halo 3", OgnlRuntime.callMethod( context, service, "getFullMessageFor", args ) ); } @Test - public void test_Class_Cache_Inspector( ) + public void test_Class_Cache_Inspector() throws Exception { - OgnlRuntime.clearCache( ); - assertEquals( 0, OgnlRuntime._propertyDescriptorCache.getSize( ) ); + OgnlRuntime.clearCache(); + assertEquals( 0, OgnlRuntime._propertyDescriptorCache.getSize() ); - Root root = new Root( ); + Root root = new Root(); OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); Node expr = Ognl.compileExpression( context, root, "property.bean3.value != null" ); - assertTrue( (Boolean) expr.getAccessor( ).get( context, root ) ); + assertTrue( (Boolean) expr.getAccessor().get( context, root ) ); - int size = OgnlRuntime._propertyDescriptorCache.getSize( ); + int size = OgnlRuntime._propertyDescriptorCache.getSize(); assertTrue( size > 0 ); - OgnlRuntime.clearCache( ); - assertEquals( 0, OgnlRuntime._propertyDescriptorCache.getSize( ) ); + OgnlRuntime.clearCache(); + assertEquals( 0, OgnlRuntime._propertyDescriptorCache.getSize() ); // now register class cache prevention - OgnlRuntime.setClassCacheInspector( new TestCacheInspector( ) ); + OgnlRuntime.setClassCacheInspector( new TestCacheInspector() ); expr = Ognl.compileExpression( context, root, "property.bean3.value != null" ); - assertTrue( (Boolean) expr.getAccessor( ).get( context, root ) ); + assertTrue( (Boolean) expr.getAccessor().get( context, root ) ); - assertEquals( ( size - 1 ), OgnlRuntime._propertyDescriptorCache.getSize( ) ); + assertEquals( ( size - 1 ), OgnlRuntime._propertyDescriptorCache.getSize() ); } class TestCacheInspector @@ -264,7 +268,7 @@ public class TestOgnlRuntime } @Test - public void test_Set_Generic_Parameter_Types( ) + public void test_Set_Generic_Parameter_Types() throws Exception { OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); @@ -272,13 +276,13 @@ public class TestOgnlRuntime Method m = OgnlRuntime.getSetMethod( context, GenericCracker.class, "param" ); assertNotNull( m ); - Class[] types = m.getParameterTypes( ); + Class[] types = m.getParameterTypes(); assertEquals( 1, types.length ); assertEquals( Integer.class, types[0] ); } @Test - public void test_Get_Generic_Parameter_Types( ) + public void test_Get_Generic_Parameter_Types() throws Exception { OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); @@ -286,11 +290,11 @@ public class TestOgnlRuntime Method m = OgnlRuntime.getGetMethod( context, GenericCracker.class, "param" ); assertNotNull( m ); - assertEquals( Integer.class, m.getReturnType( ) ); + assertEquals( Integer.class, m.getReturnType() ); } @Test - public void test_Find_Parameter_Types( ) + public void test_Find_Parameter_Types() throws Exception { OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); @@ -304,7 +308,7 @@ public class TestOgnlRuntime } @Test - public void test_Find_Parameter_Types_Superclass( ) + public void test_Find_Parameter_Types_Superclass() throws Exception { OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); @@ -318,7 +322,7 @@ public class TestOgnlRuntime } @Test - public void test_Get_Declared_Methods_With_Synthetic_Methods( ) + public void test_Get_Declared_Methods_With_Synthetic_Methods() throws Exception { List result = OgnlRuntime.getDeclaredMethods( SubclassSyntheticObject.class, "list", false ); @@ -327,17 +331,17 @@ public class TestOgnlRuntime // "public volatile java.util.List org.ognl.test.objects.SubclassSyntheticObject.getList()", // causing method return size to be 3 - assertEquals( 2, result.size( ) ); + assertEquals( 2, result.size() ); } @Test - public void test_Get_Property_Descriptors_With_Synthetic_Methods( ) + public void test_Get_Property_Descriptors_With_Synthetic_Methods() throws Exception { PropertyDescriptor pd = OgnlRuntime.getPropertyDescriptor( SubclassSyntheticObject.class, "list" ); assert pd != null; - assert OgnlRuntime.isMethodCallable( pd.getReadMethod( ) ); + assert OgnlRuntime.isMethodCallable( pd.getReadMethod() ); } private static class GenericParent<T> @@ -364,7 +368,7 @@ public class TestOgnlRuntime * Tests OGNL parameter discovery. */ @Test - public void testOGNLParameterDiscovery( ) + public void testOGNLParameterDiscovery() throws NoSuchMethodException, CacheException { Method saveMethod = GenericParent.class.getMethod( "save", Object.class ); @@ -380,7 +384,7 @@ public class TestOgnlRuntime } @Test - public void testGetField( ) + public void testGetField() throws OgnlException { Field field = OgnlRuntime.getField( OtherObjectIndexed.class, "attributes" ); @@ -388,7 +392,7 @@ public class TestOgnlRuntime } @Test - public void testGetSetMethod( ) + public void testGetSetMethod() throws IntrospectionException, OgnlException { Method setter = OgnlRuntime.getSetMethod( null, Bean1.class, "bean2" ); @@ -396,4 +400,14 @@ public class TestOgnlRuntime assertNotNull( getter ); assertNull( setter ); } + + @Test + public void testGetCompiler() + { + OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); + OgnlExpressionCompiler compiler1 = OgnlRuntime.getCompiler( context ); + context.put( "root2", new Root() ); + OgnlExpressionCompiler compiler2 = OgnlRuntime.getCompiler( context ); + assertSame( "compilers are not the same", compiler1, compiler2 ); + } }