You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2013/01/12 22:23:15 UTC
svn commit: r1432518 - in /commons/sandbox/privilizer/branches/mjb:
modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/
processor/src/main/java/org/apache/commons/weaver/
processor/src/main/java/org/apache/commons/weaver/spi/ ...
Author: mbenson
Date: Sat Jan 12 21:23:15 2013
New Revision: 1432518
URL: http://svn.apache.org/viewvc?rev=1432518&view=rev
Log:
proposed API changes per email
Added:
commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/ScanResult.java (with props)
commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeaveInterest.java (with props)
commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeavePlan.java (with props)
Modified:
commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/FilesystemPrivilizer.java
commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java
commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java
commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java
commons/sandbox/privilizer/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java
Modified: commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/FilesystemPrivilizer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/FilesystemPrivilizer.java?rev=1432518&r1=1432517&r2=1432518&view=diff
==============================================================================
--- commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/FilesystemPrivilizer.java (original)
+++ commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/FilesystemPrivilizer.java Sat Jan 12 21:23:15 2013
@@ -1,25 +1,26 @@
/*
- * Copyright the original author or authors.
+ * 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
*
- * Licensed 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
*
- * 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.
+ * 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.
*/
+
package org.apache.commons.weaver.privilizer;
import java.io.File;
import java.io.IOException;
-import java.lang.reflect.Method;
-import java.util.HashSet;
-import java.util.Set;
import javassist.CannotCompileException;
import javassist.ClassPool;
@@ -29,13 +30,11 @@ import javassist.NotFoundException;
import org.apache.commons.lang3.Validate;
-
/**
* Handles weaving of methods annotated with {@link Privileged}.
*/
public class FilesystemPrivilizer extends Privilizer<FilesystemPrivilizer> {
-
private static ClassPool createClassPool(ClassLoader classpath, File target) {
final ClassPool result = new ClassPool();
try {
@@ -48,27 +47,12 @@ public class FilesystemPrivilizer extend
return result;
}
- private static Set<Class<?>> getDeclaringClasses(Iterable<Method> methods) {
- final Set<Class<?>> declaringClasses = new HashSet<Class<?>>();
- for (final Method method : methods) {
- declaringClasses.add(method.getDeclaringClass());
- }
- return declaringClasses;
- }
-
- private static Class<?> getOutermost(Class<?> type) {
- Class<?> enclosing = type.getEnclosingClass();
- return enclosing == null ? type : getOutermost(enclosing);
- }
-
private static File validTarget(File target) {
Validate.notNull(target, "target");
Validate.isTrue(target.isDirectory(), "not a directory");
return target;
}
- private final ClassLoader classpath;
-
private final File target;
private final ClassFileWriter classFileWriter = new ClassFileWriter() {
@@ -80,13 +64,11 @@ public class FilesystemPrivilizer extend
public FilesystemPrivilizer(ClassLoader classpath, File target) {
super(createClassPool(classpath, target));
- this.classpath = classpath;
this.target = target;
}
public FilesystemPrivilizer(Policy policy, ClassLoader classpath, File target) {
super(policy, createClassPool(classpath, target));
- this.classpath = classpath;
this.target = target;
}
@@ -98,8 +80,8 @@ public class FilesystemPrivilizer extend
* @throws CannotCompileException
* @throws ClassNotFoundException
*/
- public boolean weaveClass(Class<?> clazz)
- throws NotFoundException, IOException, CannotCompileException, ClassNotFoundException, IllegalAccessException {
+ public boolean weaveClass(Class<?> clazz) throws NotFoundException, IOException, CannotCompileException,
+ ClassNotFoundException, IllegalAccessException {
return weave(classPool.get(clazz.getName()));
}
Modified: commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java?rev=1432518&r1=1432517&r2=1432518&view=diff
==============================================================================
--- commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java (original)
+++ commons/sandbox/privilizer/branches/mjb/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java Sat Jan 12 21:23:15 2013
@@ -1,29 +1,44 @@
+/*
+ * 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.
+ */
+
package org.apache.commons.weaver.privilizer;
import java.io.File;
-import java.io.IOException;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
+import java.lang.annotation.ElementType;
import java.net.URLClassLoader;
-import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
-import javassist.CannotCompileException;
-import javassist.NotFoundException;
+import org.apache.commons.weaver.spi.ScanResult;
+import org.apache.commons.weaver.spi.WeaveInterest;
+import org.apache.commons.weaver.spi.WeavePlan;
import org.apache.commons.weaver.spi.Weaver;
import org.apache.commons.weaver.utils.URLArray;
/**
* Weaver which adds doPrivileged blocks for each method annotated with
- * {@link Privileged}.
- * An instance of this class will automatically get picked up by the
- * {@link org.apache.commons.weaver.WeaveProcessor} via the
+ * {@link Privileged}. An instance of this class will automatically get picked
+ * up by the {@link org.apache.commons.weaver.WeaveProcessor} via the
* {@link java.util.ServiceLoader}.
*/
-public class PrivilizerWeaver implements Weaver
-{
+public class PrivilizerWeaver implements Weaver {
public static final String CONFIG_WEAVER = "privilizer.";
public static final String CONFIG_ACCESS_LEVEL = CONFIG_WEAVER + "accessLevel";
public static final String CONFIG_POLICY = CONFIG_WEAVER + "policy";
@@ -36,10 +51,8 @@ public class PrivilizerWeaver implements
private AccessLevel targetAccessLevel;
-
@Override
- public void configure(List<String> classPath, File target, Properties config)
- {
+ public void configure(List<String> classPath, File target, Properties config) {
LOG.info("");
String accessLevel = config.getProperty(CONFIG_ACCESS_LEVEL);
@@ -61,8 +74,7 @@ public class PrivilizerWeaver implements
}
@Override
- protected AccessLevel getTargetAccessLevel()
- {
+ protected AccessLevel getTargetAccessLevel() {
return targetAccessLevel;
}
};
@@ -70,60 +82,27 @@ public class PrivilizerWeaver implements
}
@Override
- public List<Class<? extends Annotation>> getInterest()
- {
- List<Class<? extends Annotation>> interest = new ArrayList<Class<? extends Annotation>>();
- interest.add(Privileged.class);
- return interest;
+ public WeavePlan getWeavePlan() {
+ return new WeavePlan().add(WeaveInterest.of(Privileged.class, ElementType.METHOD));
}
@Override
- public void preWeave()
- {
+ public void preWeave() {
// nothing to do
}
@Override
- public boolean weave(Class classToWeave, Class<? extends Annotation> processingAnnotation)
- {
- // Privilizer does not weave classes
- return false;
- }
-
- @Override
- public boolean weave(Method methodToWeave, Class<? extends Annotation> processingAnnotation)
- {
- try
- {
- privilizer.weaveClass(methodToWeave.getDeclaringClass());
- }
- catch (NotFoundException e)
- {
- throw new RuntimeException(e);
+ public boolean weave(ScanResult scanResult) {
+ try {
+ return privilizer.weaveClass(scanResult.getType());
+ } catch (Exception e) {
+ throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
- catch (IOException e)
- {
- throw new RuntimeException(e);
- }
- catch (CannotCompileException e)
- {
- throw new RuntimeException(e);
- }
- catch (ClassNotFoundException e)
- {
- throw new RuntimeException(e);
- }
- catch (IllegalAccessException e)
- {
- throw new RuntimeException(e);
- }
-
- return true;
}
@Override
- public void postWeave()
- {
+ public void postWeave() {
// nothing to do
}
+
}
Modified: commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java?rev=1432518&r1=1432517&r2=1432518&view=diff
==============================================================================
--- commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java (original)
+++ commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java Sat Jan 12 21:23:15 2013
@@ -19,15 +19,21 @@
package org.apache.commons.weaver;
import java.io.File;
-import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
+import org.apache.commons.weaver.spi.ScanResult;
+import org.apache.commons.weaver.spi.WeaveInterest;
+import org.apache.commons.weaver.spi.WeavePlan;
import org.apache.commons.weaver.spi.Weaver;
import org.apache.commons.weaver.utils.URLArray;
import org.apache.xbean.finder.AnnotationFinder;
@@ -41,19 +47,19 @@ public class WeaveProcessor {
private static WeaveProcessor instance;
/**
- * The classpath which will be used to look up cross references during weaving.
+ * The classpath which will be used to look up cross references during
+ * weaving.
*/
private List<String> classPath;
/**
- * The actual path which gets weaved. All the classes in this path
- * will get weaved. The weaved classes will replace the original classes.
+ * The actual path which gets weaved. All the classes in this path will get
+ * weaved. The weaved classes will replace the original classes.
*/
private File target;
/** List of picked up weaver plugins */
- private List<Weaver> weavers = new ArrayList<Weaver>();
-
+ private final List<Weaver> weavers = new ArrayList<Weaver>();
public static synchronized WeaveProcessor getInstance() {
if (instance == null) {
@@ -75,10 +81,14 @@ public class WeaveProcessor {
/**
* Configure all Weavers.
- * @param classPath the classpath to look up cross-references in during weaving
- * @param target the File path where the classes to weave reside
- * @param config additional configuration for all plugins.
- *
+ *
+ * @param classPath
+ * the classpath to look up cross-references in during weaving
+ * @param target
+ * the File path where the classes to weave reside
+ * @param config
+ * additional configuration for all plugins.
+ *
*/
public void configure(List<String> classPath, File target, Properties config) {
this.classPath = classPath;
@@ -107,22 +117,56 @@ public class WeaveProcessor {
}
private void weave(Weaver weaver) {
- List<Class<? extends Annotation>> interest = weaver.getInterest();
+ final WeavePlan weavePlan = weaver.getWeavePlan();
+
+ Map<Class<?>, ScanResult> results = new LinkedHashMap<Class<?>, ScanResult>();
ClassLoader classLoader = new URLClassLoader(URLArray.fromPaths(classPath));
AnnotationFinder annotationFinder = new AnnotationFinder(new FileArchive(classLoader, target), false);
- for (Class<? extends Annotation> annotation : interest) {
- List<Class<?>> annotatedClasses = annotationFinder.findAnnotatedClasses(annotation);
-
- for (Class<?> annotatedClass : annotatedClasses) {
- weaver.weave(annotatedClass, annotation);
- }
-
- List<Method> annotateMethods = annotationFinder.findAnnotatedMethods(annotation);
- for (Method annotatedMethod : annotateMethods) {
- weaver.weave(annotatedMethod, annotation);
+ for (WeaveInterest interest : weavePlan.getInterests()) {
+ switch (interest.target) {
+ case TYPE:
+ for (Class<?> type : annotationFinder.findAnnotatedClasses(interest.annotationType)) {
+ getScanResult(results, type).add(
+ new ScanResult.Item<Class<?>>(type, type.getAnnotation(interest.annotationType)));
+ }
+ break;
+ case METHOD:
+ for (Method method : annotationFinder.findAnnotatedMethods(interest.annotationType)) {
+ getScanResult(results, method.getDeclaringClass()).add(
+ new ScanResult.Item<Method>(method, method.getAnnotation(interest.annotationType)));
+ }
+ break;
+ case CONSTRUCTOR:
+ for (Constructor<?> cs : annotationFinder.findAnnotatedConstructors(interest.annotationType)) {
+ getScanResult(results, cs.getDeclaringClass()).add(
+ new ScanResult.Item<Constructor<?>>(cs, cs.getAnnotation(interest.annotationType)));
+ }
+ break;
+ case FIELD:
+ for (Field fld : annotationFinder.findAnnotatedFields(interest.annotationType)) {
+ getScanResult(results, fld.getDeclaringClass()).add(
+ new ScanResult.Item<Field>(fld, fld.getAnnotation(interest.annotationType)));
+ }
+ break;
+ default:
+ // should we log something?
+ break;
}
}
+ for (ScanResult result : results.values()) {
+ weaver.weave(result);
+ }
+ }
+
+ private static ScanResult getScanResult(Map<Class<?>, ScanResult> results, Class<?> forType) {
+ if (results.containsKey(forType)) {
+ return results.get(forType);
+ }
+ final ScanResult result = new ScanResult(forType);
+ results.put(forType, result);
+ return result;
}
+
}
Added: commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/ScanResult.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/ScanResult.java?rev=1432518&view=auto
==============================================================================
--- commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/ScanResult.java (added)
+++ commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/ScanResult.java Sat Jan 12 21:23:15 2013
@@ -0,0 +1,124 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.weaver.spi;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Encapsulates the result of scanning based on a {@link WeavePlan}.
+ */
+public class ScanResult {
+ public static class Item<E extends AnnotatedElement> {
+ public final E element;
+ public final Annotation annotation;
+
+ public Item(E element, Annotation annotation) {
+ super();
+ this.element = element;
+ this.annotation = annotation;
+ }
+ }
+
+ private final Class<?> type;
+ private final List<Item<?>> items = new ArrayList<ScanResult.Item<?>>();
+
+ public ScanResult(Class<?> type) {
+ super();
+ this.type = type;
+ }
+
+ public ScanResult add(Item<?> item) {
+ items.add(item);
+ return this;
+ }
+
+ public Class<?> getType() {
+ return type;
+ }
+
+ /**
+ * Get scan result Items.
+ *
+ * @return Iterable<Item>
+ */
+ public Iterable<Item<?>> getItems() {
+ return Collections.unmodifiableList(items);
+ }
+
+ /**
+ * Get typed scan result Items.
+ *
+ * @param targetType
+ * extends AnnotatedElement for easy casting to the appropriate
+ * annotated object types.
+ * @return Iterable<Item>
+ */
+ public final <E extends AnnotatedElement> Iterable<Item<E>> getItems(final Class<E> targetType) {
+ if (targetType == null) {
+ throw new NullPointerException();
+ }
+ return new Iterable<ScanResult.Item<E>>() {
+
+ @Override
+ public Iterator<Item<E>> iterator() {
+ final Iterator<Item<?>> wrapped = getItems().iterator();
+ return new Iterator<ScanResult.Item<E>>() {
+ private Item<E> next = read();
+
+ private Item<E> read() {
+ while (wrapped.hasNext()) {
+ Item<?> test = wrapped.next();
+ if (targetType.isInstance(test.element)) {
+ @SuppressWarnings("unchecked")
+ final Item<E> result = (Item<E>) test;
+ return result;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return next != null;
+ }
+
+ @Override
+ public Item<E> next() {
+ try {
+ return next;
+ } finally {
+ next = read();
+ }
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ };
+ }
+}
Propchange: commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/ScanResult.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeaveInterest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeaveInterest.java?rev=1432518&view=auto
==============================================================================
--- commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeaveInterest.java (added)
+++ commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeaveInterest.java Sat Jan 12 21:23:15 2013
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.weaver.spi;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+
+/**
+ * Weave interest composed of annotation type and target element type.
+ */
+public class WeaveInterest {
+ public final Class<? extends Annotation> annotationType;
+ public final ElementType target;
+
+ private WeaveInterest(Class<? extends Annotation> annotationType, ElementType target) {
+ super();
+ this.annotationType = annotationType;
+ this.target = target;
+ }
+
+ public static WeaveInterest of(Class<? extends Annotation> annotationType, ElementType target) {
+ return new WeaveInterest(annotationType, target);
+ }
+}
Propchange: commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeaveInterest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeavePlan.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeavePlan.java?rev=1432518&view=auto
==============================================================================
--- commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeavePlan.java (added)
+++ commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeavePlan.java Sat Jan 12 21:23:15 2013
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.weaver.spi;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Weave plan, extensible in future to include e.g. scanning strategy hints.
+ */
+public class WeavePlan {
+
+ private final List<WeaveInterest> interests = new ArrayList<WeaveInterest>();
+
+ public WeavePlan add(WeaveInterest interest) {
+ if (interest == null) {
+ throw new NullPointerException();
+ }
+ interests.add(interest);
+ return this;
+ }
+
+ public Iterable<WeaveInterest> getInterests() {
+ return Collections.unmodifiableList(interests);
+ }
+}
Propchange: commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/WeavePlan.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java?rev=1432518&r1=1432517&r2=1432518&view=diff
==============================================================================
--- commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java (original)
+++ commons/sandbox/privilizer/branches/mjb/processor/src/main/java/org/apache/commons/weaver/spi/Weaver.java Sat Jan 12 21:23:15 2013
@@ -19,34 +19,33 @@
package org.apache.commons.weaver.spi;
import java.io.File;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
import java.util.List;
import java.util.Properties;
/**
- * An implementation of a 'Weaver' takes care about
- * certain weaving jobs and will perform the byte code
- * enhancement in the classes.
- *
- * TODO: we might enhance this SPI to gather upfront information about what needs to get scanned at all!
+ * An implementation of a 'Weaver' takes care about certain weaving jobs and
+ * will perform the byte code enhancement in the classes.
*/
-public interface Weaver
-{
+public interface Weaver {
/**
- * This is for now a simple way to configure any weaver.
- * Any configuration property of a weaver should start with it's 'name'
- * e.g. 'privilizer'
- * @param classPath the classpath to look up cross-references in during weaving
- * @param target the File path where the classes to weave reside
- * @param config additional configuration for all plugins.
+ * This is for now a simple way to configure any weaver. Any configuration
+ * property of a weaver should start with it's 'name' e.g. 'privilizer'
+ *
+ * @param classPath
+ * the classpath to look up cross-references in during weaving
+ * @param target
+ * the File path where the classes to weave reside
+ * @param config
+ * additional configuration for all plugins.
*/
void configure(List<String> classPath, File target, Properties config);
/**
- * A Weaver must return a List of Annotations he is interested in.
+ * Get the "plan" of this {@code Weaver} instance.
+ *
+ * @return {@link WeavePlan}
*/
- List<Class<? extends Annotation>> getInterest();
+ WeavePlan getWeavePlan();
/**
* This will get invoked before any weaver did run
@@ -54,25 +53,19 @@ public interface Weaver
void preWeave();
/**
- * Perform weaving on the given class for any class which has one of the required annotations.
- * If there is nothing to do, then just go on.
- *
+ * Perform weaving on the given class for any class which has one of the
+ * required annotations. If there is nothing to do, then just go on.
+ *
+ * @param scanResult
+ *
* @return <code>true</code> if some bytecode has been changed
*/
- boolean weave(Class classToWeave, Class<? extends Annotation> processingAnnotation);
-
- /**
- * Perform weaving on the given class for any class which has one of the required annotations.
- * If there is nothing to do, then just go on.
- *
- * @return <code>true</code> if some bytecode has been changed
- */
- boolean weave(Method methodToWeave, Class<? extends Annotation> processingAnnotation);
+ boolean weave(ScanResult scanResult);
/**
* This method will get invoked after all {@link #weave(Class, Class)} and
- * {@link #weave(java.lang.reflect.Method, Class)} methods got invoked
- * for all classes on every weaver.
+ * {@link #weave(java.lang.reflect.Method, Class)} methods got invoked for
+ * all classes on every weaver.
*/
void postWeave();
}
Modified: commons/sandbox/privilizer/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java?rev=1432518&r1=1432517&r2=1432518&view=diff
==============================================================================
--- commons/sandbox/privilizer/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java (original)
+++ commons/sandbox/privilizer/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java Sat Jan 12 21:23:15 2013
@@ -19,31 +19,29 @@
package org.apache.commons.weaver.test.weaver;
import java.io.File;
-import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import java.util.Properties;
-import java.util.logging.Logger;
+import org.apache.commons.weaver.spi.ScanResult;
+import org.apache.commons.weaver.spi.WeaveInterest;
+import org.apache.commons.weaver.spi.WeavePlan;
+import org.apache.commons.weaver.spi.Weaver;
import org.apache.commons.weaver.test.beans.TestAnnotation;
import org.junit.Assert;
-import org.apache.commons.weaver.spi.Weaver;
-
/**
*/
-public class TestWeaver implements Weaver
-{
+public class TestWeaver implements Weaver {
public static boolean preWeaveExecuted = false;
public static boolean postWeaveExecuted = false;
public static List<Method> wovenMethods = new ArrayList<Method>();
- public static List<Class> wovenClasses = new ArrayList<Class>();
+ public static List<Class<?>> wovenClasses = new ArrayList<Class<?>>();
@Override
- public void configure(List<String> classPath, File target, Properties config)
- {
+ public void configure(List<String> classPath, File target, Properties config) {
Assert.assertNotNull(config);
Assert.assertEquals(1, config.size());
@@ -52,34 +50,32 @@ public class TestWeaver implements Weave
}
@Override
- public List<Class<? extends Annotation>> getInterest()
- {
- List<Class<? extends Annotation>> interests = new ArrayList<Class<? extends Annotation>>();
- interests.add(TestAnnotation.class);
- return interests;
+ public WeavePlan getWeavePlan() {
+ return new WeavePlan().add(WeaveInterest.of(TestAnnotation.class, ElementType.TYPE)).add(
+ WeaveInterest.of(TestAnnotation.class, ElementType.METHOD));
}
@Override
- public void preWeave()
- {
+ public void preWeave() {
preWeaveExecuted = true;
}
@Override
- public boolean weave(Class classToWeave, Class<? extends Annotation> processingAnnotation)
- {
- return wovenClasses.add(classToWeave);
+ public boolean weave(ScanResult scanResult) {
+ boolean result = false;
+ for (@SuppressWarnings("rawtypes")
+ ScanResult.Item<Class> item : scanResult.getItems(Class.class)) {
+ result |= wovenClasses.add(item.element);
+ }
+ for (ScanResult.Item<Method> item : scanResult.getItems(Method.class)) {
+ result |= wovenMethods.add(item.element);
+ }
+ return result;
}
@Override
- public boolean weave(Method methodToWeave, Class<? extends Annotation> processingAnnotation)
- {
- return wovenMethods.add(methodToWeave);
- }
-
- @Override
- public void postWeave()
- {
+ public void postWeave() {
postWeaveExecuted = true;
}
+
}