diff options
author | limpbizkit <limpbizkit@d779f126-a31b-0410-b53b-1d3aecad763e> | 2008-11-02 05:18:04 +0000 |
---|---|---|
committer | limpbizkit <limpbizkit@d779f126-a31b-0410-b53b-1d3aecad763e> | 2008-11-02 05:18:04 +0000 |
commit | 43a8b0e3475a2cc4e84930de8ebde4820977c199 (patch) | |
tree | 63f319128a5e4f439320ceb74c2d9853ec328caf /extensions | |
parent | 06898069ffc810f4dd71ef9aa8b63e2b67cacb8f (diff) | |
download | guice-43a8b0e3475a2cc4e84930de8ebde4820977c199.tar.gz |
Removing compiletime. This is obsoleted by GIN.
git-svn-id: https://google-guice.googlecode.com/svn/trunk@653 d779f126-a31b-0410-b53b-1d3aecad763e
Diffstat (limited to 'extensions')
7 files changed, 0 insertions, 537 deletions
diff --git a/extensions/compiletime/build.properties b/extensions/compiletime/build.properties deleted file mode 100644 index 5c07e400..00000000 --- a/extensions/compiletime/build.properties +++ /dev/null @@ -1,5 +0,0 @@ -lib.dir=../../lib -src.dir=src -test.dir=test -build.dir=build -test.class=com.google.inject.CompileTimeGuiceTest diff --git a/extensions/compiletime/build.xml b/extensions/compiletime/build.xml deleted file mode 100644 index 2315bedb..00000000 --- a/extensions/compiletime/build.xml +++ /dev/null @@ -1,22 +0,0 @@ -<?xml version="1.0"?> - -<project name="guice-compiletime" basedir="." default="jar"> - - <import file="../../common.xml"/> - - <path id="compile.classpath"> - <fileset dir="${lib.dir}" includes="*.jar"/> - <fileset dir="${lib.dir}/build" includes="*.jar"/> - <fileset dir="../../build/dist" includes="*.jar"/> - </path> - - <target name="jar" depends="compile, manifest" - description="Build jar."> - <mkdir dir="${build.dir}"/> - <jar destfile="${build.dir}/${ant.project.name}-${version}.jar" - manifest="${build.dir}/META-INF/MANIFEST.MF"> - <fileset dir="${build.dir}/classes"/> - </jar> - </target> - -</project> diff --git a/extensions/compiletime/compiletime.iml b/extensions/compiletime/compiletime.iml deleted file mode 100644 index 64bbec6a..00000000 --- a/extensions/compiletime/compiletime.iml +++ /dev/null @@ -1,18 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<module relativePaths="true" type="JAVA_MODULE" version="4"> - <component name="NewModuleRootManager" inherit-compiler-output="true"> - <exclude-output /> - <content url="file://$MODULE_DIR$"> - <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> - <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" /> - </content> - <content url="file://$MODULE_DIR$/../../generatedsrc"> - <sourceFolder url="file://$MODULE_DIR$/../../generatedsrc" isTestSource="true" /> - </content> - <orderEntry type="inheritedJdk" /> - <orderEntry type="sourceFolder" forTests="false" /> - <orderEntry type="module" module-name="guice" /> - <orderEntryProperties /> - </component> -</module> - diff --git a/extensions/compiletime/src/com/google/inject/CodeGenReflectionFactory.java b/extensions/compiletime/src/com/google/inject/CodeGenReflectionFactory.java deleted file mode 100644 index 142ae1d8..00000000 --- a/extensions/compiletime/src/com/google/inject/CodeGenReflectionFactory.java +++ /dev/null @@ -1,205 +0,0 @@ -/** - * Copyright (C) 2008 Google Inc. - * - * 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.google.inject; - -import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.collect.Maps; -import com.google.inject.internal.Errors; -import com.google.inject.internal.ErrorsException; -import com.google.inject.spi.Dependency; -import java.io.File; -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Member; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -/** - * Reflection that writes reflected data to a generated class. - * - * @author jessewilson@google.com (Jesse Wilson) - */ -class CodeGenReflectionFactory implements Reflection.Factory { - private static final String generatedCodePackage = "com.google.inject"; - private final String name; - private Map<Class<?>, ConstructionProxy<?>> constructionProxies = Maps.newHashMap(); - - /** - * @param name uniquely identifies this reflection instance. This name needs - * to be used both at code generation time and then later at runtime. - */ - public CodeGenReflectionFactory(String name) { - this.name = name; - } - - public Reflection create(ConstructionProxyFactory constructionProxyFactory) { - Reflection delegate = new RuntimeReflectionFactory().create(constructionProxyFactory); - return new CodeGenReflection(delegate); - } - - - public Reflection.Factory getRuntimeReflectionFactory() { - final Reflection reflection; - try { - reflection = (Reflection) Class.forName(getGeneratedClassName()).newInstance(); - } catch (Exception e) { - throw new IllegalStateException("Failed to build reflection class for \"" - + name + "\", has code been generated yet?"); - } - - return new Reflection.Factory() { - public Reflection create(ConstructionProxyFactory constructionProxyFactory) { - return reflection; - } - }; - } - - private String getGeneratedClassName() { - return generatedCodePackage + "." + generatedClassSimpleName(); - } - - private class CodeGenReflection implements Reflection { - private final Reflection delegate; - - private CodeGenReflection(Reflection delegate) { - this.delegate = checkNotNull(delegate, "delegate"); - } - - public <T> ConstructionProxy<T> getConstructionProxy(Errors errors, Class<T> implementation) - throws ErrorsException { - ConstructionProxy<T> result = delegate.getConstructionProxy(errors, implementation); - constructionProxies.put(implementation, result); - return result; - } - } - - /** - * Writes generated .java files to {@code generatedSourceDirectory}. - */ - void writeToFile(File generatedSourceDirectory) throws IOException { - JavaCodeGenerator out = JavaCodeGenerator.open(generatedSourceDirectory, - generatedCodePackage, generatedClassSimpleName()); - new ClassWriter(out).writeClass(); - out.close(); - } - - private String generatedClassSimpleName() { - return "Generated_" + name; - } - - private class ClassWriter { - final JavaCodeGenerator out; - - ClassWriter(JavaCodeGenerator out) { - this.out = out; - } - - void writeClass() throws IOException { - out.writePackageHeader(); - out.writeImport(Reflection.class); - out.writeImport(ConstructionProxy.class); - out.writeImport(InvocationTargetException.class); - out.writeImport(Dependency.class); - out.writeImport(List.class); - out.writeImport(Member.class); - out.writeImport(Arrays.class); - out.writeImport(SuppressWarnings.class); - out.writeImport(Key.class); - out.writeImport(IllegalArgumentException.class); - out.writeImport(Object.class); - for (Map.Entry<Class<?>, ConstructionProxy<?>> entry : constructionProxies.entrySet()) { - out.writeImport(entry.getKey()); - for (Dependency<?> dependency : entry.getValue().getInjectionPoint().getDependencies()) { - out.writeImport(dependency.getKey().getTypeLiteral().getType()); - } - } - - out.writeLine() - .openScope("public class %s implements %s {", generatedClassSimpleName(), Reflection.class) - .writeLine(); - - writeGetConstructionProxy(); - - out.writeLine() - .closeScope("}"); - } - - String keyLiteral(Key<?> key) { - if (!(key.getTypeLiteral().getType() instanceof Class)) { - throw new UnsupportedOperationException("TODO"); - } - if (key.getAnnotationType() != null) { - throw new UnsupportedOperationException("TODO"); - } - return String.format("Key.get(%s.class)", out.typeName(key.getTypeLiteral().getType())); - } - - - void writeGetConstructionProxy() throws IOException { - out.writeLine("@%s(\"unchecked\")", SuppressWarnings.class) - .openScope("public <T> %s<T> getConstructionProxy(%s<T> implementation) {", ConstructionProxy.class, Class.class); - - for (Map.Entry<Class<?>, ConstructionProxy<?>> entry : constructionProxies.entrySet()) { - Class<?> implementation = entry.getKey(); - out.openScope("if (implementation == %s.class) {", implementation) - .openScope("return (%s) new %s<%s>() {", ConstructionProxy.class, ConstructionProxy.class, implementation); - - // newInstance - out.openScope("public %s newInstance(final %s... arguments) throws %s {", implementation, Object.class, InvocationTargetException.class) - .openScope("return new %s(", implementation); - int argument = 0; - for (Iterator<Dependency<?>> i - = entry.getValue().getInjectionPoint().getDependencies().iterator(); i.hasNext(); ) { - Dependency<?> parameter = i.next(); - String separator = i.hasNext() ? "," : ""; - out.writeLine("(%s) arguments[%d]%s", parameter.getKey().getTypeLiteral().getType(), argument, separator); - argument++; - } - out.closeScope(");") - .closeScope("}"); - - // getParameters - out.openScope("public %s<%s<?>> getParameters() {", List.class, Dependency.class) - .openScope("return %s.<%s<?>>asList(", Arrays.class, Dependency.class); - argument = 0; - for (Iterator<Dependency<?>> i - = entry.getValue().getInjectionPoint().getDependencies().iterator(); i.hasNext(); ) { - Dependency<?> parameter = i.next(); - String separator = i.hasNext() ? "," : ""; - out.writeLine("%s.create(%s, %s, %s)%s", Dependency.class, argument, keyLiteral(parameter.getKey()), parameter.isNullable(), separator); - argument++; - } - out.closeScope(");") - .closeScope("}"); - - // getMember - out.openScope("public %s getMember() {", Member.class) - .writeLine("return null;") - .closeScope("}"); - - out.closeScope("};") - .closeScope("}"); - } - out.writeLine() - .writeLine("throw new %s();", IllegalArgumentException.class) - .closeScope("}"); - } - } -} diff --git a/extensions/compiletime/src/com/google/inject/CompileTimeGuice.java b/extensions/compiletime/src/com/google/inject/CompileTimeGuice.java deleted file mode 100644 index a8194fe1..00000000 --- a/extensions/compiletime/src/com/google/inject/CompileTimeGuice.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright (C) 2008 Google Inc. - * - * 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.google.inject; - -import java.io.File; -import java.io.IOException; -import java.util.Set; -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * <strong>Unsupported.</strong> Currently compile-time Guice code still - * requires runtime access to the reflection APIs and is not suited for use. - * - * <p>Performs "compile-time" code generation in order to avoid runtime - * reflection. This is motivated to support Guice on environments where Java - * reflection is expensive or unavailable. - * - * @author jessewilson@google.com (Jesse Wilson) - */ -/* public */ class CompileTimeGuice { - - private final String name; - private final Set<? extends Module> modules; - - /** - * Builds a new compile-time guice instance. The instance must be provided - * the exact same {@code name} and {@code modules} at both compile-time and - * at runtime, otherwise behaviour is undefined. - * - * @param name a unique, valid Java identifier such as "FooApplication". - * @param modules the modules used to build the injector. - */ - public CompileTimeGuice(String name, Set<? extends Module> modules) { - this.name = checkNotNull(name, "name"); - this.modules = checkNotNull(modules, "modules"); - } - - /** - * "Compile-time" step that generates <code>.java</code> source directories - * to support runtime reflection. This step must be executed whenever the - * modules or their dependendent classes have changed. - */ - void generateCode(File generatedSourceDirectory) throws IOException { - CodeGenReflectionFactory reflectionFactory = new CodeGenReflectionFactory(name); - - new InjectorBuilder() - .stage(Stage.TOOL) - .addModules(modules) - .usingReflectionFactory(reflectionFactory) - .build(); - - reflectionFactory.writeToFile(generatedSourceDirectory); - } - - /** - * Runtime step that uses the generated <code>.java</code> to create an - * injector. - */ - Injector createInjector() { - Reflection.Factory reflectionFactory - = new CodeGenReflectionFactory(name).getRuntimeReflectionFactory(); - - return new InjectorBuilder() - .stage(Stage.TOOL) - .addModules(modules) - .usingReflectionFactory(reflectionFactory) - .build(); - } -}
\ No newline at end of file diff --git a/extensions/compiletime/src/com/google/inject/JavaCodeGenerator.java b/extensions/compiletime/src/com/google/inject/JavaCodeGenerator.java deleted file mode 100644 index 14647fae..00000000 --- a/extensions/compiletime/src/com/google/inject/JavaCodeGenerator.java +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright (C) 2008 Google Inc. - * - * 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.google.inject; - -import java.io.*; -import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.Map; - -/** - * @author jessewilson@google.com (Jesse Wilson) - */ -/* public */ class JavaCodeGenerator { - - private final Writer writer; - private final String packageName; - private int indent = 0; - - /** - * Imported classes, by their simple name. If multiple classes with the same - * simplename have been imported, the fully qualified names will be used for - * all but one of those classes. - */ - private final Map<String, Class<?>> importedClasses = new HashMap<String, Class<?>>(); - - private JavaCodeGenerator(Writer writer, String packageName) { - this.writer = writer; - this.packageName = packageName; - } - - public static JavaCodeGenerator open(File generatedSourceDirectory, - String packageName, String topLevelClassName) throws IOException { - File directory = generatedSourceDirectory; - for (String packagePart : packageName.split("\\.")) { - directory = new File(directory, packagePart); - } - directory.mkdirs(); - File sourceFile = new File(directory, topLevelClassName + ".java"); - return new JavaCodeGenerator( - new OutputStreamWriter(new FileOutputStream(sourceFile), "ISO-8859-1"), packageName); - } - - public void writePackageHeader() throws IOException { - writeLine("// Generated by Guice. Do not edit!"); - writeLine("package %s;", packageName); - writeLine(); - } - - public void writeImport(Type type) throws IOException { - if (!(type instanceof Class)) { - throw new UnsupportedOperationException(); - } - - Class<?> clas = (Class<?>) type; - - if (importedClasses.containsKey(clas.getSimpleName())) { - return; - } - - if (!"java.lang".equals(clas.getPackage().getName())) { - writeLine("import %s;", typeName(type)); - } - importedClasses.put(clas.getSimpleName(), clas); - } - - public String typeName(Type type) { - if (type instanceof Class<?>) { - Class<?> clas = (Class<?>) type; - if (importedClasses.get(clas.getSimpleName()) == type) { - return clas.getSimpleName(); - } - StringBuilder result = new StringBuilder(); - result.append(clas.getPackage().getName()); - for (Class<?> enclosing = clas.getEnclosingClass(); enclosing != null; - enclosing = enclosing.getEnclosingClass()) { - result.append(".").append(enclosing.getSimpleName()); - } - result.append(".").append(clas.getSimpleName()); - return result.toString(); - - } else { - throw new UnsupportedOperationException(); - } - } - - private Object[] processArgs(Object... args) { - args = args.clone(); - for (int i = 0; i < args.length; i++) { - if (args[i] instanceof Class<?>) { - args[i] = typeName((Class<?>) args[i]); - } - } - return args; - } - - public JavaCodeGenerator writeLine(String format, Object... args) throws IOException { - for (int i = 0; i < indent; i++) { - writer.append(" "); - } - writer.append(String.format(format, processArgs(args))); - writeLine(); - return this; - } - - public JavaCodeGenerator openScope(String format, Object... args) throws IOException { - writeLine(format, args); - indent++; - return this; - } - - public JavaCodeGenerator closeScope(String format, Object... args) throws IOException { - indent--; - return writeLine(format, args); - } - - public JavaCodeGenerator writeLine() throws IOException { - writer.append("\n"); - return this; - } - - public void close() throws IOException { - writer.close(); - } -} diff --git a/extensions/compiletime/test/com/google/inject/CompileTimeGuiceTest.java b/extensions/compiletime/test/com/google/inject/CompileTimeGuiceTest.java deleted file mode 100644 index 862a92d9..00000000 --- a/extensions/compiletime/test/com/google/inject/CompileTimeGuiceTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright (C) 2008 Google Inc. - * - * 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.google.inject; - -import junit.framework.TestCase; - -import java.io.File; -import java.io.IOException; -import java.util.Collections; -import java.util.List; -import java.util.ArrayList; - - -/** - * @author jessewilson@google.com (Jesse Wilson) - */ -public class CompileTimeGuiceTest extends TestCase { - - private Module module = new AbstractModule() { - protected void configure() { - bind(Foo.class).to(RealFoo.class); - bind(Bar.class).toInstance(new Bar() {}); - bind(List.class).to(ArrayList.class); - } - }; - - public interface Bar { } - public interface Foo { } - public static class RealFoo implements Foo { - @Inject public RealFoo(Bar bar) { } - } - - public void test() { - Injector injector = new CompileTimeGuice("test", Collections.singleton(module)) - .createInjector(); - - assertTrue(injector.getInstance(Foo.class) instanceof RealFoo); - } - - public void generateCode() throws IOException { - new CompileTimeGuice("test", Collections.singleton(module)) - .generateCode(new File("/Users/jessewilson/svn/google-guice/generatedsrc")); - - } - - public static void main(String[] args) throws IOException { - new CompileTimeGuiceTest().generateCode(); - } -} |