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/08/11 19:48:38 UTC
svn commit: r1512978 - in
/commons/sandbox/weaver/branches/normalizer/modules: ./ normalizer/
normalizer/example/ normalizer/example/src/ normalizer/example/src/main/
normalizer/example/src/main/java/ normalizer/example/src/main/java/org/
normalizer/ex...
Author: mbenson
Date: Sun Aug 11 17:48:37 2013
New Revision: 1512978
URL: http://svn.apache.org/r1512978
Log:
working normalizer module
Added:
commons/sandbox/weaver/branches/normalizer/modules/normalizer/ (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/ (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/pom.xml (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/ContrivedWrapper.java (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/InstanceMembers.java (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/StaticMembers.java (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/TypeLiteral.java
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/normalizer/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/normalizer/example/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/normalizer/example/NormalizerTest.java (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/pom.xml (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/ (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/pom.xml (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/NormalizerWeaver.java (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Utils.java (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/META-INF/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/META-INF/services/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver (with props)
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/weaver/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/weaver/normalizer/
commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/weaver/normalizer/UtilsTest.java (with props)
Modified:
commons/sandbox/weaver/branches/normalizer/modules/pom.xml
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Aug 11 17:48:37 2013
@@ -0,0 +1,12 @@
+target
+.metadata
+.classpath
+.project
+.settings
+*.iml
+*.ipr
+*.iws
+.idea
+*.log
+.git
+.gitignore
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Aug 11 17:48:37 2013
@@ -0,0 +1,12 @@
+target
+.metadata
+.classpath
+.project
+.settings
+*.iml
+*.ipr
+*.iws
+.idea
+*.log
+.git
+.gitignore
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/pom.xml?rev=1512978&view=auto
==============================================================================
Binary file - no diff available.
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/pom.xml
------------------------------------------------------------------------------
svn:executable = *
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/pom.xml
------------------------------------------------------------------------------
svn:mime-type = application/xml
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/ContrivedWrapper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/ContrivedWrapper.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/ContrivedWrapper.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/ContrivedWrapper.java Sun Aug 11 17:48:37 2013
@@ -0,0 +1,49 @@
+/*
+ * 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.normalizer.example;
+
+public abstract class ContrivedWrapper {
+ public final Object value;
+
+ protected ContrivedWrapper(Object value) {
+ this.value = value;
+ }
+
+ protected ContrivedWrapper(int value) {
+ this.value = Integer.valueOf(value);
+ }
+
+ @Override
+ public final boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof ContrivedWrapper == false) {
+ return false;
+ }
+ final ContrivedWrapper other = (ContrivedWrapper) obj;
+ return value.equals(other.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return 41 << 4 | value.hashCode();
+ }
+
+}
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/ContrivedWrapper.java
------------------------------------------------------------------------------
svn:executable = *
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/InstanceMembers.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/InstanceMembers.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/InstanceMembers.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/InstanceMembers.java Sun Aug 11 17:48:37 2013
@@ -0,0 +1,31 @@
+/*
+ * 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.normalizer.example;
+
+public class InstanceMembers {
+ public final TypeLiteral<String> STRING_TYPE = new TypeLiteral<String>() {};
+ public final TypeLiteral<String> STRING_TYPE2 = new TypeLiteral<String>() {};
+ public final TypeLiteral<Iterable<Integer>> INTEGER_ITERABLE_TYPE = new TypeLiteral<Iterable<Integer>>() {};
+
+ public final ContrivedWrapper WRAPPED_OBJECT = new ContrivedWrapper(new Object()) {};
+ public final ContrivedWrapper WRAPPED_STRING = new ContrivedWrapper("foo") {};
+ public final ContrivedWrapper WRAPPED_STRING2 = new ContrivedWrapper("foo") {};
+ public final ContrivedWrapper WRAPPED_INTEGER = new ContrivedWrapper(Integer.valueOf(1)) {};
+ public final ContrivedWrapper WRAPPED_INT = new ContrivedWrapper(1) {};
+}
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/InstanceMembers.java
------------------------------------------------------------------------------
svn:executable = *
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/StaticMembers.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/StaticMembers.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/StaticMembers.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/StaticMembers.java Sun Aug 11 17:48:37 2013
@@ -0,0 +1,31 @@
+/*
+ * 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.normalizer.example;
+
+public class StaticMembers {
+ public static final TypeLiteral<String> STRING_TYPE = new TypeLiteral<String>() {};
+ public static final TypeLiteral<String> STRING_TYPE2 = new TypeLiteral<String>() {};
+ public static final TypeLiteral<Iterable<Integer>> INTEGER_ITERABLE_TYPE = new TypeLiteral<Iterable<Integer>>() {};
+
+ public static final ContrivedWrapper WRAPPED_OBJECT = new ContrivedWrapper(new Object()) {};
+ public static final ContrivedWrapper WRAPPED_STRING = new ContrivedWrapper("foo") {};
+ public static final ContrivedWrapper WRAPPED_STRING2 = new ContrivedWrapper("foo") {};
+ public static final ContrivedWrapper WRAPPED_INTEGER = new ContrivedWrapper(Integer.valueOf(1)) {};
+ public static final ContrivedWrapper WRAPPED_INT = new ContrivedWrapper(1) {};
+}
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/StaticMembers.java
------------------------------------------------------------------------------
svn:executable = *
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/TypeLiteral.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/TypeLiteral.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/TypeLiteral.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/main/java/org/apache/commons/weaver/normalizer/example/TypeLiteral.java Sun Aug 11 17:48:37 2013
@@ -0,0 +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.normalizer.example;
+
+import java.lang.reflect.Type;
+
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.reflect.TypeUtils;
+
+/**
+ * Same old "Type literal."
+ */
+public abstract class TypeLiteral<T> {
+ public final Type value;
+
+ protected TypeLiteral() {
+ this.value =
+ ObjectUtils
+ .defaultIfNull(
+ TypeUtils.getTypeArguments(getClass(), TypeLiteral.class).get(
+ TypeLiteral.class.getTypeParameters()[0]), Object.class);
+ }
+
+ @Override
+ public final boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof TypeLiteral == false) {
+ return false;
+ }
+ final TypeLiteral<?> other = (TypeLiteral<?>) obj;
+ return value.equals(other.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return 37 << 4 | value.hashCode();
+ }
+
+}
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/normalizer/example/NormalizerTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/normalizer/example/NormalizerTest.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/normalizer/example/NormalizerTest.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/normalizer/example/NormalizerTest.java Sun Aug 11 17:48:37 2013
@@ -0,0 +1,63 @@
+/*
+ * 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.normalizer.example;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class NormalizerTest {
+ private InstanceMembers instanceMembers;
+
+ @Before
+ public void setup() {
+ instanceMembers = new InstanceMembers();
+ }
+
+ @Test
+ public void testGenericType() {
+ final Class<?> stringLiteral = StaticMembers.STRING_TYPE.getClass();
+ assertTrue(stringLiteral.isInstance(StaticMembers.STRING_TYPE2));
+ assertFalse(stringLiteral.isInstance(StaticMembers.INTEGER_ITERABLE_TYPE));
+ assertTrue(stringLiteral.isInstance(instanceMembers.STRING_TYPE));
+ assertTrue(stringLiteral.isInstance(instanceMembers.STRING_TYPE2));
+ assertFalse(stringLiteral.isInstance(instanceMembers.INTEGER_ITERABLE_TYPE));
+
+ final Class<?> integerIterable = StaticMembers.INTEGER_ITERABLE_TYPE.getClass();
+ assertTrue(integerIterable.isInstance(instanceMembers.INTEGER_ITERABLE_TYPE));
+ }
+
+ @Test
+ public void testAlternateConstructors() {
+ final Class<?> objectWrapper = StaticMembers.WRAPPED_OBJECT.getClass();
+ assertTrue(objectWrapper.isInstance(StaticMembers.WRAPPED_STRING));
+ assertTrue(objectWrapper.isInstance(StaticMembers.WRAPPED_STRING2));
+ assertTrue(objectWrapper.isInstance(StaticMembers.WRAPPED_INTEGER));
+ assertFalse(objectWrapper.isInstance(StaticMembers.WRAPPED_INT));
+ assertTrue(objectWrapper.isInstance(instanceMembers.WRAPPED_OBJECT));
+ assertTrue(objectWrapper.isInstance(instanceMembers.WRAPPED_STRING));
+ assertTrue(objectWrapper.isInstance(instanceMembers.WRAPPED_STRING2));
+ assertTrue(objectWrapper.isInstance(instanceMembers.WRAPPED_INTEGER));
+ assertFalse(objectWrapper.isInstance(instanceMembers.WRAPPED_INT));
+ final Class<?> intWrapper = StaticMembers.WRAPPED_INT.getClass();
+ assertTrue(intWrapper.isInstance(instanceMembers.WRAPPED_INT));
+ }
+}
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/example/src/test/java/org/apache/commons/weaver/normalizer/example/NormalizerTest.java
------------------------------------------------------------------------------
svn:executable = *
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/pom.xml?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/pom.xml (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/pom.xml Sun Aug 11 17:48:37 2013
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-weaver-modules-parent</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>commons-weaver-normalizer-parent</artifactId>
+ <packaging>pom</packaging>
+
+ <name>Commons Weaver Normalizer Parent POM</name>
+
+ <description>
+ The Normalizer module merges identical anonymous class definitions into
+ a single type, thereby "normalizing" them and reducing their
+ collective footprint on your archive and more importantly on your JVM.
+ </description>
+
+ <modules>
+ <module>weaver</module>
+ <module>example</module>
+ </modules>
+</project>
+
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/pom.xml
------------------------------------------------------------------------------
svn:executable = *
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/pom.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Aug 11 17:48:37 2013
@@ -0,0 +1,12 @@
+target
+.metadata
+.classpath
+.project
+.settings
+*.iml
+*.ipr
+*.iws
+.idea
+*.log
+.git
+.gitignore
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/pom.xml?rev=1512978&view=auto
==============================================================================
Binary file - no diff available.
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/pom.xml
------------------------------------------------------------------------------
svn:executable = *
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/pom.xml
------------------------------------------------------------------------------
svn:mime-type = application/xml
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java Sun Aug 11 17:48:37 2013
@@ -0,0 +1,501 @@
+/*
+ * 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.normalizer;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+import java.nio.charset.Charset;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.CharEncoding;
+import org.apache.commons.lang3.Validate;
+import org.apache.commons.lang3.mutable.MutableBoolean;
+import org.apache.commons.lang3.tuple.MutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.weaver.model.ScanRequest;
+import org.apache.commons.weaver.model.ScanResult;
+import org.apache.commons.weaver.model.Scanner;
+import org.apache.commons.weaver.model.WeavableClass;
+import org.apache.commons.weaver.model.WeaveEnvironment;
+import org.apache.xbean.finder.archive.FileArchive;
+import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ClassWriter;
+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.Remapper;
+import org.objectweb.asm.commons.RemappingClassAdapter;
+import org.objectweb.asm.commons.SimpleRemapper;
+
+public class Normalizer {
+ private static final Type OBJECT_TYPE = Type.getType(Object.class);
+
+ @Target(ElementType.TYPE)
+ private @interface Marker {
+ }
+
+ private static class ClassWrapper {
+ final Class<?> wrapped;
+ final boolean mustRewriteConstructor;
+
+ ClassWrapper(Class<?> wrapped, boolean mustRewriteConstructor) {
+ this.wrapped = wrapped;
+ this.mustRewriteConstructor = mustRewriteConstructor;
+ }
+ }
+
+ private class WriteClass extends ClassVisitor {
+ private String className;
+
+ WriteClass() {
+ super(Opcodes.ASM4, new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS));
+ }
+
+ WriteClass(ClassReader reader) {
+ super(Opcodes.ASM4, new ClassWriter(reader, 0));
+ }
+
+ @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;
+ }
+
+ @Override
+ public void visitEnd() {
+ super.visitEnd();
+ final byte[] bytecode = ((ClassWriter) cv).toByteArray();
+
+ final File f = new File(fileArchive.getDir(), className.replace('.', File.separatorChar) + ".class");
+ env.debug("Writing class %s to %s", className, f);
+ try {
+ FileUtils.writeByteArrayToFile(f, bytecode);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private enum IneligibilityReason {
+ NOT_ANONYMOUS, TOO_MANY_CONSTRUCTORS, IMPLEMENTS_METHODS, TOO_BUSY_CONSTRUCTOR;
+ }
+
+ public static final String CONFIG_WEAVER = "normalizer.";
+
+ /**
+ * Property name referencing a comma-delimited list of types whose subclasses/implementations should be normalized,
+ * e.g. {@code javax.enterprise.util.TypeLiteral}.
+ */
+ public static final String CONFIG_SUPER_TYPES = CONFIG_WEAVER + "superTypes";
+
+ /**
+ * Property name referencing a package name to which merged types should be added.
+ */
+ public static final String CONFIG_TARGET_PACKAGE = CONFIG_WEAVER + "targetPackage";
+
+ private static final Charset UTF8 = Charset.forName(CharEncoding.UTF_8);
+
+ private final WeaveEnvironment env;
+ private final FileArchive fileArchive;
+
+ private final Set<Class<?>> normalizeTypes;
+ private final String targetPackage;
+
+ public Normalizer(WeaveEnvironment env) {
+ this.env = env;
+ fileArchive = new FileArchive(env.classLoader, env.target);
+
+ this.targetPackage =
+ Utils.validatePackageName(Validate.notBlank(env.config.getProperty(CONFIG_TARGET_PACKAGE),
+ "missing target package name"));
+ this.normalizeTypes =
+ Utils.parseTypes(
+ Validate.notEmpty(env.config.getProperty(CONFIG_SUPER_TYPES), "no types specified for normalization"),
+ env.classLoader);
+ }
+
+ public boolean normalize(Scanner scanner) {
+ for (Class<?> supertype : normalizeTypes) {
+ final Set<Class<?>> subtypes = getBroadlyEligibleSubclasses(supertype, scanner);
+ try {
+ final Map<Pair<String, String>, Set<ClassWrapper>> segregatedSubtypes = segregate(subtypes);
+ for (Map.Entry<Pair<String, String>, Set<ClassWrapper>> e : segregatedSubtypes.entrySet()) {
+ rewrite(e.getKey(), e.getValue());
+ }
+ } catch (Exception e) {
+ throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
+ }
+ }
+ return true;
+ }
+
+ private Map<String, Map<String, ClassWrapper>> byEnclosingClass(Set<ClassWrapper> sort) {
+ final Map<String, Map<String, ClassWrapper>> result = new HashMap<String, Map<String, ClassWrapper>>();
+ for (ClassWrapper w : sort) {
+ final String outer = w.wrapped.getEnclosingClass().getName();
+ Map<String, ClassWrapper> m = result.get(outer);
+ if (m == null) {
+ m = new LinkedHashMap<String, Normalizer.ClassWrapper>();
+ result.put(outer, m);
+ }
+ m.put(w.wrapped.getName().replace('.', '/'), w);
+ }
+ return result;
+ }
+
+ private void rewrite(Pair<String, String> key, Set<ClassWrapper> toMerge) throws IOException,
+ ClassNotFoundException {
+ final String target = copy(key, toMerge.iterator().next());
+ env.info("Merging %s identical %s implementations with constructor %s to type %s", toMerge.size(),
+ key.getLeft(), key.getRight(), target);
+
+ final Map<String, Map<String, ClassWrapper>> byEnclosingClass = byEnclosingClass(toMerge);
+ for (final Map.Entry<String, Map<String, ClassWrapper>> e : byEnclosingClass.entrySet()) {
+ final String outer = e.getKey();
+ env.debug("Normalizing %s inner classes of %s", e.getValue().size(), outer);
+ final Map<String, String> classMap = new HashMap<String, String>();
+ for (String merged : e.getValue().keySet()) {
+ classMap.put(merged, target);
+ }
+ final Remapper remapper = new SimpleRemapper(classMap);
+
+ final InputStream enclosingBytecode = fileArchive.getBytecode(outer);
+ try {
+ final ClassReader reader = new ClassReader(enclosingBytecode);
+
+ final ClassVisitor cv = new RemappingClassAdapter(new WriteClass(reader), remapper) {
+
+ @Override
+ public void visitInnerClass(String name, String outerName, String innerName, int access) {
+ if (!classMap.containsKey(name)) {
+ super.visitInnerClass(name, outerName, innerName, access);
+ }
+ }
+
+ @Override
+ public MethodVisitor visitMethod(int access, String name, String desc, String signature,
+ String[] exceptions) {
+ final MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
+ return new MethodVisitor(Opcodes.ASM4, mv) {
+ @Override
+ public void visitMethodInsn(int opcode, String owner, String name, String desc) {
+ if ("<init>".equals(name)) {
+ final ClassWrapper w = e.getValue().get(owner);
+ if (w != null && w.mustRewriteConstructor) {
+ // simply replace first argument type with OBJECT_TYPE:
+ final Type[] args = Type.getArgumentTypes(desc);
+ args[0] = OBJECT_TYPE;
+ desc = new Method("<init>", Type.VOID_TYPE, args).getDescriptor();
+ }
+ }
+ super.visitMethodInsn(opcode, owner, name, desc);
+ }
+ };
+ }
+ };
+
+ reader.accept(cv, 0);
+ } finally {
+ IOUtils.closeQuietly(enclosingBytecode);
+ }
+ for (String merged : e.getValue().keySet()) {
+ final File classfile = new File(env.target, merged + ".class");
+ if (!classfile.exists()) {
+ env.error("Cannot find classfile for type %s; expected at %s", merged, classfile);
+ }
+ if (classfile.delete()) {
+ env.debug("Deleted classfile %s", classfile);
+ } else {
+ env.warn("Unable to delete classfile %s", classfile);
+ }
+ }
+ }
+
+ }
+
+ /**
+ * Find subclasses/implementors of {code supertype} that:
+ * <ul>
+ * <li>are anonymous</li>
+ * <li>declare a single constructor (probably redundant in the case of an anonymous class)</li>
+ * <li>do not implement any methods</li>
+ * </ul>
+ *
+ * Considered "broadly" eligible because the instructions in the implemented constructor may remove the class from
+ * consideration later on.
+ *
+ * @param supertype
+ * @param scanner
+ * @return Set of Class
+ * @see #segregate(Iterable)
+ */
+ private Set<Class<?>> getBroadlyEligibleSubclasses(Class<?> supertype, Scanner scanner) {
+ final ScanResult scanResult = scanner.scan(new ScanRequest().addSupertypes(supertype));
+ final Set<Class<?>> result = new LinkedHashSet<Class<?>>();
+ for (WeavableClass<?> w : scanResult.getClasses()) {
+ final Class<?> subtype = w.getTarget();
+ IneligibilityReason reason = null;
+ if (!subtype.isAnonymousClass()) {
+ reason = IneligibilityReason.NOT_ANONYMOUS;
+ } else if (subtype.getDeclaredConstructors().length != 1) {
+ reason = IneligibilityReason.TOO_MANY_CONSTRUCTORS;
+ } else if (subtype.getDeclaredMethods().length > 0) {
+ reason = IneligibilityReason.IMPLEMENTS_METHODS;
+ }
+ if (reason == null) {
+ result.add(subtype);
+ } else {
+ env.debug("Removed %s from consideration due to %s", subtype, reason);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Segregate a number of classes (presumed subclasses/implementors of a common supertype/interface). The keys of the
+ * map consist of the important parts for identifying similar anonymous types: the "signature" and the invoked
+ * superclass constructor. For our purposes, the signature consists of the first applicable item of:
+ * <ol>
+ * <li>The generic signature of the class</li>
+ * <li>The sole implemented interface</li>
+ * <li>The superclass</li>
+ * </ol>
+ *
+ * The class will be considered ineligible if its constructor is too "busy" as its side effects cannot be
+ * anticipated; the normalizer will err on the side of caution.
+ *
+ * Further, we will here avail ourselves of the opportunity to discard any types we have already normalized.
+ *
+ * @param subtypes
+ * @return Map of ClassKey to Set of Classes
+ * @throws Exception
+ */
+ private Map<Pair<String, String>, Set<ClassWrapper>> segregate(Iterable<Class<?>> subtypes) throws Exception {
+ final Map<Pair<String, String>, Set<ClassWrapper>> classMap =
+ new LinkedHashMap<Pair<String, String>, Set<ClassWrapper>>();
+ for (Class<?> subtype : subtypes) {
+ final MutablePair<String, String> key = new MutablePair<String, String>();
+ final MutableBoolean valid = new MutableBoolean(true);
+ final MutableBoolean mustRewriteConstructor = new MutableBoolean();
+ final InputStream bytecode = fileArchive.getBytecode(subtype.getName());
+
+ try {
+ new ClassReader(bytecode).accept(new ClassVisitor(Opcodes.ASM4) {
+ String superName;
+
+ @Override
+ public void visit(int version, int access, String name, String signature, String superName,
+ String[] interfaces) {
+ super.visit(version, access, name, signature, superName, interfaces);
+ this.superName = superName;
+ final String left;
+ if (signature != null) {
+ left = signature;
+ } else if (ArrayUtils.getLength(interfaces) == 1) {
+ left = interfaces[0];
+ } else {
+ left = superName;
+ }
+ key.setLeft(left);
+ }
+
+ @Override
+ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
+ if (Type.getType(Marker.class).getDescriptor().equals(desc)) {
+ valid.setValue(false);
+ }
+ return null;
+ }
+
+ @Override
+ public MethodVisitor visitMethod(int access, String name, String desc, String signature,
+ String[] exceptions) {
+ if ("<init>".equals(name)) {
+ return new MethodVisitor(Opcodes.ASM4) {
+ public void visitMethodInsn(int opcode, String owner, String name, String desc) {
+ if ("<init>".equals(name) && owner.equals(superName)) {
+ key.setRight(desc);
+ } else {
+ valid.setValue(false);
+ }
+ }
+
+ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
+ if ("this$0".equals(name) && opcode == Opcodes.PUTFIELD) {
+ mustRewriteConstructor.setValue(true);
+ return;
+ }
+ valid.setValue(false);
+ }
+ };
+ }
+ return null;
+ }
+ }, 0);
+ } finally {
+ IOUtils.closeQuietly(bytecode);
+ }
+ if (valid.booleanValue()) {
+ Set<ClassWrapper> set = classMap.get(key);
+ if (set == null) {
+ set = new LinkedHashSet<ClassWrapper>();
+ classMap.put(key, set);
+ }
+ set.add(new ClassWrapper(subtype, mustRewriteConstructor.booleanValue()));
+ } else {
+ env.debug("%s is ineligible for normalization due to %s", subtype,
+ IneligibilityReason.TOO_BUSY_CONSTRUCTOR);
+ }
+ }
+ return classMap;
+ }
+
+ /**
+ * Create the normalized version of a given class in the configured target package. The Normalizer will gladly
+ * create this in a package from which the normalized class will not actually be able to reference any types upon
+ * which it relies; in such a situation you must specify the target package as the package of the supertype.
+ *
+ * @param key used to generate the normalized classname.
+ * @param classWrapper
+ * @return the generated classname.
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ private String copy(final Pair<String, String> key, ClassWrapper classWrapper) throws IOException,
+ ClassNotFoundException {
+ final MessageDigest md5;
+ try {
+ md5 = MessageDigest.getInstance("MD5");
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException(e);
+ }
+ md5.update(key.getLeft().getBytes(UTF8));
+ md5.update(key.getRight().getBytes(UTF8));
+
+ final long digest = Utils.byteArrayToLong(md5.digest(), 0, 0L, 0, Long.SIZE / Byte.SIZE);
+
+ final String result = new StringBuilder(targetPackage).append("/$normalized").append(digest).toString();
+
+ final String className = classWrapper.wrapped.getName();
+ env.debug("Copying class %s to %s", className, result);
+
+ final InputStream bytecode = fileArchive.getBytecode(className);
+
+ try {
+ final ClassReader reader = new ClassReader(bytecode);
+
+ final ClassVisitor w = new WriteClass();
+
+ // we're doing most of this by hand; we only read the original class to hijack signature, ctor exceptions,
+ // etc.:
+
+ reader.accept(new ClassVisitor(Opcodes.ASM4) {
+ Type supertype;
+
+ @Override
+ public void visit(int version, int access, String name, String signature, String superName,
+ String[] interfaces) {
+ supertype = Type.getObjectType(superName);
+ w.visit(version, Opcodes.ACC_PUBLIC, result, signature, superName, interfaces);
+
+ visitAnnotation(Type.getType(Marker.class).getDescriptor(), false);
+ }
+
+ @Override
+ public MethodVisitor visitMethod(int access, String name, String desc, String signature,
+ String[] exceptions) {
+ if ("<init>".equals(name)) {
+
+ final Method staticCtor = new Method("<init>", key.getRight());
+ final Type[] argumentTypes = staticCtor.getArgumentTypes();
+ final Type[] exceptionTypes = toObjectTypes(exceptions);
+
+ {
+ final GeneratorAdapter mg =
+ new GeneratorAdapter(Opcodes.ACC_PUBLIC, staticCtor, signature, exceptionTypes, w);
+ mg.visitCode();
+ mg.loadThis();
+ for (int i = 0; i < argumentTypes.length; i++) {
+ mg.loadArg(i);
+ }
+ mg.invokeConstructor(supertype, staticCtor);
+ mg.returnValue();
+ mg.endMethod();
+ }
+ /*
+ * now declare a dummy constructor that will match, and discard,
+ * any originally inner-class bound constructor i.e. that set up a this$0 field.
+ * By doing this we can avoid playing with the stack that originally
+ * invoked such a constructor and simply rewrite the method
+ */
+ {
+ final Method instanceCtor =
+ new Method("<init>", Type.VOID_TYPE, ArrayUtils.add(argumentTypes, 0, OBJECT_TYPE));
+ final GeneratorAdapter mg =
+ new GeneratorAdapter(Opcodes.ACC_PUBLIC, instanceCtor, signature, exceptionTypes, w);
+ mg.visitCode();
+ mg.loadThis();
+ for (int i = 0; i < argumentTypes.length; i++) {
+ mg.loadArg(i + 1);
+ }
+ mg.invokeConstructor(supertype, staticCtor);
+ mg.returnValue();
+ mg.endMethod();
+ }
+ return null;
+ }
+ return null;
+ }
+
+ @Override
+ public void visitEnd() {
+ w.visitEnd();
+ }
+ }, 0);
+ } finally {
+ IOUtils.closeQuietly(bytecode);
+ }
+ return result;
+ }
+
+ private Type[] toObjectTypes(String[] types) {
+ if (types == null) {
+ return null;
+ }
+ final Type[] result = new Type[types.length];
+ for (int i = 0; i < types.length; i++) {
+ result[i] = Type.getObjectType(types[i]);
+ }
+ return result;
+ }
+}
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java
------------------------------------------------------------------------------
svn:executable = *
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/NormalizerWeaver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/NormalizerWeaver.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/NormalizerWeaver.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/NormalizerWeaver.java Sun Aug 11 17:48:37 2013
@@ -0,0 +1,36 @@
+/*
+ * 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.normalizer;
+
+import org.apache.commons.weaver.model.Scanner;
+import org.apache.commons.weaver.model.WeaveEnvironment;
+import org.apache.commons.weaver.spi.Weaver;
+
+/**
+ * The purpose of the normalizer module is to merge identical anonymous class definitions into a single type, thereby
+ * "normalizing" them and reducing their collective footprint on your archive and more importantly on your JVM.
+ */
+public class NormalizerWeaver implements Weaver {
+
+ @Override
+ public boolean process(WeaveEnvironment environment, Scanner scanner) {
+ return new Normalizer(environment).normalize(scanner);
+ }
+
+}
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/NormalizerWeaver.java
------------------------------------------------------------------------------
svn:executable = *
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Utils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Utils.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Utils.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Utils.java Sun Aug 11 17:48:37 2013
@@ -0,0 +1,109 @@
+/*
+ * 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.normalizer;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import org.apache.commons.lang3.ClassUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.Validate;
+
+class Utils {
+
+ static String validatePackageName(String pkg) {
+ if (StringUtils.isBlank(pkg)) {
+ return "";
+ }
+ pkg = pkg.trim();
+
+ final String unexpected = "Unexpected character %s at pos %s of package name \"%s\"";
+
+ boolean next = true;
+ for (int pos = 0; pos < pkg.length(); pos++) {
+ final char c = pkg.charAt(pos);
+ if (next) {
+ next = false;
+ Validate.isTrue(Character.isJavaIdentifierStart(c), unexpected, c, pos, pkg);
+ continue;
+ }
+ if (c == '/' || c == '.') {
+ next = true;
+ continue;
+ }
+ Validate.isTrue(Character.isJavaIdentifierPart(c), unexpected, c, pos, pkg);
+ }
+
+ pkg = pkg.replace('.', '/');
+ final int last = pkg.length() - 1;
+ if (pkg.charAt(last) == '/') {
+ pkg = pkg.substring(0, last);
+ }
+ return pkg;
+ }
+
+ static Set<Class<?>> parseTypes(String types, ClassLoader cl) {
+ final Set<Class<?>> result = new LinkedHashSet<Class<?>>();
+ for (String s : StringUtils.splitByWholeSeparatorPreserveAllTokens(types, ",")) {
+ try {
+ result.add(ClassUtils.getClass(cl, s.trim().replace('/', '.')));
+ } catch (ClassNotFoundException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * From Commons lang 3.2; use when available.
+ * <p>
+ * Converts an array of byte into a long using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ * </p>
+ *
+ * @param src the byte array to convert
+ * @param srcPos the position in {@code src}, in byte unit, from where to start the
+ * conversion
+ * @param dstInit initial value of the destination long
+ * @param dstPos the position of the lsb, in bits, in the result long
+ * @param nBytes the number of bytes to convert
+ * @return a long containing the selected bits
+ * @throws NullPointerException if {@code src} is {@code null}
+ * @throws IllegalArgumentException if {@code (nBytes-1)*8+dstPos >= 64}
+ * @throws ArrayIndexOutOfBoundsException if {@code srcPos + nBytes > src.length}
+ */
+ public static long byteArrayToLong(final byte[] src, final int srcPos, final long dstInit, final int dstPos,
+ final int nBytes) {
+ if ((src.length == 0 && srcPos == 0) || 0 == nBytes) {
+ return dstInit;
+ }
+ if ((nBytes - 1) * 8 + dstPos >= 64) {
+ throw new IllegalArgumentException(
+ "(nBytes-1)*8+dstPos is greather or equal to than 64");
+ }
+ long out = dstInit;
+ int shift = 0;
+ for (int i = 0; i < nBytes; i++ ) {
+ shift = i * 8 + dstPos;
+ final long bits = (0xffL & src[i + srcPos]) << shift;
+ final long mask = 0xffL << shift;
+ out = (out & ~mask) | bits;
+ }
+ return out;
+ }
+
+}
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/java/org/apache/commons/weaver/normalizer/Utils.java
------------------------------------------------------------------------------
svn:executable = *
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver Sun Aug 11 17:48:37 2013
@@ -0,0 +1,19 @@
+# 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.
+
+# this class gets picked up by the WeaveProcessor
+org.apache.commons.weaver.normalizer.NormalizerWeaver
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver
------------------------------------------------------------------------------
svn:executable = *
Added: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/weaver/normalizer/UtilsTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/weaver/normalizer/UtilsTest.java?rev=1512978&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/weaver/normalizer/UtilsTest.java (added)
+++ commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/weaver/normalizer/UtilsTest.java Sun Aug 11 17:48:37 2013
@@ -0,0 +1,100 @@
+/*
+ * 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.normalizer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.junit.Test;
+
+public class UtilsTest {
+ @Test
+ public void testValidatePackageName() {
+ assertEquals("", Utils.validatePackageName(""));
+ assertEquals("", Utils.validatePackageName(" "));
+ assertEquals("foo", Utils.validatePackageName("foo"));
+ assertEquals("foo/bar", Utils.validatePackageName("foo.bar"));
+ assertEquals("foo/bar", Utils.validatePackageName("foo/bar"));
+ assertEquals("foo/bar/baz", Utils.validatePackageName("foo.bar.baz"));
+ assertEquals("foo/bar/baz", Utils.validatePackageName("foo.bar/baz"));
+ assertEquals("foo/bar/baz", Utils.validatePackageName("foo/bar.baz"));
+ assertEquals("foo/bar/baz", Utils.validatePackageName("foo/bar/baz"));
+ assertEquals("$foo", Utils.validatePackageName("$foo"));
+ assertEquals("_foo", Utils.validatePackageName("_foo"));
+ assertEquals("foo2", Utils.validatePackageName("foo2"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testValidatePackageNameStartsWithDigit() {
+ Utils.validatePackageName("2foo");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testValidatePackageNameEmbeddedWhitespace() {
+ Utils.validatePackageName("foo bar");
+ }
+
+ @Test
+ public void testParseTypes() {
+ assertContainsInOrder(Utils.parseTypes(" java.lang.Number ", getClass().getClassLoader()), Number.class);
+ assertContainsInOrder(
+ Utils.parseTypes("java.lang.Number,java.lang.String,java.util.Map", getClass().getClassLoader()),
+ Number.class, String.class, Map.class);
+ assertContainsInOrder(
+ Utils.parseTypes("java.lang.Number, java.lang.String, java.util.Map", getClass().getClassLoader()),
+ Number.class, String.class, Map.class);
+ assertContainsInOrder(
+ Utils.parseTypes("java/lang/Number, java/lang/String, java/util/Map", getClass().getClassLoader()),
+ Number.class, String.class, Map.class);
+ assertContainsInOrder(
+ Utils.parseTypes("java.lang.Number,\njava.lang.String,\njava.util.Map", getClass().getClassLoader()),
+ Number.class, String.class, Map.class);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testParseUnknownType() {
+ Utils.parseTypes("gobbledygook", getClass().getClassLoader());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testParseMissingFirstType() {
+ Utils.parseTypes(",java.lang.Object", getClass().getClassLoader());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testParseMissingLastType() {
+ Utils.parseTypes("java.lang.Object,", getClass().getClassLoader());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testParseMissingType() {
+ Utils.parseTypes("java.lang.Object,,java.lang.Iterable", getClass().getClassLoader());
+ }
+
+ <E> void assertContainsInOrder(Iterable<E> iterable, E... expectedElements) {
+ final Iterator<E> iterator = iterable.iterator();
+ for (E e : expectedElements) {
+ assertTrue(iterator.hasNext());
+ assertEquals(e, iterator.next());
+ }
+ assertFalse(iterator.hasNext());
+ }
+}
Propchange: commons/sandbox/weaver/branches/normalizer/modules/normalizer/weaver/src/test/java/org/apache/commons/weaver/normalizer/UtilsTest.java
------------------------------------------------------------------------------
svn:executable = *
Modified: commons/sandbox/weaver/branches/normalizer/modules/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/normalizer/modules/pom.xml?rev=1512978&r1=1512977&r2=1512978&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/normalizer/modules/pom.xml (original)
+++ commons/sandbox/weaver/branches/normalizer/modules/pom.xml Sun Aug 11 17:48:37 2013
@@ -1,4 +1,4 @@
-<?xml version="1.0"?>
+<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
@@ -16,8 +16,7 @@ software distributed under the License i
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
--->
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>commons-weaver-parent</artifactId>
@@ -31,6 +30,7 @@ under the License.
<name>Commons Weaver Modules aggregator project</name>
<modules>
<module>privilizer</module>
+ <module>normalizer</module>
</modules>
<dependencies>
<dependency>