pax_global_header00006660000000000000000000000064121560432650014516gustar00rootroot0000000000000052 comment=3ff406525c09f66889508060e7dc81e86a70da73 bridge-method-injector-bridge-method-injector-parent-1.8/000077500000000000000000000000001215604326500235255ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/.gitignore000066400000000000000000000000311215604326500255070ustar00rootroot00000000000000target *.iml *.ipr *.iws bridge-method-injector-bridge-method-injector-parent-1.8/annotation/000077500000000000000000000000001215604326500256775ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/pom.xml000066400000000000000000000013441215604326500272160ustar00rootroot00000000000000 4.0.0 com.infradna.tool bridge-method-injector-parent 1.8 ../pom.xml bridge-method-annotation Bridge method injection annotations org.jenkins-ci annotation-indexer 1.4 bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/000077500000000000000000000000001215604326500264665ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/main/000077500000000000000000000000001215604326500274125ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/main/java/000077500000000000000000000000001215604326500303335ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/main/java/com/000077500000000000000000000000001215604326500311115ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/main/java/com/infradna/000077500000000000000000000000001215604326500326735ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/main/java/com/infradna/tool/000077500000000000000000000000001215604326500336505ustar00rootroot00000000000000000077500000000000000000000000001215604326500402625ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/main/java/com/infradna/tool/bridge_method_injectorBridgeMethodsAdded.java000066400000000000000000000032531215604326500445720ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/main/java/com/infradna/tool/bridge_method_injector/* * The MIT License * * Copyright (c) 2010, InfraDNA, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.infradna.tool.bridge_method_injector; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.*; /** * This annotation is added after the class transformation to indicate that * the class has already been processed by {@link MethodInjector}. * *

* Used for up-to-date check to avoid unnecessary transformations. * * @author Kohsuke Kawaguchi */ @Retention(CLASS) @Target(TYPE) public @interface BridgeMethodsAdded { } WithBridgeMethods.java000066400000000000000000000066721215604326500445140ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/annotation/src/main/java/com/infradna/tool/bridge_method_injector/* * The MIT License * * Copyright (c) 2010, InfraDNA, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.infradna.tool.bridge_method_injector; import org.jvnet.hudson.annotation_indexer.Indexed; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.CLASS; /** * Request that bridge methods of the same name and same arguments be generated * with each specified type as the return type. This helps you maintain binary compatibility * as you evolve your classes. * *

* For example, if you have the following code: * *

 * @WithBridgeMethods(Foo.class)
 * public FooSubType getFoo() { ... }
 * 
* *

* The Maven mojo will insert the following bridge method: * *

 * public Foo getFoo() {
 *     return getFoo(); // invokevirtual to getFoo() that returns FooSubType
 * }
 * 
* *

* * In some cases, it's necessary to widen the return type of a method, but in a way that legacy * calls would still return instances of the original type. In this case, add * {@link #castRequired() castRequired=true} to the annotation. For example, if you have the * following code: *

 * @WithBridgeMethods(value=FooSubType.class, castRequired=true)
 * public <T extends Foo> createFoo(Class<T> clazz) {
 *   return clazz.newInstance();
 * }
 * 
*

* The Maven mojo will insert the following bridge method: * *

 * public FooSubType createFoo(Class clazz) {
 *   return (FooSubType) createFoo(clazz); // invokeVirtual to createFoo that returns Foo
 * }
 * 
* @author Kohsuke Kawaguchi */ @Retention(CLASS) @Target(METHOD) @Documented @Indexed public @interface WithBridgeMethods { /** * Specifies the return types. These types must be assignable to the actual * method return type, or {@link #castRequired()} should be set to true. */ Class[] value(); /** * Specifies whether the injected bridge methods should perform a cast prior to returning. Only * set this to true when it is known that calls to the bridge methods will in fact return * types assignable to the actual method return type, even though declared return types are * not assignable to the actual method return type. * * @since 1.4 */ boolean castRequired() default false; } bridge-method-injector-bridge-method-injector-parent-1.8/injector/000077500000000000000000000000001215604326500253425ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/pom.xml000066400000000000000000000136541215604326500266700ustar00rootroot00000000000000 4.0.0 com.infradna.tool bridge-method-injector-parent 1.8 ../pom.xml bridge-method-injector maven-plugin bridge-method-injector Evolve your classes without breaking compatibility maven-compiler-plugin 1.5 1.5 org.apache.maven.plugins maven-release-plugin 2.0 org.apache.maven.scm maven-scm-provider-gitexe 1.2 maven-antrun-plugin run test com.sun tools 1.6 system ${java.home}/../lib/tools.jar org.jvnet.wagon-svn wagon-svn 1.9 scm:git:git@github.com:infradna/bridge-method-injector.git kohsuke Kohsuke Kawaguchi MIT License repository http://www.opensource.org/licenses/mit-license.php ${project.groupId} bridge-method-annotation ${project.version} asm asm-debug-all 3.2 junit junit 3.8.1 test org.apache.maven maven-plugin-api 2.0.1 bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/000077500000000000000000000000001215604326500261315ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/000077500000000000000000000000001215604326500270555ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/java/000077500000000000000000000000001215604326500277765ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/java/com/000077500000000000000000000000001215604326500305545ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/java/com/infradna/000077500000000000000000000000001215604326500323365ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/java/com/infradna/tool/000077500000000000000000000000001215604326500333135ustar00rootroot00000000000000000077500000000000000000000000001215604326500377255ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/java/com/infradna/tool/bridge_method_injectorClassAnnotationInjector.java000066400000000000000000000047111215604326500453710ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/java/com/infradna/tool/bridge_method_injector/* * The MIT License * * Copyright (c) 2010, InfraDNA, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.infradna.tool.bridge_method_injector; import org.objectweb.asm.ClassAdapter; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; /** * Adds class annotations. * * @author Kohsuke Kawaguchi */ abstract class ClassAnnotationInjector extends ClassAdapter { ClassAnnotationInjector(ClassVisitor cv) { super(cv); } private boolean emitted = false; private void maybeEmit() { if (!emitted) { emitted = true; emit(); } } protected abstract void emit(); @Override public void visitInnerClass(String name, String outerName, String innerName, int access) { maybeEmit(); super.visitInnerClass(name, outerName, innerName, access); } @Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { maybeEmit(); return super.visitField(access, name, desc, signature, value); } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { maybeEmit(); return super.visitMethod(access, name, desc, signature, exceptions); } @Override public void visitEnd() { maybeEmit(); super.visitEnd(); } } MethodInjector.java000066400000000000000000000245261215604326500435170ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/java/com/infradna/tool/bridge_method_injector/* * The MIT License * * Copyright (c) 2010, InfraDNA, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.infradna.tool.bridge_method_injector; import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.ClassAdapter; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodAdapter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Type; import org.objectweb.asm.commons.EmptyVisitor; import org.objectweb.asm.tree.AnnotationNode; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import static org.objectweb.asm.ClassWriter.*; import static org.objectweb.asm.Opcodes.*; /** * Injects bridge methods as per {@link WithBridgeMethods}. * * @author Kohsuke Kawaguchi */ public class MethodInjector { public void handleRecursively(File f) throws IOException { if (f.isDirectory()) { for (File c : f.listFiles()) handleRecursively(c); } else if (f.getName().endsWith(".class")) handle(f); } public void handle(File classFile) throws IOException { FileInputStream in = new FileInputStream(classFile); byte[] image; try { ClassReader cr = new ClassReader(new BufferedInputStream(in)); ClassWriter cw = new ClassWriter(cr, COMPUTE_MAXS); cr.accept(new Transformer(new ClassAnnotationInjectorImpl(cw)),0); image = cw.toByteArray(); } catch (AlreadyUpToDate _) { // no need to process this class. it's already up-to-date. return; } catch (IOException e) { throw (IOException)new IOException("Failed to process "+classFile).initCause(e); } catch (RuntimeException e) { throw (IOException)new IOException("Failed to process "+classFile).initCause(e); } finally { in.close(); } // write it back FileOutputStream out = new FileOutputStream(classFile); out.write(image); out.close(); } /** * Thrown to indicate that there's no need to re-process this class file. */ class AlreadyUpToDate extends RuntimeException { private static final long serialVersionUID = 1L; } static class ClassAnnotationInjectorImpl extends ClassAnnotationInjector { ClassAnnotationInjectorImpl(ClassVisitor cv) { super(cv); } @Override protected void emit() { AnnotationVisitor av = cv.visitAnnotation(SYNTHETIC_METHODS_ADDED, false); av.visitEnd(); } } private static class WithBridgeMethodsAnnotationVisitor extends EmptyVisitor { private boolean castRequired = false; private List types = new ArrayList(); @Override public void visit(String name, Object value) { if ("castRequired".equals(name) && value instanceof Boolean) { castRequired = (Boolean) value; } if (value instanceof Type) { // assume this is a member of the array of classes named "value" in WithBridgeMethods types.add((Type) value); } } } class Transformer extends ClassAdapter { private String internalClassName; /** * Synthetic methods to be generated. */ private final List syntheticMethods = new ArrayList(); class SyntheticMethod { final int access; final String name; final String desc; final String originalSignature; final String[] exceptions; final boolean castRequired; final Type returnType; SyntheticMethod(int access, String name, String desc, String originalSignature, String[] exceptions, Type returnType, boolean castRequired) { this.access = access; this.name = name; this.desc = desc; this.originalSignature = originalSignature; this.exceptions = exceptions; this.returnType = returnType; this.castRequired = castRequired; } /** * Injects a synthetic method and send it to cv. */ public void inject(ClassVisitor cv) { Type[] paramTypes = Type.getArgumentTypes(desc); Type originalReturnType = Type.getReturnType(desc); MethodVisitor mv = cv.visitMethod(access | ACC_SYNTHETIC | ACC_BRIDGE, name, Type.getMethodDescriptor(returnType, paramTypes), null/*TODO:is this really correct?*/, exceptions); if ((access&ACC_ABSTRACT)==0) { mv.visitCode(); int sz = 0; boolean isStatic = (access & ACC_STATIC) != 0; if (!isStatic) { mv.visitVarInsn(ALOAD,0); sz++; } for (Type p : paramTypes) { mv.visitVarInsn(p.getOpcode(ILOAD), sz); sz += p.getSize(); } mv.visitMethodInsn( isStatic ? INVOKESTATIC : INVOKEVIRTUAL,internalClassName,name,desc); if (castRequired) { mv.visitTypeInsn(CHECKCAST, returnType.getInternalName()); } if (returnType.equals(Type.VOID_TYPE) || returnType.getClassName().equals("java.lang.Void")) { // bridge to void, which means disregard the return value from the original method switch (originalReturnType.getSize()) { case 1: mv.visitInsn(originalReturnType.getOpcode(POP)); break; case 2: mv.visitInsn(originalReturnType.getOpcode(POP2)); break; default: throw new AssertionError("Unexpected operand size: "+originalReturnType); } } mv.visitInsn(returnType.getOpcode(IRETURN)); mv.visitMaxs(sz,0); } mv.visitEnd(); } } Transformer(ClassVisitor cv) { super(cv); } @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { this.internalClassName = name; super.visit(version, access, name, signature, superName, interfaces); } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if (desc.equals(SYNTHETIC_METHODS_ADDED)) throw new AlreadyUpToDate(); // no need to process this class return super.visitAnnotation(desc, visible); } /** * Look for methods annotated with {@link WithBridgeMethods}. */ @Override public MethodVisitor visitMethod(final int access, final String name, final String mdesc, final String signature, final String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, mdesc, signature, exceptions); return new MethodAdapter(mv) { @Override public AnnotationVisitor visitAnnotation(String adesc, boolean visible) { final AnnotationVisitor av = super.visitAnnotation(adesc, visible); if (adesc.equals(WITH_SYNTHETIC_METHODS)) return new AnnotationNode(adesc) { @Override public void visitEnd() { super.visitEnd(); // forward this annotation to the receiver accept(av); WithBridgeMethodsAnnotationVisitor annotationVisitor = new WithBridgeMethodsAnnotationVisitor(); accept(annotationVisitor); for (Type type : annotationVisitor.types) syntheticMethods.add(new SyntheticMethod( access,name,mdesc,signature,exceptions,type, annotationVisitor.castRequired )); } }; return av; } }; } /** * Inject methods at the end. */ @Override public void visitEnd() { for (SyntheticMethod m : syntheticMethods) m.inject(cv); super.visitEnd(); } } public static void main(String[] args) throws IOException { MethodInjector mi = new MethodInjector(); for (String a : args) { mi.handleRecursively(new File(a)); } } private static final String SYNTHETIC_METHODS_ADDED = Type.getDescriptor(BridgeMethodsAdded.class); private static final String WITH_SYNTHETIC_METHODS = Type.getDescriptor(WithBridgeMethods.class); } ProcessMojo.java000066400000000000000000000054721215604326500430430ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/main/java/com/infradna/tool/bridge_method_injector/* * The MIT License * * Copyright (c) 2010, InfraDNA, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.infradna.tool.bridge_method_injector; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; /** * @author Kohsuke Kawaguchi * @goal process * @phase process-classes * @requiresDependencyResolution runtime */ public class ProcessMojo extends AbstractMojo { /** * The directory containing generated classes. * * @parameter expression="${project.build.outputDirectory}" * @required */ private File classesDirectory; public void execute() throws MojoExecutionException, MojoFailureException { File index = new File(classesDirectory, "META-INF/annotations/" + WithBridgeMethods.class.getName()); if (!index.exists()) { getLog().debug("Skipping because there's no "+index); return; } BufferedReader r = null; try { r = new BufferedReader(new InputStreamReader(new FileInputStream(index),"UTF-8")); String line; while ((line=r.readLine())!=null) { File classFile = new File(classesDirectory,line.replace('.','/')+".class"); getLog().debug("Processing "+line); new MethodInjector().handle(classFile); } } catch (IOException e) { throw new MojoExecutionException("Failed to process @WithBridgeMethods",e); } finally { try { if (r!=null) r.close(); } catch (IOException _) { } } } } bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/000077500000000000000000000000001215604326500271105ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/client/000077500000000000000000000000001215604326500303665ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/client/Main.java000066400000000000000000000024631215604326500321220ustar00rootroot00000000000000public class Main { public static void main(String[] args) throws Exception { // invocation of void method. verify that it runs without error Foo.hello(); Object o = new Foo().getMessage(); assertEquals(args[0],o); String n = new Foo().getString(); assertEquals(args[0],n); Object s = Foo.getStaticMessage(); assertEquals(args[0],s); Object w = Foo.methodToWiden(String.class); assertEquals(args[0],w); // using reflection to ensure that JIT isn't doing inlining check((Foo)Foo.class.newInstance(),args[0]); // still a work in progress // check((Bar)Bar.class.newInstance(),args[0]); } private static void assertEquals(Object expected, Object actual) { System.out.println("We got "+actual+", expecting "+expected); if (!actual.equals(expected)) System.exit(1); } private static void check(IFoo f, String expected) { Object o = f.getMessage(); assertEquals(expected,o); String n = f.getString(); assertEquals(expected,n); } private static void check(IBar f, String expected) { Object o = f.narrow(); assertEquals(expected,o); String n = f.widen(); assertEquals(expected,n); } } bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v1/000077500000000000000000000000001215604326500274365ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v1/Bar.java000066400000000000000000000001761215604326500310110ustar00rootroot00000000000000public class Bar implements IBar { public String widen() { return "foo"; } public Object narrow() { return "foo"; } }bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v1/Foo.java000066400000000000000000000011221215604326500310200ustar00rootroot00000000000000public class Foo implements IFoo { /** * Testing narrowing. In v2, we'll narrow this to return the String type. This is type safe. */ public Object getMessage() { return "foo"; } /** * Testing widening. In v2, we'll widen this to Object. Potentially type unsafe. */ public String getString() { return "foo"; } public static Object getStaticMessage() { return "foo"; } public static T methodToWiden(Class clazz) { return clazz.cast("foo"); } public static void hello() {} }bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v1/IBar.java000066400000000000000000000001021215604326500311070ustar00rootroot00000000000000public interface IBar { String widen(); Object narrow(); }bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v1/IFoo.java000066400000000000000000000001301215604326500311270ustar00rootroot00000000000000public interface IFoo { public Object getMessage(); public String getString(); }bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v2/000077500000000000000000000000001215604326500274375ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v2/Bar.java000066400000000000000000000001761215604326500310120ustar00rootroot00000000000000public class Bar implements IBar { public Object widen() { return "bar"; } public String narrow() { return "bar"; } }bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v2/Foo.java000066400000000000000000000013441215604326500310270ustar00rootroot00000000000000import com.infradna.tool.bridge_method_injector.WithBridgeMethods; public class Foo implements IFoo { @WithBridgeMethods(Object.class) public String getMessage() { return "bar"; } /** * Widening String to Object. */ @WithBridgeMethods(value=String.class,castRequired=true) public Object getString() { return "bar"; } @WithBridgeMethods(Object.class) public static String getStaticMessage() { return "bar"; } @WithBridgeMethods(value=String.class, castRequired=true) public static T methodToWiden(Class clazz) { return clazz.cast("bar"); } @WithBridgeMethods(void.class) public static boolean hello() { return true; } }bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v2/IBar.java000066400000000000000000000003501215604326500311150ustar00rootroot00000000000000import com.infradna.tool.bridge_method_injector.WithBridgeMethods; public interface IBar { @WithBridgeMethods(value=String.class,castRequired=true) Object widen(); @WithBridgeMethods(Object.class) String narrow(); }bridge-method-injector-bridge-method-injector-parent-1.8/injector/src/test/v2/IFoo.java000066400000000000000000000003771215604326500311450ustar00rootroot00000000000000import com.infradna.tool.bridge_method_injector.WithBridgeMethods; public interface IFoo { @WithBridgeMethods(Object.class) public String getMessage(); @WithBridgeMethods(value=String.class,castRequired=true) public Object getString(); }bridge-method-injector-bridge-method-injector-parent-1.8/pom.xml000066400000000000000000000050261215604326500250450ustar00rootroot00000000000000 4.0.0 com.infradna.tool bridge-method-injector-parent 1.8 pom Bridge Method Injection Parent POM Evolve your classes without breaking compatibility annotation injector maven-compiler-plugin 1.5 1.5 org.apache.maven.plugins maven-release-plugin 2.0 org.apache.maven.scm maven-scm-provider-gitexe 1.2 org.jvnet.wagon-svn wagon-svn 1.9 scm:git:git@github.com:infradna/bridge-method-injector.git kohsuke Kohsuke Kawaguchi MIT License repository http://www.opensource.org/licenses/mit-license.php maven.jenkins-ci.org http://maven.jenkins-ci.org:8081/content/repositories/releases org.apache.maven.plugins maven-javadoc-plugin true bridge-method-injector-bridge-method-injector-parent-1.8/src/000077500000000000000000000000001215604326500243145ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/src/site/000077500000000000000000000000001215604326500252605ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/src/site/apt/000077500000000000000000000000001215604326500260445ustar00rootroot00000000000000bridge-method-injector-bridge-method-injector-parent-1.8/src/site/apt/index.apt.vm000066400000000000000000000112571215604326500303100ustar00rootroot00000000000000What's this? When you are writing a library, there are various restrictions about the kind of changes you can make, in order to maintain binary compatibility. One such restriction is an inability to restrict the return type. Say in v1 of your library you had the following code: ------------------------ public Foo getFoo() { return new Foo(); } ------------------------ In v2, say if you introduce a subtype of <<>> called <<>>, and you want to change the getFoo method to return <<>>. ------------------------ public FooSubType getFoo() { return new FooSubType(); } ------------------------ But if you do this, you break the binary compatibility. The clients need to be recompiled to be able to work with the new signature. This is where this bridge method injector can help. By adding an annotation like the following: ------------------------ @WithBridgeMethods(Foo.class) public FooSubType getFoo() { return new FooSubType(); } ------------------------ ... and running the bytecode post processor, your class file will get the additional "bridge methods." In pseudo-code, it'll look like this: ------------------------ // your original definition @WithBridgeMethods(Foo.class) public FooSubType getFoo() { return new FooSubType(); } // added bridge method public Foo getFoo() { invokevirtual this.getFoo()LFooSubType; areturn } ------------------------ Such code isn't allowed in Java source files, but class files allow that. With this addition, existing clients will continue to function. In this way, you can evolve your classes more easily without breaking backward compatibility. Widening the return type In some cases, it's convenient to widen the return type of a method. As this is potentially a type-unsafe change (as the callee can return a type that's not assignable to what the caller expects), so you as a programmer explicitly need to tell us that you know what you are doing by adding <<>> to the annotation. For example, suppose that v1 had a method: ------------------------ public createFoo(Class clazz) { return clazz.newInstance(); } ------------------------ and in v2 you wanted to widen this method to. Note that you can prove that this is still type-safe, while your compile cannot: ------------------------ public createFoo(Class clazz) { return clazz.newInstance(); } ------------------------ The annotation to provide backwards compatibility would be: ------------------------ @WithBridgeMethods(value=FooSubType.class, castRequired=true) public createFoo(Class clazz) { return clazz.newInstance(); } ------------------------ Running the bytecode post processor, the resulting class file will look like the following pseudo-code: ------------------------ // your original definition @WithBridgeMethods(value=FooSubType.class, castRequired=true) public createFoo(Class clazz) { return clazz.newInstance(); } // added bridge method public FooSubType createFoo(Class clazz) { invokevirtual this.createFoo(java/lang/Class)LFoo checkcast FooSubType areturn } ------------------------ Bridge Methods and Interfaces You can use <<<@WithBridgeMethods>>> with interfaces, too. However, making this work correctly is tricky, as you have to ensure that bridge methods are implemented on all the classes that implement the interface, for example by adding <<<@WithBridgeMethods>>> on every implementation of the method in question, or by introducing a base class that provides a bridge method. Integration into Your Build Add the following dependency in your POM. This dependency is only necessary during the compile time and not runtime, so an optional dependency is suffice. ------------------------ com.infradna.tool bridge-method-annotation 1.4 true ------------------------ Then put the following fragment in your build to have the byte-code post processor kick in to inject the necessary bridge methods. ------------------------ com.infradna.tool bridge-method-injector 1.4 process ------------------------ The artifacts are deployed to {{{http://maven.dyndns.org/2/com/infradna/tool/bridge-method-injector/}java.net Maven2 repository}}. bridge-method-injector-bridge-method-injector-parent-1.8/src/site/site.xml000066400000000000000000000015561215604326500267550ustar00rootroot00000000000000 Bridge Method Injector http://bridge-method-injector.infradna.com/ org.kohsuke maven-skin 1.1