Author: mbenson
Date: Fri Apr  5 18:10:01 2013
New Revision: 1465080

URL: http://svn.apache.org/r1465080
Log:
handle public fields, but no others

Removed:
    
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/UsingOACL3Blueprints.java
    
commons/sandbox/weaver/branches/fields/example/src/test/java/org/apache/commons/weaver/privilizer/example/UsingOACL3BlueprintsTest.java
Modified:
    
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/UsingBlueprints.java
    
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/Utils.java
    
commons/sandbox/weaver/branches/fields/example/src/test/java/org/apache/commons/weaver/privilizer/example/UsingBlueprintsTest.java
    
commons/sandbox/weaver/branches/fields/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java

Modified: 
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/UsingBlueprints.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/UsingBlueprints.java?rev=1465080&r1=1465079&r2=1465080&view=diff
==============================================================================
--- 
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/UsingBlueprints.java
 (original)
+++ 
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/UsingBlueprints.java
 Fri Apr  5 18:10:01 2013
@@ -21,8 +21,8 @@ import org.apache.commons.weaver.privili
 @Privilizing({ @CallTo(Utils.class), @CallTo(value = Utils.More.class, methods 
= "getProperty") })
 public class UsingBlueprints {
 
-    public String utilsReadConstant() {
-        return Utils.readConstant();
+    public String utilsReadPublicConstant() {
+        return Utils.readPublicConstant();
     }
 
     public String utilsGetProperty() {
@@ -44,7 +44,4 @@ public class UsingBlueprints {
     public String moreGetTopStackElementClassName() {
         return Utils.More.getTopStackElementClassName();
     }
-
-    private void foo() {
-    }
 }

Modified: 
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/Utils.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/Utils.java?rev=1465080&r1=1465079&r2=1465080&view=diff
==============================================================================
--- 
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/Utils.java
 (original)
+++ 
commons/sandbox/weaver/branches/fields/example/src/main/java/org/apache/commons/weaver/privilizer/example/Utils.java
 Fri Apr  5 18:10:01 2013
@@ -32,10 +32,10 @@ public class Utils {
     private Utils() {
     }
 
-    private static final String foo = "foo".intern();
+    public static final String FOO = "foo".intern();
 
-    public static String readConstant() {
-        return foo;
+    public static String readPublicConstant() {
+        return FOO;
     }
 
     public static String getProperty() {

Modified: 
commons/sandbox/weaver/branches/fields/example/src/test/java/org/apache/commons/weaver/privilizer/example/UsingBlueprintsTest.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/fields/example/src/test/java/org/apache/commons/weaver/privilizer/example/UsingBlueprintsTest.java?rev=1465080&r1=1465079&r2=1465080&view=diff
==============================================================================
--- 
commons/sandbox/weaver/branches/fields/example/src/test/java/org/apache/commons/weaver/privilizer/example/UsingBlueprintsTest.java
 (original)
+++ 
commons/sandbox/weaver/branches/fields/example/src/test/java/org/apache/commons/weaver/privilizer/example/UsingBlueprintsTest.java
 Fri Apr  5 18:10:01 2013
@@ -42,8 +42,8 @@ public class UsingBlueprintsTest {
     }
 
     @Test
-    public void testUtilsReadConstant() {
-        assertEquals("foo", usingBlueprints.utilsReadConstant());
+    public void testUtilsReadPublicConstant() {
+        assertEquals(Utils.FOO, usingBlueprints.utilsReadPublicConstant());
     }
 
     @Test

Modified: 
commons/sandbox/weaver/branches/fields/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/fields/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java?rev=1465080&r1=1465079&r2=1465080&view=diff
==============================================================================
--- 
commons/sandbox/weaver/branches/fields/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java
 (original)
+++ 
commons/sandbox/weaver/branches/fields/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java
 Fri Apr  5 18:10:01 2013
@@ -302,6 +302,10 @@ public abstract class Privilizer {
                     catch (Exception e) {
                         throw new RuntimeException(e);
                     }
+                    if (copy == null) {
+                        debug("Unable to use %s as blueprint method in %s", 
Privilizer.this.toString(called), Privilizer.this.toString(method));
+                        return;
+                    }
                     Body redirect = new Body(Privilizer.this, "call %s", 
Privilizer.this.toString(called));
                     if (Privilizer.this.policy.isConditional()) {
                         redirect.startBlock("if (%s)", 
Privilizer.this.policy.condition);
@@ -392,6 +396,20 @@ public abstract class Privilizer {
                 method.instrument(redirect(blueprint, local));
             }
         }
+
+        // we have code to handle non-public fields, but the generated code 
gets VerifyErrors at runtime;
+        // for now we must skip blueprinting such methods.
+        boolean referencesPublicFieldsOnly = true;
+        for (CtField refd : referencedFields) {
+            if (!Modifier.isPublic(refd.getModifiers())) {
+               warn("Method %s references non-public field %s.%s", 
toString(method), refd.getDeclaringClass().getName(), refd.getName());
+                referencesPublicFieldsOnly = false;
+            }
+        }
+        if (!referencesPublicFieldsOnly) {
+            return null;
+        }
+
         final CtMethod result = CtNewMethod.copy(method, toName, target, null);
         result.setModifiers(Modifier.PRIVATE | Modifier.STATIC);
         target.addMethod(result);
@@ -473,20 +491,23 @@ public abstract class Privilizer {
                         throw new RuntimeException(e);
                     }
                 }
-                if (Modifier.isPublic(fld.getModifiers()) || 
!Modifier.isStatic(fld.getModifiers())) {
-                    return;
-                }
-
                 final String replacement;
                 if (f.isReader()) {
-                    replacement = String.format("$_ = ($%s) %s(%s.class, 
\"%s\", null);", primitive ? "w" : "r",
-                            assistant.fieldReader(target).getName(), 
fld.getDeclaringClass().getName(),
-                            f.getFieldName());
-                }
-                else {
-                    replacement = String.format("%s(%s.class, \"%s\", null, 
%s);", assistant.fieldWriter(target)
-                            .getName(), fld.getDeclaringClass().getName(), 
f.getFieldName(), primitive ? "($w) $1"
-                            : "$1");
+                    if (Modifier.isPublic(fld.getModifiers())) {
+                        replacement = String.format("$_ = %s.%s;", 
fld.getDeclaringClass().getName(), f.getFieldName());
+                    } else {
+                        replacement =
+                            String.format("$_ = ($%s) %s(%s.class, \"%s\", 
null);", primitive ? "w" : "r", assistant
+                                .fieldReader(target).getName(), 
fld.getDeclaringClass().getName(), f.getFieldName());
+                    }
+                } else {
+                    if (Modifier.isPublic(fld.getModifiers())) {
+                        replacement = String.format("%s.%s = $1;", 
fld.getDeclaringClass().getName(), f.getFieldName());
+                    } else {
+                        replacement =
+                            String.format("%s(%s.class, \"%s\", null, %s);", 
assistant.fieldWriter(target).getName(),
+                                fld.getDeclaringClass().getName(), 
f.getFieldName(), primitive ? "($w) $1" : "$1");
+                    }
                 }
                 debug("Replacing %s access of %s.%s", f.isReader() ? "read" : 
"write", fld.getDeclaringClass()
                         .getName(), f.getFieldName());


Reply via email to