1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -165,7 +165,7 @@ public TypedValue read(EvaluationContext context, Object target, String name) th
165
165
return new TypedValue (value , invoker .typeDescriptor .narrow (value ));
166
166
}
167
167
catch (Exception ex ) {
168
- throw new AccessException ("Unable to access property '" + name + "' through getter" , ex );
168
+ throw new AccessException ("Unable to access property '" + name + "' through getter method " , ex );
169
169
}
170
170
}
171
171
}
@@ -187,12 +187,12 @@ public TypedValue read(EvaluationContext context, Object target, String name) th
187
187
return new TypedValue (value , invoker .typeDescriptor .narrow (value ));
188
188
}
189
189
catch (Exception ex ) {
190
- throw new AccessException ("Unable to access field: " + name , ex );
190
+ throw new AccessException ("Unable to access field ' " + name + "'" , ex );
191
191
}
192
192
}
193
193
}
194
194
195
- throw new AccessException ("Neither getter nor field found for property '" + name + "'" );
195
+ throw new AccessException ("Neither getter method nor field found for property '" + name + "'" );
196
196
}
197
197
198
198
@ Override
@@ -240,7 +240,7 @@ public void write(EvaluationContext context, Object target, String name, Object
240
240
newValue , TypeDescriptor .forObject (newValue ), typeDescriptor );
241
241
}
242
242
catch (EvaluationException evaluationException ) {
243
- throw new AccessException ("Type conversion failure" ,evaluationException );
243
+ throw new AccessException ("Type conversion failure" , evaluationException );
244
244
}
245
245
}
246
246
CacheKey cacheKey = new CacheKey (type , name , target instanceof Class );
@@ -262,7 +262,7 @@ public void write(EvaluationContext context, Object target, String name, Object
262
262
return ;
263
263
}
264
264
catch (Exception ex ) {
265
- throw new AccessException ("Unable to access property '" + name + "' through setter" , ex );
265
+ throw new AccessException ("Unable to access property '" + name + "' through setter method " , ex );
266
266
}
267
267
}
268
268
}
@@ -283,12 +283,12 @@ public void write(EvaluationContext context, Object target, String name, Object
283
283
return ;
284
284
}
285
285
catch (Exception ex ) {
286
- throw new AccessException ("Unable to access field: " + name , ex );
286
+ throw new AccessException ("Unable to access field ' " + name + "'" , ex );
287
287
}
288
288
}
289
289
}
290
290
291
- throw new AccessException ("Neither setter nor field found for property '" + name + "'" );
291
+ throw new AccessException ("Neither setter method nor field found for property '" + name + "'" );
292
292
}
293
293
294
294
private TypeDescriptor getTypeDescriptor (EvaluationContext context , Object target , String name ) {
@@ -469,11 +469,11 @@ public PropertyAccessor createOptimalAccessor(EvaluationContext evalContext, Obj
469
469
InvokerPair invocationTarget = this .readerCache .get (cacheKey );
470
470
471
471
if (invocationTarget == null || invocationTarget .member instanceof Method ) {
472
- Method method = (Method ) (invocationTarget == null ? null : invocationTarget .member );
472
+ Method method = (Method ) (invocationTarget != null ? invocationTarget .member : null );
473
473
if (method == null ) {
474
474
method = findGetterForProperty (name , type , target );
475
475
if (method != null ) {
476
- invocationTarget = new InvokerPair (method ,new TypeDescriptor (new MethodParameter (method ,-1 )));
476
+ invocationTarget = new InvokerPair (method , new TypeDescriptor (new MethodParameter (method , -1 )));
477
477
ReflectionUtils .makeAccessible (method );
478
478
this .readerCache .put (cacheKey , invocationTarget );
479
479
}
@@ -497,6 +497,7 @@ public PropertyAccessor createOptimalAccessor(EvaluationContext evalContext, Obj
497
497
return new OptimalPropertyAccessor (invocationTarget );
498
498
}
499
499
}
500
+
500
501
return this ;
501
502
}
502
503
@@ -577,16 +578,8 @@ public static class OptimalPropertyAccessor implements CompilablePropertyAccesso
577
578
OptimalPropertyAccessor (InvokerPair target ) {
578
579
this .member = target .member ;
579
580
this .typeDescriptor = target .typeDescriptor ;
580
- if (this .member instanceof Field ) {
581
- Field field = (Field ) this .member ;
582
- this .needsToBeMadeAccessible = (!Modifier .isPublic (field .getModifiers ()) ||
583
- !Modifier .isPublic (field .getDeclaringClass ().getModifiers ())) && !field .isAccessible ();
584
- }
585
- else {
586
- Method method = (Method ) this .member ;
587
- this .needsToBeMadeAccessible = ((!Modifier .isPublic (method .getModifiers ()) ||
588
- !Modifier .isPublic (method .getDeclaringClass ().getModifiers ())) && !method .isAccessible ());
589
- }
581
+ this .needsToBeMadeAccessible = (!Modifier .isPublic (this .member .getModifiers ()) ||
582
+ !Modifier .isPublic (this .member .getDeclaringClass ().getModifiers ()));
590
583
}
591
584
592
585
@ Override
@@ -599,10 +592,12 @@ public boolean canRead(EvaluationContext context, Object target, String name) th
599
592
if (target == null ) {
600
593
return false ;
601
594
}
595
+
602
596
Class <?> type = (target instanceof Class ? (Class <?>) target : target .getClass ());
603
597
if (type .isArray ()) {
604
598
return false ;
605
599
}
600
+
606
601
if (this .member instanceof Method ) {
607
602
Method method = (Method ) this .member ;
608
603
String getterName = "get" + StringUtils .capitalize (name );
@@ -621,30 +616,31 @@ public boolean canRead(EvaluationContext context, Object target, String name) th
621
616
@ Override
622
617
public TypedValue read (EvaluationContext context , Object target , String name ) throws AccessException {
623
618
if (this .member instanceof Method ) {
619
+ Method method = (Method ) this .member ;
624
620
try {
625
- if (this .needsToBeMadeAccessible ) {
626
- ReflectionUtils . makeAccessible (( Method ) this . member );
621
+ if (this .needsToBeMadeAccessible && ! method . isAccessible () ) {
622
+ method . setAccessible ( true );
627
623
}
628
- Object value = (( Method ) this . member ) .invoke (target );
624
+ Object value = method .invoke (target );
629
625
return new TypedValue (value , this .typeDescriptor .narrow (value ));
630
626
}
631
627
catch (Exception ex ) {
632
- throw new AccessException ("Unable to access property '" + name + "' through getter" , ex );
628
+ throw new AccessException ("Unable to access property '" + name + "' through getter method " , ex );
633
629
}
634
630
}
635
- if (this .member instanceof Field ) {
631
+ else {
632
+ Field field = (Field ) this .member ;
636
633
try {
637
- if (this .needsToBeMadeAccessible ) {
638
- ReflectionUtils . makeAccessible (( Field ) this . member );
634
+ if (this .needsToBeMadeAccessible && ! field . isAccessible () ) {
635
+ field . setAccessible ( true );
639
636
}
640
- Object value = (( Field ) this . member ) .get (target );
637
+ Object value = field .get (target );
641
638
return new TypedValue (value , this .typeDescriptor .narrow (value ));
642
639
}
643
640
catch (Exception ex ) {
644
- throw new AccessException ("Unable to access field: " + name , ex );
641
+ throw new AccessException ("Unable to access field ' " + name + "'" , ex );
645
642
}
646
643
}
647
- throw new AccessException ("Neither getter nor field found for property '" + name + "'" );
648
644
}
649
645
650
646
@ Override
@@ -665,40 +661,43 @@ public boolean isCompilable() {
665
661
666
662
@ Override
667
663
public Class <?> getPropertyType () {
668
- if (this .member instanceof Field ) {
669
- return ((Field ) this .member ).getType ();
664
+ if (this .member instanceof Method ) {
665
+ return ((Method ) this .member ).getReturnType ();
670
666
}
671
667
else {
672
- return ((Method ) this .member ).getReturnType ();
668
+ return ((Field ) this .member ).getType ();
673
669
}
674
670
}
675
671
676
672
@ Override
677
673
public void generateCode (String propertyName , MethodVisitor mv , CodeFlow cf ) {
678
674
boolean isStatic = Modifier .isStatic (this .member .getModifiers ());
679
675
String descriptor = cf .lastDescriptor ();
680
- String memberDeclaringClassSlashedDescriptor = this .member .getDeclaringClass ().getName ().replace ('.' , '/' );
676
+ String classDesc = this .member .getDeclaringClass ().getName ().replace ('.' , '/' );
677
+
681
678
if (!isStatic ) {
682
679
if (descriptor == null ) {
683
680
cf .loadTarget (mv );
684
681
}
685
- if (descriptor == null || !memberDeclaringClassSlashedDescriptor .equals (descriptor .substring (1 ))) {
686
- mv .visitTypeInsn (CHECKCAST , memberDeclaringClassSlashedDescriptor );
682
+ if (descriptor == null || !classDesc .equals (descriptor .substring (1 ))) {
683
+ mv .visitTypeInsn (CHECKCAST , classDesc );
687
684
}
688
- } else {
685
+ }
686
+ else {
689
687
if (descriptor != null ) {
690
688
// A static field/method call will not consume what is on the stack,
691
689
// it needs to be popped off.
692
690
mv .visitInsn (POP );
693
691
}
694
692
}
695
- if (this .member instanceof Field ) {
696
- mv .visitFieldInsn (isStatic ? GETSTATIC : GETFIELD , memberDeclaringClassSlashedDescriptor ,
697
- this .member .getName (), CodeFlow .toJvmDescriptor (((Field ) this .member ).getType ()));
693
+
694
+ if (this .member instanceof Method ) {
695
+ mv .visitMethodInsn ((isStatic ? INVOKESTATIC : INVOKEVIRTUAL ), classDesc , this .member .getName (),
696
+ CodeFlow .createSignatureDescriptor ((Method ) this .member ), false );
698
697
}
699
698
else {
700
- mv .visitMethodInsn ( isStatic ? INVOKESTATIC : INVOKEVIRTUAL , memberDeclaringClassSlashedDescriptor ,
701
- this . member . getName (), CodeFlow .createSignatureDescriptor (( Method ) this .member ), false );
699
+ mv .visitFieldInsn (( isStatic ? GETSTATIC : GETFIELD ), classDesc , this . member . getName () ,
700
+ CodeFlow .toJvmDescriptor ((( Field ) this .member ). getType ()) );
702
701
}
703
702
}
704
703
}
0 commit comments