Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0268110883 for ; Wed, 12 Feb 2014 22:40:58 +0000 (UTC) Received: (qmail 9513 invoked by uid 500); 12 Feb 2014 22:40:47 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 9324 invoked by uid 500); 12 Feb 2014 22:40:40 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 9189 invoked by uid 99); 12 Feb 2014 22:40:34 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Feb 2014 22:40:34 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Feb 2014 22:40:30 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 0047723889FA; Wed, 12 Feb 2014 22:40:10 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1567781 [4/5] - in /commons/proper/weaver/trunk: ant/lib/src/main/java/org/apache/commons/weaver/ant/ dist/ dist/src/assembly/ example/src/main/java/org/apache/commons/weaver/privilizer/example/ example/src/test/java/org/apache/commons/wea... Date: Wed, 12 Feb 2014 22:40:08 -0000 To: commits@commons.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140212224010.0047723889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilized.java URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilized.java?rev=1567781&r1=1567780&r2=1567781&view=diff ============================================================================== --- commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilized.java (original) +++ commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilized.java Wed Feb 12 22:40:06 2014 @@ -1,33 +1,33 @@ -/* - * 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.lang.annotation.ElementType; -import java.lang.annotation.Target; - -/** - * Class-retention annotation to mark privilized classes. - */ -@Target(ElementType.TYPE) -public @interface Privilized { - /** - * Name of {@link Policy} with which privilized weaving was performed. - */ - String value(); +/* + * 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.lang.annotation.ElementType; +import java.lang.annotation.Target; + +/** + * Class-retention annotation to mark privilized classes. + */ +@Target(ElementType.TYPE) +public @interface Privilized { + /** + * Name of {@link Policy} with which privilized weaving was performed. + */ + String value(); } \ No newline at end of file Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilized.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilized.java ('svn:executable' removed) Modified: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java?rev=1567781&r1=1567780&r2=1567781&view=diff ============================================================================== --- commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java (original) +++ commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java Wed Feb 12 22:40:06 2014 @@ -1,247 +1,247 @@ -/* - * 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.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.HashSet; -import java.util.Set; - -import javax.activation.DataSource; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.Validate; -import org.apache.commons.weaver.model.WeaveEnvironment; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassVisitor; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; -import org.objectweb.asm.util.CheckClassAdapter; -import org.objectweb.asm.util.TraceClassVisitor; - -/** - * Coordinates privilization activities. - */ -public class Privilizer { - /** - * An ASM {@link ClassVisitor} for privilization. - */ - abstract class PrivilizerClassVisitor extends ClassVisitor { - String className; - Type target; - - protected PrivilizerClassVisitor() { - this(null); - } - - protected PrivilizerClassVisitor(ClassVisitor cv) { - super(Opcodes.ASM4, cv); - } - - protected Privilizer privilizer() { - return Privilizer.this; - } - - @Override - public void visit(int version, int access, String name, String signature, String superName, - String[] interfaces) { - super.visit(version, access, name, signature, superName, interfaces); - className = name; - target = Type.getObjectType(name); - } - } - - /** - * Convenient {@link ClassVisitor} layer to write classfiles into the {@link WeaveEnvironment}. - */ - class WriteClass extends PrivilizerClassVisitor { - WriteClass(ClassWriter cw) { - super(cw); - } - - @Override - public void visitEnd() { - super.visitEnd(); - final byte[] bytecode = ((ClassWriter) cv).toByteArray(); - - if (verify) { - verify(className, bytecode); - } - - final DataSource classfile = env.getClassfile(className); - env.debug("Writing class %s to resource %s", className, classfile.getName()); - OutputStream outputStream = null; - try { - outputStream = classfile.getOutputStream(); - IOUtils.write(bytecode, outputStream); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - IOUtils.closeQuietly(outputStream); - } - } - } - - /** - * Privilizer weaver configuration prefix. - */ - public static final String CONFIG_WEAVER = "privilizer."; - - /** - * {@link AccessLevel} configuration key. - * @see AccessLevel#parse(String) - */ - public static final String CONFIG_ACCESS_LEVEL = CONFIG_WEAVER + "accessLevel"; - - /** - * Weave {@link Policy} configuration key. - * @see Policy#parse(String) - */ - public static final String CONFIG_POLICY = CONFIG_WEAVER + "policy"; - - /** - * Verification configuration key. - * @see BooleanUtils#toBoolean(String) - */ - public static final String CONFIG_VERIFY = CONFIG_WEAVER + "verify"; - - private static final String GENERATE_NAME = "__privileged_%s"; - - static final Type[] EMPTY_TYPE_ARRAY = new Type[0]; - - final WeaveEnvironment env; - final AccessLevel accessLevel; - final Policy policy; - final boolean verify; - - /** - * Create a new {@link Privilizer}. - * @param env to use - */ - public Privilizer(WeaveEnvironment env) { - super(); - this.env = env; - this.policy = Policy.parse(env.config.getProperty(CONFIG_POLICY)); - this.accessLevel = AccessLevel.parse(env.config.getProperty(CONFIG_ACCESS_LEVEL)); - verify = BooleanUtils.toBoolean(env.config.getProperty(CONFIG_VERIFY)); - } - - String generateName(String simple) { - return String.format(GENERATE_NAME, simple); - } - - Type wrap(Type t) { - switch (t.getSort()) { - case Type.BOOLEAN: - return Type.getType(Boolean.class); - case Type.BYTE: - return Type.getType(Byte.class); - case Type.SHORT: - return Type.getType(Short.class); - case Type.INT: - return Type.getType(Integer.class); - case Type.CHAR: - return Type.getType(Character.class); - case Type.LONG: - return Type.getType(Long.class); - case Type.FLOAT: - return Type.getType(Float.class); - case Type.DOUBLE: - return Type.getType(Double.class); - case Type.VOID: - return Type.getType(Void.class); - default: - return t; - } - } - - void blueprint(final Class type, final Privilizing privilizing) { - Object[] args = { type.getName(), privilizing }; - env.debug("blueprinting class %s %s", args); - InputStream bytecode = null; - try { - bytecode = env.getClassfile(type).getInputStream(); - final ClassReader classReader = new ClassReader(bytecode); - - ClassVisitor cv; - cv = new WriteClass(new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS)); - cv = new PrivilizingVisitor(this, cv); - cv = new BlueprintingVisitor(this, cv, privilizing); - - classReader.accept(cv, ClassReader.EXPAND_FRAMES); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - IOUtils.closeQuietly(bytecode); - } - } - - void privilize(final Class type) { - Object[] args = { type.getName() }; - env.debug("privilizing class %s", args); - InputStream bytecode = null; - try { - bytecode = env.getClassfile(type).getInputStream(); - final ClassReader classReader = new ClassReader(bytecode); - ClassVisitor cv; - cv = new WriteClass(new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS)); - cv = new PrivilizingVisitor(this, cv); - - classReader.accept(cv, ClassReader.EXPAND_FRAMES); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - IOUtils.closeQuietly(bytecode); - } - } - - void verify(final String className, final byte[] bytecode) { - final ClassReader reader = new ClassReader(bytecode); - - env.debug("Verifying bytecode for class %s", className); - final StringWriter w = new StringWriter(); - CheckClassAdapter.verify(reader, env.classLoader, false, new PrintWriter(w)); - final String error = w.toString(); - if (!error.isEmpty()) { - env.error(error); - final StringWriter trace = new StringWriter(); - reader.accept(new TraceClassVisitor(new PrintWriter(trace)), ClassReader.SKIP_DEBUG); - env.debug(trace.toString()); - throw new IllegalStateException(); - } - Validate.validState(StringUtils.isBlank(error), error); - - final ClassVisitor checkInnerClasses = new ClassVisitor(Opcodes.ASM4, null) { - final Set innerNames = new HashSet(); - - @Override - public void visitInnerClass(String name, String outerName, String innerName, int access) { - super.visitInnerClass(name, outerName, innerName, access); - Validate.validState(innerNames.add(innerName), "%s already defined", innerName); - } - }; - reader.accept(checkInnerClasses, ClassReader.SKIP_CODE); - } -} +/* + * 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.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.HashSet; +import java.util.Set; + +import javax.activation.DataSource; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; +import org.apache.commons.weaver.model.WeaveEnvironment; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.util.CheckClassAdapter; +import org.objectweb.asm.util.TraceClassVisitor; + +/** + * Coordinates privilization activities. + */ +public class Privilizer { + /** + * An ASM {@link ClassVisitor} for privilization. + */ + abstract class PrivilizerClassVisitor extends ClassVisitor { + String className; + Type target; + + protected PrivilizerClassVisitor() { + this(null); + } + + protected PrivilizerClassVisitor(ClassVisitor cv) { + super(Opcodes.ASM4, cv); + } + + protected Privilizer privilizer() { + return Privilizer.this; + } + + @Override + public void visit(int version, int access, String name, String signature, String superName, + String[] interfaces) { + super.visit(version, access, name, signature, superName, interfaces); + className = name; + target = Type.getObjectType(name); + } + } + + /** + * Convenient {@link ClassVisitor} layer to write classfiles into the {@link WeaveEnvironment}. + */ + class WriteClass extends PrivilizerClassVisitor { + WriteClass(ClassWriter cw) { + super(cw); + } + + @Override + public void visitEnd() { + super.visitEnd(); + final byte[] bytecode = ((ClassWriter) cv).toByteArray(); + + if (verify) { + verify(className, bytecode); + } + + final DataSource classfile = env.getClassfile(className); + env.debug("Writing class %s to resource %s", className, classfile.getName()); + OutputStream outputStream = null; + try { + outputStream = classfile.getOutputStream(); + IOUtils.write(bytecode, outputStream); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(outputStream); + } + } + } + + /** + * Privilizer weaver configuration prefix. + */ + public static final String CONFIG_WEAVER = "privilizer."; + + /** + * {@link AccessLevel} configuration key. + * @see AccessLevel#parse(String) + */ + public static final String CONFIG_ACCESS_LEVEL = CONFIG_WEAVER + "accessLevel"; + + /** + * Weave {@link Policy} configuration key. + * @see Policy#parse(String) + */ + public static final String CONFIG_POLICY = CONFIG_WEAVER + "policy"; + + /** + * Verification configuration key. + * @see BooleanUtils#toBoolean(String) + */ + public static final String CONFIG_VERIFY = CONFIG_WEAVER + "verify"; + + private static final String GENERATE_NAME = "__privileged_%s"; + + static final Type[] EMPTY_TYPE_ARRAY = new Type[0]; + + final WeaveEnvironment env; + final AccessLevel accessLevel; + final Policy policy; + final boolean verify; + + /** + * Create a new {@link Privilizer}. + * @param env to use + */ + public Privilizer(WeaveEnvironment env) { + super(); + this.env = env; + this.policy = Policy.parse(env.config.getProperty(CONFIG_POLICY)); + this.accessLevel = AccessLevel.parse(env.config.getProperty(CONFIG_ACCESS_LEVEL)); + verify = BooleanUtils.toBoolean(env.config.getProperty(CONFIG_VERIFY)); + } + + String generateName(String simple) { + return String.format(GENERATE_NAME, simple); + } + + Type wrap(Type t) { + switch (t.getSort()) { + case Type.BOOLEAN: + return Type.getType(Boolean.class); + case Type.BYTE: + return Type.getType(Byte.class); + case Type.SHORT: + return Type.getType(Short.class); + case Type.INT: + return Type.getType(Integer.class); + case Type.CHAR: + return Type.getType(Character.class); + case Type.LONG: + return Type.getType(Long.class); + case Type.FLOAT: + return Type.getType(Float.class); + case Type.DOUBLE: + return Type.getType(Double.class); + case Type.VOID: + return Type.getType(Void.class); + default: + return t; + } + } + + void blueprint(final Class type, final Privilizing privilizing) { + Object[] args = { type.getName(), privilizing }; + env.debug("blueprinting class %s %s", args); + InputStream bytecode = null; + try { + bytecode = env.getClassfile(type).getInputStream(); + final ClassReader classReader = new ClassReader(bytecode); + + ClassVisitor cv; + cv = new WriteClass(new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS)); + cv = new PrivilizingVisitor(this, cv); + cv = new BlueprintingVisitor(this, cv, privilizing); + + classReader.accept(cv, ClassReader.EXPAND_FRAMES); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(bytecode); + } + } + + void privilize(final Class type) { + Object[] args = { type.getName() }; + env.debug("privilizing class %s", args); + InputStream bytecode = null; + try { + bytecode = env.getClassfile(type).getInputStream(); + final ClassReader classReader = new ClassReader(bytecode); + ClassVisitor cv; + cv = new WriteClass(new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS)); + cv = new PrivilizingVisitor(this, cv); + + classReader.accept(cv, ClassReader.EXPAND_FRAMES); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(bytecode); + } + } + + void verify(final String className, final byte[] bytecode) { + final ClassReader reader = new ClassReader(bytecode); + + env.debug("Verifying bytecode for class %s", className); + final StringWriter w = new StringWriter(); + CheckClassAdapter.verify(reader, env.classLoader, false, new PrintWriter(w)); + final String error = w.toString(); + if (!error.isEmpty()) { + env.error(error); + final StringWriter trace = new StringWriter(); + reader.accept(new TraceClassVisitor(new PrintWriter(trace)), ClassReader.SKIP_DEBUG); + env.debug(trace.toString()); + throw new IllegalStateException(); + } + Validate.validState(StringUtils.isBlank(error), error); + + final ClassVisitor checkInnerClasses = new ClassVisitor(Opcodes.ASM4, null) { + final Set innerNames = new HashSet(); + + @Override + public void visitInnerClass(String name, String outerName, String innerName, int access) { + super.visitInnerClass(name, outerName, innerName, access); + Validate.validState(innerNames.add(innerName), "%s already defined", innerName); + } + }; + reader.accept(checkInnerClasses, ClassReader.SKIP_CODE); + } +} Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java?rev=1567781&r1=1567780&r2=1567781&view=diff ============================================================================== --- commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java (original) +++ commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java Wed Feb 12 22:40:06 2014 @@ -1,81 +1,81 @@ -/* - * 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.lang.annotation.ElementType; -import java.util.LinkedHashSet; -import java.util.Set; - -import org.apache.commons.lang3.Validate; -import org.apache.commons.weaver.model.ScanRequest; -import org.apache.commons.weaver.model.Scanner; -import org.apache.commons.weaver.model.WeavableClass; -import org.apache.commons.weaver.model.WeaveEnvironment; -import org.apache.commons.weaver.model.WeaveInterest; -import org.apache.commons.weaver.spi.Weaver; - -/** - * Privilizer {@link Weaver} implementation. - */ -public class PrivilizerWeaver implements Weaver { - @Override - public boolean process(WeaveEnvironment weaveEnvironment, Scanner scanner) { - final Privilizer privilizer = new Privilizer(weaveEnvironment); - - final Set> privilizedTypes = new LinkedHashSet>(); - - // handle blueprints: - for (WeavableClass type : scanner.scan( - new ScanRequest().add(WeaveInterest.of(Privilizing.class, ElementType.TYPE))).getClasses()) { - - final Class t = type.getTarget(); - if (privilizedTypes.add(t) && validateRequest(privilizer, type)) { - privilizer.blueprint(t, type.getAnnotation(Privilizing.class)); - } - } - - // handle remaining classes declaring @Privileged methods: - - for (WeavableClass type : scanner.scan( - new ScanRequest().add(WeaveInterest.of(Privileged.class, ElementType.METHOD))).getClasses()) { - final Class t = type.getTarget(); - if (privilizedTypes.add(t) && validateRequest(privilizer, type)) { - privilizer.privilize(t); - } - } - return !privilizedTypes.isEmpty(); - } - - /** - * Validate a weaving request for a given target type. - * @param privilizer whose configuration to consult - * @param type target - * @return whether weaving should proceed - * @throws IllegalStateException if class has already been woven with some other policy - */ - private boolean validateRequest(Privilizer privilizer, WeavableClass type) { - Privilized marker = type.getAnnotation(Privilized.class); - if (marker == null) { - return privilizer.policy != Policy.NEVER; - } - Validate.validState(privilizer.policy.name().equals(marker.value()), "%s already privilized with policy %s", - type.getTarget().getName(), marker.value()); - return false; - } -} +/* + * 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.lang.annotation.ElementType; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.apache.commons.lang3.Validate; +import org.apache.commons.weaver.model.ScanRequest; +import org.apache.commons.weaver.model.Scanner; +import org.apache.commons.weaver.model.WeavableClass; +import org.apache.commons.weaver.model.WeaveEnvironment; +import org.apache.commons.weaver.model.WeaveInterest; +import org.apache.commons.weaver.spi.Weaver; + +/** + * Privilizer {@link Weaver} implementation. + */ +public class PrivilizerWeaver implements Weaver { + @Override + public boolean process(WeaveEnvironment weaveEnvironment, Scanner scanner) { + final Privilizer privilizer = new Privilizer(weaveEnvironment); + + final Set> privilizedTypes = new LinkedHashSet>(); + + // handle blueprints: + for (WeavableClass type : scanner.scan( + new ScanRequest().add(WeaveInterest.of(Privilizing.class, ElementType.TYPE))).getClasses()) { + + final Class t = type.getTarget(); + if (privilizedTypes.add(t) && validateRequest(privilizer, type)) { + privilizer.blueprint(t, type.getAnnotation(Privilizing.class)); + } + } + + // handle remaining classes declaring @Privileged methods: + + for (WeavableClass type : scanner.scan( + new ScanRequest().add(WeaveInterest.of(Privileged.class, ElementType.METHOD))).getClasses()) { + final Class t = type.getTarget(); + if (privilizedTypes.add(t) && validateRequest(privilizer, type)) { + privilizer.privilize(t); + } + } + return !privilizedTypes.isEmpty(); + } + + /** + * Validate a weaving request for a given target type. + * @param privilizer whose configuration to consult + * @param type target + * @return whether weaving should proceed + * @throws IllegalStateException if class has already been woven with some other policy + */ + private boolean validateRequest(Privilizer privilizer, WeavableClass type) { + Privilized marker = type.getAnnotation(Privilized.class); + if (marker == null) { + return privilizer.policy != Policy.NEVER; + } + Validate.validState(privilizer.policy.name().equals(marker.value()), "%s already privilized with policy %s", + type.getTarget().getName(), marker.value()); + return false; + } +} Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerWeaver.java ('svn:executable' removed) Modified: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java?rev=1567781&r1=1567780&r2=1567781&view=diff ============================================================================== --- commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java (original) +++ commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java Wed Feb 12 22:40:06 2014 @@ -1,239 +1,239 @@ -/* - * 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.lang.reflect.Modifier; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; -import java.util.LinkedHashMap; -import java.util.Map; - -import org.apache.commons.lang3.ArrayUtils; -import org.objectweb.asm.AnnotationVisitor; -import org.objectweb.asm.ClassVisitor; -import org.objectweb.asm.FieldVisitor; -import org.objectweb.asm.Label; -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; -import org.objectweb.asm.commons.GeneratorAdapter; -import org.objectweb.asm.commons.Method; -import org.objectweb.asm.commons.StaticInitMerger; - -/** - * ASM {@link ClassVisitor} to privilize {@link Privileged} methods. - */ -class PrivilizingVisitor extends Privilizer.PrivilizerClassVisitor { - final Map privilegedMethods = new LinkedHashMap(); - boolean annotated; - final Policy policy; - final AccessLevel accessLevel; - - /** - * Create a new {@link PrivilizingVisitor}. - * @param privilizer owner - * @param cv next - */ - PrivilizingVisitor(Privilizer privilizer, ClassVisitor cv) { - privilizer.super(); - this.policy = privilizer.policy; - this.accessLevel = privilizer.accessLevel; - this.cv = - new InlineNestedPrivilegedCalls(privilizer, privilegedMethods, new StaticInitMerger( - privilizer.generateName("clinit"), cv)); - } - - private void annotate() { - if (!annotated) { - annotated = true; - final AnnotationVisitor privilizedVisitor = - super.visitAnnotation(Type.getType(Privilized.class).getDescriptor(), false); - privilizedVisitor.visit("value", policy.name()); - privilizedVisitor.visitEnd(); - } - } - - @Override - public void visitInnerClass(String name, String outerName, String innerName, int access) { - annotate(); - super.visitInnerClass(name, outerName, innerName, access); - } - - @Override - public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { - annotate(); - return super.visitField(access, name, desc, signature, value); - } - - @Override - public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, - final String[] exceptions) { - annotate(); - final MethodVisitor originalMethod = super.visitMethod(access, name, desc, signature, exceptions); - final Method m = new Method(name, desc); - - return new GeneratorAdapter(Opcodes.ASM4, originalMethod, access, name, desc) { - - @Override - public AnnotationVisitor visitAnnotation(String desc, boolean visible) { - if (Type.getType(Privileged.class).getDescriptor().equals(desc)) { - final AccessLevel localAccessLevel = AccessLevel.of(access); - if (accessLevel.compareTo(localAccessLevel) > 0) { - throw new RuntimeException(new IllegalAccessException("Method " + className + "#" + m - + " must have maximum access level '" + accessLevel + "' but is defined wider ('" - + localAccessLevel + "')")); - } - if (AccessLevel.PACKAGE.compareTo(accessLevel) > 0) { - privilizer().env.warn("Possible security leak: granting privileges to %s method %s.%s", - localAccessLevel, className, m); - } - privilegedMethods.put(m, privilizer().generateName(name)); - } - return super.visitAnnotation(desc, visible); - } - - @Override - public void visitCode() { - super.visitCode(); - if (!privilegedMethods.containsKey(m)) { - return; - } - final String impl = privilegedMethods.get(m); - final boolean instanceMethod = !Modifier.isStatic(access); - - if (policy.isConditional()) { - privilizer().env.debug("setting up conditional execution due to policy %s", policy); - // test, loading boolean - if (policy == Policy.ON_INIT) { - getStatic(target, privilizer().generateName("hasSecurityManager"), Type.BOOLEAN_TYPE); - } else if (policy == Policy.DYNAMIC) { - checkSecurityManager(this); - } - final Label doPrivileged = new Label(); - - // if true, goto doPrivileged: - ifZCmp(NE, doPrivileged); - - final Method implMethod = new Method(impl, desc); - if (instanceMethod) { - loadThis(); - loadArgs(); - invokeVirtual(target, implMethod); - } else { - loadArgs(); - invokeStatic(target, implMethod); - } - returnValue(); - mark(doPrivileged); - } else { - privilizer().env.debug("setting up unconditional privileged execution due to policy %s", policy); - } - - // generate action: - final Type[] ctorArgs; - if (instanceMethod) { - ctorArgs = ArrayUtils.add(m.getArgumentTypes(), 0, target); - } else { - ctorArgs = m.getArgumentTypes(); - } - final Type actionType = new ActionGenerator(access, m, exceptions, PrivilizingVisitor.this).build(); - newInstance(actionType); - dup(); - if (instanceMethod) { - loadThis(); - } - loadArgs(); - invokeConstructor(actionType, new Method("", Type.VOID_TYPE, ctorArgs)); - - final boolean exc = ArrayUtils.isNotEmpty(exceptions); - // mark try if needed - final Label privTry = exc ? mark() : null; - - // execute action - final Type arg = - exc ? Type.getType(PrivilegedExceptionAction.class) : Type.getType(PrivilegedAction.class); - final Method doPrivileged = new Method("doPrivileged", Type.getType(Object.class), new Type[] { arg }); - invokeStatic(Type.getType(AccessController.class), doPrivileged); - - unbox(m.getReturnType()); - returnValue(); - - if (exc) { - final Type caught = Type.getType(PrivilegedActionException.class); - // end try - final Label privCatch = mark(); - // catch - catchException(privTry, privCatch, caught); - // unwrap - invokeVirtual(caught, new Method("getException", Type.getType(Exception.class), - Privilizer.EMPTY_TYPE_ARRAY)); - // throw - throwException(); - } - - // end original method - endMethod(); - - // substitute an impl visitor and continue - mv = cv.visitMethod(AccessLevel.PRIVATE.merge(access), impl, desc, signature, exceptions); - mv.visitCode(); - } - }; - - } - - public void visitEnd() { - annotate(); - if (privilizer().policy == Policy.ON_INIT) { - final String fieldName = privilizer().generateName("hasSecurityManager"); - - visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, fieldName, - Type.BOOLEAN_TYPE.getDescriptor(), null, null).visitEnd(); - - final GeneratorAdapter mg = - new GeneratorAdapter(Opcodes.ACC_STATIC, new Method("", "()V"), null, - Privilizer.EMPTY_TYPE_ARRAY, this); - checkSecurityManager(mg); - mg.putStatic(target, fieldName, Type.BOOLEAN_TYPE); - mg.returnValue(); - mg.endMethod(); - } - super.visitEnd(); - } - - /** - * Generates the instructions to push onto the stack whether there is a - * security manager available. - * @param mg to control - */ - private static void checkSecurityManager(GeneratorAdapter mg) { - final Label setFalse = new Label(); - final Label done = new Label(); - mg.invokeStatic(Type.getType(System.class), - new Method("getSecurityManager", Type.getType(SecurityManager.class), Privilizer.EMPTY_TYPE_ARRAY)); - mg.ifNull(setFalse); - mg.push(true); - mg.goTo(done); - mg.mark(setFalse); - mg.push(false); - mg.mark(done); - } -} +/* + * 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.lang.reflect.Modifier; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.commons.lang3.ArrayUtils; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.commons.GeneratorAdapter; +import org.objectweb.asm.commons.Method; +import org.objectweb.asm.commons.StaticInitMerger; + +/** + * ASM {@link ClassVisitor} to privilize {@link Privileged} methods. + */ +class PrivilizingVisitor extends Privilizer.PrivilizerClassVisitor { + final Map privilegedMethods = new LinkedHashMap(); + boolean annotated; + final Policy policy; + final AccessLevel accessLevel; + + /** + * Create a new {@link PrivilizingVisitor}. + * @param privilizer owner + * @param cv next + */ + PrivilizingVisitor(Privilizer privilizer, ClassVisitor cv) { + privilizer.super(); + this.policy = privilizer.policy; + this.accessLevel = privilizer.accessLevel; + this.cv = + new InlineNestedPrivilegedCalls(privilizer, privilegedMethods, new StaticInitMerger( + privilizer.generateName("clinit"), cv)); + } + + private void annotate() { + if (!annotated) { + annotated = true; + final AnnotationVisitor privilizedVisitor = + super.visitAnnotation(Type.getType(Privilized.class).getDescriptor(), false); + privilizedVisitor.visit("value", policy.name()); + privilizedVisitor.visitEnd(); + } + } + + @Override + public void visitInnerClass(String name, String outerName, String innerName, int access) { + annotate(); + super.visitInnerClass(name, outerName, innerName, access); + } + + @Override + public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { + annotate(); + return super.visitField(access, name, desc, signature, value); + } + + @Override + public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, + final String[] exceptions) { + annotate(); + final MethodVisitor originalMethod = super.visitMethod(access, name, desc, signature, exceptions); + final Method m = new Method(name, desc); + + return new GeneratorAdapter(Opcodes.ASM4, originalMethod, access, name, desc) { + + @Override + public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + if (Type.getType(Privileged.class).getDescriptor().equals(desc)) { + final AccessLevel localAccessLevel = AccessLevel.of(access); + if (accessLevel.compareTo(localAccessLevel) > 0) { + throw new RuntimeException(new IllegalAccessException("Method " + className + "#" + m + + " must have maximum access level '" + accessLevel + "' but is defined wider ('" + + localAccessLevel + "')")); + } + if (AccessLevel.PACKAGE.compareTo(accessLevel) > 0) { + privilizer().env.warn("Possible security leak: granting privileges to %s method %s.%s", + localAccessLevel, className, m); + } + privilegedMethods.put(m, privilizer().generateName(name)); + } + return super.visitAnnotation(desc, visible); + } + + @Override + public void visitCode() { + super.visitCode(); + if (!privilegedMethods.containsKey(m)) { + return; + } + final String impl = privilegedMethods.get(m); + final boolean instanceMethod = !Modifier.isStatic(access); + + if (policy.isConditional()) { + privilizer().env.debug("setting up conditional execution due to policy %s", policy); + // test, loading boolean + if (policy == Policy.ON_INIT) { + getStatic(target, privilizer().generateName("hasSecurityManager"), Type.BOOLEAN_TYPE); + } else if (policy == Policy.DYNAMIC) { + checkSecurityManager(this); + } + final Label doPrivileged = new Label(); + + // if true, goto doPrivileged: + ifZCmp(NE, doPrivileged); + + final Method implMethod = new Method(impl, desc); + if (instanceMethod) { + loadThis(); + loadArgs(); + invokeVirtual(target, implMethod); + } else { + loadArgs(); + invokeStatic(target, implMethod); + } + returnValue(); + mark(doPrivileged); + } else { + privilizer().env.debug("setting up unconditional privileged execution due to policy %s", policy); + } + + // generate action: + final Type[] ctorArgs; + if (instanceMethod) { + ctorArgs = ArrayUtils.add(m.getArgumentTypes(), 0, target); + } else { + ctorArgs = m.getArgumentTypes(); + } + final Type actionType = new ActionGenerator(access, m, exceptions, PrivilizingVisitor.this).build(); + newInstance(actionType); + dup(); + if (instanceMethod) { + loadThis(); + } + loadArgs(); + invokeConstructor(actionType, new Method("", Type.VOID_TYPE, ctorArgs)); + + final boolean exc = ArrayUtils.isNotEmpty(exceptions); + // mark try if needed + final Label privTry = exc ? mark() : null; + + // execute action + final Type arg = + exc ? Type.getType(PrivilegedExceptionAction.class) : Type.getType(PrivilegedAction.class); + final Method doPrivileged = new Method("doPrivileged", Type.getType(Object.class), new Type[] { arg }); + invokeStatic(Type.getType(AccessController.class), doPrivileged); + + unbox(m.getReturnType()); + returnValue(); + + if (exc) { + final Type caught = Type.getType(PrivilegedActionException.class); + // end try + final Label privCatch = mark(); + // catch + catchException(privTry, privCatch, caught); + // unwrap + invokeVirtual(caught, new Method("getException", Type.getType(Exception.class), + Privilizer.EMPTY_TYPE_ARRAY)); + // throw + throwException(); + } + + // end original method + endMethod(); + + // substitute an impl visitor and continue + mv = cv.visitMethod(AccessLevel.PRIVATE.merge(access), impl, desc, signature, exceptions); + mv.visitCode(); + } + }; + + } + + public void visitEnd() { + annotate(); + if (privilizer().policy == Policy.ON_INIT) { + final String fieldName = privilizer().generateName("hasSecurityManager"); + + visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, fieldName, + Type.BOOLEAN_TYPE.getDescriptor(), null, null).visitEnd(); + + final GeneratorAdapter mg = + new GeneratorAdapter(Opcodes.ACC_STATIC, new Method("", "()V"), null, + Privilizer.EMPTY_TYPE_ARRAY, this); + checkSecurityManager(mg); + mg.putStatic(target, fieldName, Type.BOOLEAN_TYPE); + mg.returnValue(); + mg.endMethod(); + } + super.visitEnd(); + } + + /** + * Generates the instructions to push onto the stack whether there is a + * security manager available. + * @param mg to control + */ + private static void checkSecurityManager(GeneratorAdapter mg) { + final Label setFalse = new Label(); + final Label done = new Label(); + mg.invokeStatic(Type.getType(System.class), + new Method("getSecurityManager", Type.getType(SecurityManager.class), Privilizer.EMPTY_TYPE_ARRAY)); + mg.ifNull(setFalse); + mg.push(true); + mg.goTo(done); + mg.mark(setFalse); + mg.push(false); + mg.mark(done); + } +} Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/package-info.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/package-info.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/CleanProcessor.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/Inflater.java URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/Inflater.java?rev=1567781&r1=1567780&r2=1567781&view=diff ============================================================================== --- commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/Inflater.java (original) +++ commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/Inflater.java Wed Feb 12 22:40:06 2014 @@ -1,197 +1,197 @@ -/* - * 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; - -import java.lang.annotation.Annotation; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.commons.weaver.model.ScanResult; -import org.apache.commons.weaver.model.WeavableClass; -import org.apache.commons.weaver.model.WeavableConstructor; -import org.apache.commons.weaver.model.WeavableConstructorParameter; -import org.apache.commons.weaver.model.WeavableField; -import org.apache.commons.weaver.model.WeavableMethod; -import org.apache.commons.weaver.model.WeavableMethodParameter; -import org.apache.commons.weaver.model.WeavablePackage; -import org.apache.xbean.finder.AnnotationFinder.ClassInfo; -import org.apache.xbean.finder.AnnotationFinder.FieldInfo; -import org.apache.xbean.finder.AnnotationFinder.Info; -import org.apache.xbean.finder.AnnotationFinder.MethodInfo; -import org.apache.xbean.finder.AnnotationFinder.PackageInfo; -import org.apache.xbean.finder.AnnotationFinder.ParameterInfo; -import org.apache.xbean.finder.Parameter; - -/** - * Adds all classfile annotations to a ScanResult. - */ -class Inflater { - private class InfoMatcher { - final Class type; - - InfoMatcher(Class type) { - super(); - this.type = type; - } - - boolean test(Info info) { - return type.isInstance(info); - } - - } - - private class MethodMatcher extends InfoMatcher { - final boolean cs; - - MethodMatcher(boolean cs) { - super(MethodInfo.class); - this.cs = cs; - } - - @Override - boolean test(Info info) { - return super.test(info) && ((MethodInfo) info).isConstructor() == cs; - } - } - - private class ParameterMatcher extends InfoMatcher { - final boolean cs; - - ParameterMatcher(boolean cs) { - super(ParameterInfo.class); - this.cs = cs; - } - - @Override - boolean test(Info info) { - return super.test(info) && ((ParameterInfo) info).getDeclaringMethod().isConstructor() == cs; - } - } - - final Map> packageAnnotations; - final Map> classAnnotations; - final Map> fieldAnnotations; - final Map> ctorAnnotations; - final Map> methodAnnotations; - final Map> ctorParameterAnnotations; - final Map> methodParameterAnnotations; - - Inflater(Map> m) { - super(); - - this.packageAnnotations = subMap(m, new InfoMatcher(PackageInfo.class)); - this.classAnnotations = subMap(m, new InfoMatcher(ClassInfo.class)); - this.fieldAnnotations = subMap(m, new InfoMatcher(FieldInfo.class)); - this.ctorAnnotations = subMap(m, new MethodMatcher(true)); - this.methodAnnotations = subMap(m, new MethodMatcher(false)); - this.ctorParameterAnnotations = subMap(m, new ParameterMatcher(true)); - this.methodParameterAnnotations = subMap(m, new ParameterMatcher(false)); - } - - static Map> subMap(Map> source, InfoMatcher matcher) { - final HashMap> result = new HashMap>(); - for (Map.Entry> e : source.entrySet()) { - if (matcher.test(e.getKey())) { - @SuppressWarnings("unchecked") - final I key = (I) e.getKey(); - result.put(key, e.getValue()); - } - } - return result; - } - - ScanResult inflate(final ScanResult scanResult) { - for (WeavablePackage pkg : scanResult.getPackages()) { - for (Map.Entry> e : packageAnnotations.entrySet()) { - if (e.getKey().getName().equals(pkg.getTarget().getName())) { - pkg.addAnnotations(e.getValue()); - } - } - for (WeavableClass cls : pkg.getClasses()) { - for (Map.Entry> e : classAnnotations.entrySet()) { - if (e.getKey().getName().equals(cls.getTarget().getName())) { - cls.addAnnotations(e.getValue()); - } - } - for (WeavableField fld : cls.getFields()) { - for (Map.Entry> e : fieldAnnotations.entrySet()) { - try { - if (e.getKey().get().equals(fld.getTarget())) { - fld.addAnnotations(e.getValue()); - } - } catch (ClassNotFoundException cnfe) { - // :/ - } - } - } - for (WeavableConstructor cs : cls.getConstructors()) { - for (Map.Entry> e : ctorAnnotations.entrySet()) { - try { - if (e.getKey().get().equals(cs.getTarget())) { - cs.addAnnotations(e.getValue()); - } - } catch (ClassNotFoundException cnfe) { - // :/ - } - } - for (WeavableConstructorParameter p : cs.getParameters()) { - for (Map.Entry> e : ctorParameterAnnotations.entrySet()) { - try { - final Parameter parameter = e.getKey().get(); - if (parameter.getDeclaringExecutable().equals(cs.getTarget()) - && p.getTarget().intValue() == parameter.getIndex()) { - p.addAnnotations(e.getValue()); - } - } catch (ClassNotFoundException cnfe) { - // :/ - } - } - } - } - for (WeavableMethod mt : cls.getMethods()) { - for (Map.Entry> e : methodAnnotations.entrySet()) { - try { - if (e.getKey().get().equals(mt.getTarget())) { - mt.addAnnotations(e.getValue()); - } - } catch (ClassNotFoundException cnfe) { - // :/ - } - } - for (WeavableMethodParameter p : mt.getParameters()) { - for (Map.Entry> e : methodParameterAnnotations.entrySet()) { - try { - final Parameter parameter = e.getKey().get(); - if (parameter.getDeclaringExecutable().equals(mt.getTarget()) - && p.getTarget().intValue() == parameter.getIndex()) { - p.addAnnotations(e.getValue()); - } - } catch (ClassNotFoundException cnfe) { - // :/ - } - } - } - } - } - } - return scanResult; - } - -} +/* + * 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; + +import java.lang.annotation.Annotation; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.weaver.model.ScanResult; +import org.apache.commons.weaver.model.WeavableClass; +import org.apache.commons.weaver.model.WeavableConstructor; +import org.apache.commons.weaver.model.WeavableConstructorParameter; +import org.apache.commons.weaver.model.WeavableField; +import org.apache.commons.weaver.model.WeavableMethod; +import org.apache.commons.weaver.model.WeavableMethodParameter; +import org.apache.commons.weaver.model.WeavablePackage; +import org.apache.xbean.finder.AnnotationFinder.ClassInfo; +import org.apache.xbean.finder.AnnotationFinder.FieldInfo; +import org.apache.xbean.finder.AnnotationFinder.Info; +import org.apache.xbean.finder.AnnotationFinder.MethodInfo; +import org.apache.xbean.finder.AnnotationFinder.PackageInfo; +import org.apache.xbean.finder.AnnotationFinder.ParameterInfo; +import org.apache.xbean.finder.Parameter; + +/** + * Adds all classfile annotations to a ScanResult. + */ +class Inflater { + private class InfoMatcher { + final Class type; + + InfoMatcher(Class type) { + super(); + this.type = type; + } + + boolean test(Info info) { + return type.isInstance(info); + } + + } + + private class MethodMatcher extends InfoMatcher { + final boolean cs; + + MethodMatcher(boolean cs) { + super(MethodInfo.class); + this.cs = cs; + } + + @Override + boolean test(Info info) { + return super.test(info) && ((MethodInfo) info).isConstructor() == cs; + } + } + + private class ParameterMatcher extends InfoMatcher { + final boolean cs; + + ParameterMatcher(boolean cs) { + super(ParameterInfo.class); + this.cs = cs; + } + + @Override + boolean test(Info info) { + return super.test(info) && ((ParameterInfo) info).getDeclaringMethod().isConstructor() == cs; + } + } + + final Map> packageAnnotations; + final Map> classAnnotations; + final Map> fieldAnnotations; + final Map> ctorAnnotations; + final Map> methodAnnotations; + final Map> ctorParameterAnnotations; + final Map> methodParameterAnnotations; + + Inflater(Map> m) { + super(); + + this.packageAnnotations = subMap(m, new InfoMatcher(PackageInfo.class)); + this.classAnnotations = subMap(m, new InfoMatcher(ClassInfo.class)); + this.fieldAnnotations = subMap(m, new InfoMatcher(FieldInfo.class)); + this.ctorAnnotations = subMap(m, new MethodMatcher(true)); + this.methodAnnotations = subMap(m, new MethodMatcher(false)); + this.ctorParameterAnnotations = subMap(m, new ParameterMatcher(true)); + this.methodParameterAnnotations = subMap(m, new ParameterMatcher(false)); + } + + static Map> subMap(Map> source, InfoMatcher matcher) { + final HashMap> result = new HashMap>(); + for (Map.Entry> e : source.entrySet()) { + if (matcher.test(e.getKey())) { + @SuppressWarnings("unchecked") + final I key = (I) e.getKey(); + result.put(key, e.getValue()); + } + } + return result; + } + + ScanResult inflate(final ScanResult scanResult) { + for (WeavablePackage pkg : scanResult.getPackages()) { + for (Map.Entry> e : packageAnnotations.entrySet()) { + if (e.getKey().getName().equals(pkg.getTarget().getName())) { + pkg.addAnnotations(e.getValue()); + } + } + for (WeavableClass cls : pkg.getClasses()) { + for (Map.Entry> e : classAnnotations.entrySet()) { + if (e.getKey().getName().equals(cls.getTarget().getName())) { + cls.addAnnotations(e.getValue()); + } + } + for (WeavableField fld : cls.getFields()) { + for (Map.Entry> e : fieldAnnotations.entrySet()) { + try { + if (e.getKey().get().equals(fld.getTarget())) { + fld.addAnnotations(e.getValue()); + } + } catch (ClassNotFoundException cnfe) { + // :/ + } + } + } + for (WeavableConstructor cs : cls.getConstructors()) { + for (Map.Entry> e : ctorAnnotations.entrySet()) { + try { + if (e.getKey().get().equals(cs.getTarget())) { + cs.addAnnotations(e.getValue()); + } + } catch (ClassNotFoundException cnfe) { + // :/ + } + } + for (WeavableConstructorParameter p : cs.getParameters()) { + for (Map.Entry> e : ctorParameterAnnotations.entrySet()) { + try { + final Parameter parameter = e.getKey().get(); + if (parameter.getDeclaringExecutable().equals(cs.getTarget()) + && p.getTarget().intValue() == parameter.getIndex()) { + p.addAnnotations(e.getValue()); + } + } catch (ClassNotFoundException cnfe) { + // :/ + } + } + } + } + for (WeavableMethod mt : cls.getMethods()) { + for (Map.Entry> e : methodAnnotations.entrySet()) { + try { + if (e.getKey().get().equals(mt.getTarget())) { + mt.addAnnotations(e.getValue()); + } + } catch (ClassNotFoundException cnfe) { + // :/ + } + } + for (WeavableMethodParameter p : mt.getParameters()) { + for (Map.Entry> e : methodParameterAnnotations.entrySet()) { + try { + final Parameter parameter = e.getKey().get(); + if (parameter.getDeclaringExecutable().equals(mt.getTarget()) + && p.getTarget().intValue() == parameter.getIndex()) { + p.addAnnotations(e.getValue()); + } + } catch (ClassNotFoundException cnfe) { + // :/ + } + } + } + } + } + } + return scanResult; + } + +} Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/Inflater.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/Inflater.java ('svn:executable' removed) Modified: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/LocalWeaveEnvironment.java URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/LocalWeaveEnvironment.java?rev=1567781&r1=1567780&r2=1567781&view=diff ============================================================================== --- commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/LocalWeaveEnvironment.java (original) +++ commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/LocalWeaveEnvironment.java Wed Feb 12 22:40:06 2014 @@ -1,57 +1,57 @@ -/* - * 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; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.Properties; -import java.util.logging.Logger; - -import org.apache.commons.lang3.Validate; -import org.apache.commons.weaver.model.WeaveEnvironment; - -class LocalWeaveEnvironment extends WeaveEnvironment { - - private final File target; - - protected LocalWeaveEnvironment(File target, ClassLoader classLoader, Properties config, Logger log) { - super(classLoader, config, log); - Validate.notNull(target, "target"); - this.target = target; - } - - @Override - public boolean deleteResource(String name) { - return new File(target, name).delete(); - } - - @Override - protected OutputStream getOutputStream(String resourceName) throws IOException { - final File file = new File(target, resourceName); - final File parent = file.getParentFile(); - if (!parent.exists()) { - Validate.validState(parent.mkdirs(), "Unable to create output directory %s", parent); - } else { - Validate.validState(parent.isDirectory(), "Cannot write %s to non-directory parent", file); - } - return new FileOutputStream(file); - } -} +/* + * 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; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Properties; +import java.util.logging.Logger; + +import org.apache.commons.lang3.Validate; +import org.apache.commons.weaver.model.WeaveEnvironment; + +class LocalWeaveEnvironment extends WeaveEnvironment { + + private final File target; + + protected LocalWeaveEnvironment(File target, ClassLoader classLoader, Properties config, Logger log) { + super(classLoader, config, log); + Validate.notNull(target, "target"); + this.target = target; + } + + @Override + public boolean deleteResource(String name) { + return new File(target, name).delete(); + } + + @Override + protected OutputStream getOutputStream(String resourceName) throws IOException { + final File file = new File(target, resourceName); + final File parent = file.getParentFile(); + if (!parent.exists()) { + Validate.validState(parent.mkdirs(), "Unable to create output directory %s", parent); + } else { + Validate.validState(parent.isDirectory(), "Cannot write %s to non-directory parent", file); + } + return new FileOutputStream(file); + } +} Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/LocalWeaveEnvironment.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/LocalWeaveEnvironment.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/Scanner.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/package-info.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/model/package-info.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/package-info.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/package-info.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/spi/Cleaner.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/spi/package-info.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/spi/package-info.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/utils/package-info.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/utils/package-info.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/CleanProcessorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/AbstractTestBean.java URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/AbstractTestBean.java?rev=1567781&r1=1567780&r2=1567781&view=diff ============================================================================== --- commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/AbstractTestBean.java (original) +++ commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/AbstractTestBean.java Wed Feb 12 22:40:06 2014 @@ -1,23 +1,23 @@ -/* - * 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.test.beans; - -public abstract class AbstractTestBean { - -} +/* + * 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.test.beans; + +public abstract class AbstractTestBean { + +} Propchange: commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/AbstractTestBean.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/AbstractTestBean.java ('svn:executable' removed) Modified: commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanInterface.java URL: http://svn.apache.org/viewvc/commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanInterface.java?rev=1567781&r1=1567780&r2=1567781&view=diff ============================================================================== --- commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanInterface.java (original) +++ commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanInterface.java Wed Feb 12 22:40:06 2014 @@ -1,23 +1,23 @@ -/* - * 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.test.beans; - -public interface TestBeanInterface { - -} +/* + * 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.test.beans; + +public interface TestBeanInterface { + +} Propchange: commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanInterface.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanInterface.java ('svn:executable' removed) Propchange: commons/proper/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestCleaner.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/src/changes/changes.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/weaver/trunk/src/changes/changes.xml ('svn:executable' removed)