Commits

Tim Vernum committed db13edf

Support class (path) loading in conversion tool

Comments (0)

Files changed (10)

 cd $(dirname $0)
 cd ..
 
+export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/"
+
 CP="parser/output/ant/classes/parser:convert/output/ant/classes/main"
 for JAR in lib/core/*.jar
 do
     CP="${CP}:${JAR}"
 done
 
+for JAR in lib/samples/*.jar
+do
+    if [ -z "${CP2}" ]
+    then
+        CP2="${JAR}"
+    else
+        CP2="${CP2}:${JAR}"
+    fi
+done
+
 MAIN="org.adjective.syntactic.convert.j8to7.Main"
 
 OUTPUT="output/convert"
 
 for FILE in "$@"
 do
-    java -cp "${CP}" "${MAIN}" --source "${FILE}" --output "${OUTPUT}"
+    java -cp "${CP}" "${MAIN}" --source "${FILE}" --output "${OUTPUT}" --classpath "${CP2}"
     if [ -f ${FILE} ]
     then
         show "${OUTPUT}/$( basename ${FILE} )"

convert/source/java/main/org/adjective/syntactic/convert/j8to7/AbstractASTConverter.java

         final TypeInfo typeInfo = _classFinder.getClass(name, getTypeParameters(forType));
         if (typeInfo == null)
         {
-            throw new ConversionException("Cannot find '" + name + "'");
+            throw new ConversionException("Cannot find '" + name + "' in " + _classFinder);
         }
         return typeInfo;
     }

convert/source/java/main/org/adjective/syntactic/convert/j8to7/Convert8To7Visitor.java

 package org.adjective.syntactic.convert.j8to7;
 
 import org.adjective.syntactic.convert.j8to7.util.ClassFinder;
+import org.adjective.syntactic.convert.j8to7.util.ClassPath;
 import org.adjective.syntactic.convert.j8to7.util.Importer;
 import org.adjective.syntactic.convert.j8to7.util.VariableStack;
 import org.adjective.syntactic.parser.ast.*;
 import org.adjective.syntactic.parser.util.ModifierSet;
 import org.adjective.syntactic.util.Pair;
 
+import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
         _toMarkFinal = new ArrayList<Pair<VariableDeclarationNode, String>>();
     }
 
+    public void setClassPath(ClassPath cp)
+    {
+        _classFinder.setClassLoader(new URLClassLoader(cp.getUrls()));
+    }
+
     public void convert(final ASTCompilationUnit tree)
     {
         if (tree == null)

convert/source/java/main/org/adjective/syntactic/convert/j8to7/Main.java

 
 package org.adjective.syntactic.convert.j8to7;
 
+import org.adjective.syntactic.convert.j8to7.util.ClassPath;
 import org.adjective.syntactic.parser.jj.JavaParser;
 import org.adjective.syntactic.parser.node.BaseNode;
 import org.adjective.syntactic.parser.util.PrintSourceVisitor;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.PrintStream;
 
 public class Main
     @Option(name = "--flatten", aliases = {"-f"}, usage = "Flatten directory structure", required = false)
     private boolean _flatten = false;
 
-    public static void main(String[] args) throws CmdLineException, FileNotFoundException
+    public static void main(String[] args) throws CmdLineException, IOException
     {
         final Main main = new Main();
         final CmdLineParser parser = new CmdLineParser(main);
         main.execute();
     }
 
-    public void execute() throws FileNotFoundException
+    public void execute() throws IOException
     {
         if (!_source.exists())
         {
         }
     }
 
-    private void processDirectory(final File source, final File output) throws FileNotFoundException
+    private void processDirectory(final File source, final File output) throws IOException
     {
         final File[] files = source.listFiles();
         if (files == null)
         }
     }
 
-    private void processFile(final File source, File output) throws FileNotFoundException
+    private void processFile(final File source, File output) throws IOException
     {
         if (output.isDirectory())
         {
         PrintStream out = new PrintStream(new FileOutputStream(output));
         try
         {
-            ast.jjtAccept(new Convert8To7Visitor(), null);
+            final Convert8To7Visitor converter = new Convert8To7Visitor();
+            if (_classpath != null)
+            {
+                converter.setClassPath(new ClassPath(_classpath));
+            }
+            ast.jjtAccept(converter, null);
             ast.jjtAccept(new PrintSourceVisitor(), out);
         }
         finally

convert/source/java/main/org/adjective/syntactic/convert/j8to7/util/ClassFinder.java

 import org.adjective.syntactic.parser.ast.ASTClassOrInterfaceDeclaration;
 import org.adjective.syntactic.parser.name.TypeParameter;
 
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.HashMap;
 import java.util.Map;
 
 public class ClassFinder
 {
-    private Map<String, ASTClassOrInterfaceDeclaration> _localTypes;
+    private final Map<String, ASTClassOrInterfaceDeclaration> _localTypes;
+    private ClassLoader _classLoader;
 
     public ClassFinder()
     {
         _localTypes = new HashMap<String, ASTClassOrInterfaceDeclaration>();
+        _classLoader = getClass().getClassLoader();
+    }
+
+    public void setClassLoader(ClassLoader classLoader)
+    {
+        this._classLoader = classLoader;
     }
 
     public boolean hasClass(final String qualifiedName)
         return new ClassInfo(cls, parameters);
     }
 
-    private Class<?> findClass(final String qualifiedName)
+    private Class<?> findClass(String qualifiedName)
+    {
+        for (; ; )
+        {
+            final Class<?> cls = loadClass(qualifiedName);
+            if (cls != null)
+            {
+                return cls;
+            }
+            int dot = qualifiedName.lastIndexOf('.');
+            if (dot == -1)
+            {
+                return null;
+            }
+            qualifiedName = qualifiedName.substring(0, dot) + '$' + qualifiedName.substring(dot + 1);
+        }
+    }
+
+    private Class<?> loadClass(final String qualifiedName)
     {
         try
         {
-            return Class.forName(qualifiedName);
+            return _classLoader.loadClass(qualifiedName);
         }
         catch (ClassNotFoundException e)
         {
     {
         _localTypes.put(node.getName().getIdentifier(), node);
     }
+
+    @Override
+    public String toString()
+    {
+        return getClass().getSimpleName() + "//" + describe(_classLoader);
+    }
+
+    private CharSequence describe(final ClassLoader classLoader)
+    {
+        if (classLoader instanceof URLClassLoader)
+        {
+            URLClassLoader ucl = (URLClassLoader) classLoader;
+            StringBuilder description = new StringBuilder(ucl.getClass().getSimpleName());
+            description.append('~');
+            for (URL url : ucl.getURLs())
+            {
+                description.append(url);
+                description.append(',');
+            }
+            return description;
+        }
+        return classLoader.toString();
+    }
 }

convert/source/java/main/org/adjective/syntactic/convert/j8to7/util/ClassPath.java

+/* ------------------------------------------------------------------------
+ * Copyright 2013 Tim Vernum
+ * ------------------------------------------------------------------------
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.adjective.syntactic.convert.j8to7.util;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class ClassPath
+{
+    private final URL[] _urls;
+
+    public ClassPath(String classpath) throws MalformedURLException
+    {
+        final String[] split = classpath.split(":");
+        _urls = new URL[split.length];
+        for (int i = 0; i < split.length; i++)
+        {
+            _urls[i] = getUrl(split[i]);
+        }
+    }
+
+    private URL getUrl(final String s) throws MalformedURLException
+    {
+        return new File(s).toURI().toURL();
+    }
+
+    public URL[] getUrls()
+    {
+        return _urls;
+    }
+}
Add a comment to this file

convert/source/java/samples/sample/GenericLambda$Transformer.class

Binary file added.

Add a comment to this file

convert/source/java/samples/sample/GenericLambda.class

Binary file added.

convert/source/java/samples/sample/GwtCommand.java

+/* ------------------------------------------------------------------------
+ * Copyright 2013 Tim Vernum
+ * ------------------------------------------------------------------------
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ * ------------------------------------------------------------------------
+ */
+
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.core.client.Scheduler.ScheduledCommand;
+
+public class GwtCommand
+{
+    public void start()
+    {
+        final ScheduledCommand command = () -> {
+            System.out.println("deferred");
+        };
+        Scheduler.get().scheduleDeferred(command);
+    }
+}

convert/source/java/samples/sample/VertxHandler.java

+/* ------------------------------------------------------------------------
+ * Copyright 2013 Tim Vernum
+ * ------------------------------------------------------------------------
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ * ------------------------------------------------------------------------
+ */
+
+import org.vertx.java.core.Handler;
+import org.vertx.java.core.eventbus.EventBus;
+import org.vertx.java.core.eventbus.Message;
+
+public class VertxHandler {
+
+    public void register(EventBus bus) {
+        final Handler<Message<String>> handler = msg -> msg.reply("Hello " + msg.body);
+        bus.registerHandler("hello.me", handler);
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.