From 0c97c866ce45efbaa523fa06e1540f7545d0d246 Mon Sep 17 00:00:00 2001 From: mikaelpeltier Date: Tue, 10 Mar 2015 11:00:47 +0100 Subject: Add a service provider for JackConfigProvider Change-Id: Ib599f5b012aec9d15c71fd314fe859628e855c00 --- .../src/com/android/jack/api/example/Main.java | 101 -------------------- .../jack/api/example/SampleWithClassLoader.java | 102 +++++++++++++++++++++ .../jack/api/example/SampleWithServiceLoader.java | 39 ++++++++ 3 files changed, 141 insertions(+), 101 deletions(-) delete mode 100644 jack-api/src/com/android/jack/api/example/Main.java create mode 100644 jack-api/src/com/android/jack/api/example/SampleWithClassLoader.java create mode 100644 jack-api/src/com/android/jack/api/example/SampleWithServiceLoader.java (limited to 'jack-api/src/com/android/jack/api') diff --git a/jack-api/src/com/android/jack/api/example/Main.java b/jack-api/src/com/android/jack/api/example/Main.java deleted file mode 100644 index fe6d7ab8..00000000 --- a/jack-api/src/com/android/jack/api/example/Main.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * 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 com.android.jack.api.example; - -import com.android.jack.api.ConfigNotSupportedException; -import com.android.jack.api.JackConfig; -import com.android.jack.api.JackConfigProvider; -import com.android.jack.api.v01.AbortException; -import com.android.jack.api.v01.Api01CompilationTask; -import com.android.jack.api.v01.Api01Config; -import com.android.jack.api.v01.ConfigurationException; -import com.android.jack.api.v01.UnrecoverableException; - -import java.io.File; -import java.lang.reflect.InvocationTargetException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; - -/** - * STOPSHIP - */ -public class Main { - public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, - SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, - IllegalAccessException, InvocationTargetException { - ClassLoader loader = - URLClassLoader.newInstance(new URL[] {new File( - "").toURI().toURL()}, - Main.class.getClassLoader()); - - Class confProviderClass = - Class.forName(JackConfigProvider.CLASS_NAME, true, loader).asSubclass( - JackConfigProvider.class); - - JackConfigProvider confProvider = confProviderClass.getConstructor().newInstance(); - - System.out.println("Jack version: " + confProvider.getCompilerVersion() + " '" - + confProvider.getCompilerCodeName() + "' (" + confProvider.getCompilerBuildId() + " " - + confProvider.getCompilerCodeBase() + ")"); - - System.out.println("Supported configs: "); - for (Class cls : confProvider.getSupportedConfigs()) { - System.out.print(cls.getSimpleName() + " "); - } - System.out.println(); - - Api01CompilationTask compilationTask; - Api01Config config; - - // Get configuration object - try { - config = confProvider.getConfig(Api01Config.class); - } catch (ConfigNotSupportedException e1) { - System.err.println("Brest config not supported)"); - return; - } - - // Configure the compiler - try { - // Set standard options - config.setOutputDexDir(new File("out/")); - config.setJarJarConfigFile(new File("rules.jarjar")); - // Set provisional properties - config.setProperty("jack.internal.test", "true"); - // Check and build compiler - compilationTask = config.getTask(); - } catch (ConfigurationException e) { - System.err.println(e.getMessage()); - return; - } - - // Run the compilation - try { - compilationTask.run(); - } catch (AbortException e) { - System.out.println("User error, see reporter"); - return; - } catch (UnrecoverableException e) { - System.out.println("Something out of Jack control has happen: " + e.getMessage()); - return; - } catch (ConfigurationException e) { - System.err.println(e.getMessage()); - return; - } - } -} diff --git a/jack-api/src/com/android/jack/api/example/SampleWithClassLoader.java b/jack-api/src/com/android/jack/api/example/SampleWithClassLoader.java new file mode 100644 index 00000000..f2b3040d --- /dev/null +++ b/jack-api/src/com/android/jack/api/example/SampleWithClassLoader.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * 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 com.android.jack.api.example; + +import com.android.jack.api.ConfigNotSupportedException; +import com.android.jack.api.JackConfig; +import com.android.jack.api.JackConfigProvider; +import com.android.jack.api.v01.AbortException; +import com.android.jack.api.v01.Api01CompilationTask; +import com.android.jack.api.v01.Api01Config; +import com.android.jack.api.v01.ConfigurationException; +import com.android.jack.api.v01.UnrecoverableException; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; + +/** + * STOPSHIP + */ +public class SampleWithClassLoader { + + public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, + SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, + IllegalAccessException, InvocationTargetException { + ClassLoader loader = + URLClassLoader.newInstance(new URL[] {new File( + "").toURI().toURL()}, + SampleWithClassLoader.class.getClassLoader()); + + Class confProviderClass = + Class.forName(JackConfigProvider.CLASS_NAME, true, loader).asSubclass( + JackConfigProvider.class); + + JackConfigProvider confProvider = confProviderClass.getConstructor().newInstance(); + + System.out.println("Jack version: " + confProvider.getCompilerVersion() + " '" + + confProvider.getCompilerCodeName() + "' (" + confProvider.getCompilerBuildId() + " " + + confProvider.getCompilerCodeBase() + ")"); + + System.out.println("Supported configs: "); + for (Class cls : confProvider.getSupportedConfigs()) { + System.out.print(cls.getSimpleName() + " "); + } + System.out.println(); + + Api01CompilationTask compilationTask; + Api01Config config; + + // Get configuration object + try { + config = confProvider.getConfig(Api01Config.class); + } catch (ConfigNotSupportedException e1) { + System.err.println("Brest config not supported)"); + return; + } + + // Configure the compiler + try { + // Set standard options + config.setOutputDexDir(new File("out/")); + config.setJarJarConfigFile(new File("rules.jarjar")); + // Set provisional properties + config.setProperty("jack.internal.test", "true"); + // Check and build compiler + compilationTask = config.getTask(); + } catch (ConfigurationException e) { + System.err.println(e.getMessage()); + return; + } + + // Run the compilation + try { + compilationTask.run(); + } catch (AbortException e) { + System.out.println("User error, see reporter"); + return; + } catch (UnrecoverableException e) { + System.out.println("Something out of Jack control has happen: " + e.getMessage()); + return; + } catch (ConfigurationException e) { + System.err.println(e.getMessage()); + return; + } + } +} diff --git a/jack-api/src/com/android/jack/api/example/SampleWithServiceLoader.java b/jack-api/src/com/android/jack/api/example/SampleWithServiceLoader.java new file mode 100644 index 00000000..995bd9df --- /dev/null +++ b/jack-api/src/com/android/jack/api/example/SampleWithServiceLoader.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * 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 com.android.jack.api.example; + +import com.android.jack.api.JackConfigProvider; + +import java.util.NoSuchElementException; +import java.util.ServiceLoader; + +/** + * Sample of Jack api usage based on a service provider. + * This sample requires jack.jar on classpath. + */ +public class SampleWithServiceLoader { + + public static void main(String[] args) throws SecurityException, IllegalArgumentException { + ServiceLoader serviceLoader = ServiceLoader.load(JackConfigProvider.class); + try { + JackConfigProvider confProvider = serviceLoader.iterator().next(); + System.out.println("Jack version: " + confProvider.getCompilerVersion()); + } catch (NoSuchElementException e) { + System.out.println("Check that jack.jar is on classpath"); + } + } +} -- cgit v1.2.3