aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1389125 - in /aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool: Main.java StaticToolClassWriter.java
Date Sun, 23 Sep 2012 19:18:23 GMT
Author: davidb
Date: Sun Sep 23 19:18:22 2012
New Revision: 1389125

URL: http://svn.apache.org/viewvc?rev=1389125&view=rev
Log:
Classloading fix to the static tool.

Added:
    aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/StaticToolClassWriter.java
Modified:
    aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java

Modified: aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java?rev=1389125&r1=1389124&r2=1389125&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
(original)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
Sun Sep 23 19:18:22 2012
@@ -26,7 +26,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
-import java.util.Map;
+import java.net.URLClassLoader;
 import java.util.Properties;
 import java.util.Set;
 import java.util.jar.Attributes;
@@ -41,7 +41,6 @@ import org.apache.aries.spifly.Streams;
 import org.apache.aries.spifly.Util;
 import org.apache.aries.spifly.WeavingData;
 import org.apache.aries.spifly.weaver.TCCLSetterVisitor;
-import org.apache.aries.util.manifest.ManifestHeaderProcessor.GenericMetadata;
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.ClassWriter;
 import org.osgi.framework.Constants;
@@ -119,29 +118,6 @@ public class Main {
         delTree(tempDir);
     }
 
-    private static String serializeRequirement(GenericMetadata req) {
-        StringBuilder serialized = new StringBuilder();
-
-        serialized.append(req.getNamespace());
-        serialized.append(';');
-        for (Map.Entry<String, Object> attr : req.getAttributes().entrySet()) {
-            serialized.append(attr.getKey());
-            serialized.append('=');
-            serialized.append(attr.getValue());
-            serialized.append(';');
-        }
-
-        for (Map.Entry<String, String> dir : req.getDirectives().entrySet()) {
-            serialized.append(dir.getKey());
-            serialized.append(":=");
-            serialized.append(dir.getValue());
-            serialized.append(';');
-        }
-        serialized.deleteCharAt(serialized.length() -1);
-        serialized.append(',');
-        return serialized.toString();
-    }
-
     private static void extendImportPackage(Manifest manifest) throws IOException {
         String utilPkgVersion = getPackageVersion(Util.class);
 
@@ -190,6 +166,7 @@ public class Main {
     private static void weaveDir(File dir, String consumerHeaderKey, String consumerHeaderValue,
String bundleClassPath) throws Exception {
         Set<WeavingData> wd = ConsumerHeaderProcessor.processHeader(consumerHeaderKey,
consumerHeaderValue);
 
+        URLClassLoader cl = new URLClassLoader(new URL [] {dir.toURI().toURL()}, Main.class.getClassLoader());
         String dirName = dir.getAbsolutePath();
 
         DirTree dt = new DirTree(dir);
@@ -207,7 +184,7 @@ public class Main {
             byte[] b;
             try {
                 ClassReader cr = new ClassReader(is);
-                ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
+                ClassWriter cw = new StaticToolClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES,
cl);
                 TCCLSetterVisitor cv = new TCCLSetterVisitor(cw, className, wd);
                 cr.accept(cv, ClassReader.SKIP_FRAMES);
                 if (cv.isWoven()) {

Added: aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/StaticToolClassWriter.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/StaticToolClassWriter.java?rev=1389125&view=auto
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/StaticToolClassWriter.java
(added)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/StaticToolClassWriter.java
Sun Sep 23 19:18:22 2012
@@ -0,0 +1,72 @@
+/*
+ * 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.aries.spifly.statictool;
+
+import org.objectweb.asm.ClassWriter;
+
+/**
+ * We need to override ASM's default behaviour in
+ * {@link #getCommonSuperClass(String, String)} so that it accepts a custom
+ * classloader that can also see the jar that is being processed.
+ */
+public final class StaticToolClassWriter extends ClassWriter {
+
+    private static final String OBJECT_INTERNAL_NAME = "java/lang/Object";
+    private final ClassLoader loader;
+
+    public StaticToolClassWriter(int flags, ClassLoader loader) {
+        super(flags);
+
+        this.loader = loader;
+    }
+
+    /**
+     * The implementation uses the classloader provided using the Constructor.
+     *
+     * This is a slight variation on ASM's default behaviour as that obtains the
+     * classloader to use ASMs classloader.
+     */
+    @Override
+    protected String getCommonSuperClass(final String type1, final String type2) {
+        Class<?> c, d;
+        try {
+            c = Class.forName(type1.replace('/', '.'), false, loader);
+            d = Class.forName(type2.replace('/', '.'), false, loader);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        if (c.isAssignableFrom(d)) {
+            return type1;
+        }
+        if (d.isAssignableFrom(c)) {
+            return type2;
+        }
+
+        if (c.isInterface() || d.isInterface()) {
+            return OBJECT_INTERNAL_NAME;
+        }
+
+        do {
+            c = c.getSuperclass();
+        } while (!c.isAssignableFrom(d));
+
+        return c.getName().replace('.', '/');
+    }
+}



Mime
View raw message