Bug 510077: [otdre] More logging of errors during weaving
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/DelegatingTransformer.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/DelegatingTransformer.java
index 32f2aea..cf12392 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/DelegatingTransformer.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/DelegatingTransformer.java
@@ -78,7 +78,7 @@
 		public Collection<String> fetchAdaptedBases() {
 			return transformer.fetchAdaptedBases();
 		}
-		public byte[] transform(final Bundle bundle, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] bytes) {
+		public byte[] transform(final Bundle bundle, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] bytes) throws IllegalClassFormatException {
 			// TODO provide classID
 			return transformer.transform(getBundleLoader(bundle), className, className.replace('.', '/'), classBeingRedefined, bytes);
 		}
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
index 282d526..631c884 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
@@ -480,8 +480,15 @@
 			}
 			beingDefined.remove(className);
 			IReweavingTask task = pendingReweavingTasks.remove(className);
-			if (task != null)
-				task.reweave(wovenClass.getDefinedClass());
+			if (task != null) {
+				try {
+					task.reweave(wovenClass.getDefinedClass());
+				} catch (IllegalClassFormatException e) {
+					log(e, "Failed to reweave class "+wovenClass.getClassName());
+					TransformerPlugin.flushLog();
+					return;
+				}
+			}
 			instantiateScheduledTeams(className);
 
 			TransformerPlugin.flushLog();
diff --git a/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/bytecode/AbstractBoundClass.java b/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/bytecode/AbstractBoundClass.java
index 732f8cc..ac8aaf3 100644
--- a/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/bytecode/AbstractBoundClass.java
+++ b/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/bytecode/AbstractBoundClass.java
@@ -16,6 +16,7 @@
  **********************************************************************/
 package org.eclipse.objectteams.otredyn.bytecode;
 
+import java.lang.instrument.IllegalClassFormatException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -365,8 +366,9 @@
 	 * Add the empty method access
 	 * Add the empty method accessStatic
 	 * Add the empty method callAllBindings
+	 * @throws IllegalClassFormatException various bytecode problems, e.g., unexpected RET instruction etc.
 	 */
-	public void transformAtLoadTime() {
+	public void transformAtLoadTime() throws IllegalClassFormatException {
 		handleTaskList(null);
 	}
 
@@ -623,8 +625,9 @@
 	 * Handle all open weaving tasks for this class.
 	 * It redefines the class, if it is not called while loading
 	 * @param definedClass previously defined class if available
+	 * @throws IllegalClassFormatException various bytecode problems, e.g., unexpected RET instruction etc.
 	 */
-	public void handleTaskList(Class<?> definedClass) {
+	public void handleTaskList(Class<?> definedClass) throws IllegalClassFormatException {
 		if (isTransformationActive()) return;
 
 		if (this.isUnweavable)
@@ -828,7 +831,7 @@
 				for (final AbstractBoundClass affected : affectedClasses)
 					if (affected.isLoaded) {
 						IReweavingTask task = new IReweavingTask() {
-							@Override public void reweave(Class<?> definedClass) {
+							@Override public void reweave(Class<?> definedClass) throws IllegalClassFormatException {
 								affected.handleTaskList(definedClass);
 							}
 						};
@@ -887,7 +890,11 @@
 		if (task == null)
 			task = new WeavingTask(type, binding.getMemberName(), binding.getMemberSignature(), 
 											binding.getBaseFlags(), binding.isHandleCovariantReturn(), binding.requiresBaseSuperCall());
-		addWeavingTask(task, false);
+		try {
+			addWeavingTask(task, false);
+		} catch (IllegalClassFormatException e) {
+			e.printStackTrace(); // we're called from TeamManager, which can neither log nor handle exceptions
+		}
 	}
 	
 	@Override
@@ -899,11 +906,15 @@
 	public synchronized void commitTransaction() {
 		--this.transactionCount;
 		if (this.transactionCount == 0 && this.isLoaded) {
-			handleTaskList(null);
+			try {
+				handleTaskList(null);
+			} catch (IllegalClassFormatException e) {
+				e.printStackTrace(); // we're called from TeamManager, which can neither log nor handle exceptions
+			}
 		}
 	}
 	
-	private void addWeavingTask(WeavingTask task, boolean standBy) {
+	private void addWeavingTask(WeavingTask task, boolean standBy) throws IllegalClassFormatException {
 		boolean isNewTask = addWeavingTaskLazy(task);
 
 		if (this.isLoaded && isNewTask && !standBy && this.transactionCount == 0) {
@@ -1160,9 +1171,9 @@
 	
 	protected abstract void startTransformation();
 
-	protected abstract void endTransformation(Class<?> definedClass);
+	protected abstract void endTransformation(Class<?> definedClass) throws IllegalClassFormatException;
 
-	protected abstract void superTransformation(Class<?> definedClass);
+	protected abstract void superTransformation(Class<?> definedClass) throws IllegalClassFormatException;
 
 	protected abstract void prepareAsPossibleBaseClass();
 
diff --git a/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/bytecode/asm/AsmWritableBoundClass.java b/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/bytecode/asm/AsmWritableBoundClass.java
index 2a110a7..7c6c5e5 100644
--- a/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/bytecode/asm/AsmWritableBoundClass.java
+++ b/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/bytecode/asm/AsmWritableBoundClass.java
@@ -19,6 +19,7 @@
 import java.io.File;

 import java.io.FileOutputStream;

 import java.io.IOException;

+import java.lang.instrument.IllegalClassFormatException;

 import java.util.ArrayList;

 import java.util.List;

 

@@ -142,9 +143,10 @@
 

 	/**

 	 * Executes all pending transformations.

+	 * @throws IllegalClassFormatException various bytecode problems, e.g., unexpected RET instruction etc.

 	 */

 	@Override

-	protected void endTransformation(final Class<?> definedClass) {

+	protected void endTransformation(final Class<?> definedClass) throws IllegalClassFormatException {

 		assert (isTransformationActive) : "No transformation active";

 		

 		if (multiAdapter == null || nodes == null)

@@ -152,13 +154,11 @@
 		if (multiAdapter.hasVisitors() || !nodes.isEmpty()) {

 			// //TODO (ofra): Do everything in one transformation

 			// Do all transformation with the Core API of ASM

-//			try {

+			try {

 				reader.accept(multiAdapter, ClassReader.SKIP_FRAMES);

-//			} catch (RuntimeException e) {

-//				System.err.println("Cannot transform class "+this+":");

-//				e.printStackTrace();

-//				return;

-//			}

+			} catch (RuntimeException e) {

+				throw new IllegalClassFormatException("Cannot transform class "+this+":"+e.getMessage());

+			}

 			setBytecode(writer.toByteArray());

 			//Do all transformations with the Tree API of ASM

 			for (AbstractTransformableClassNode node : nodes) {

@@ -223,7 +223,7 @@
 		releaseBytecode();

 	}

 

-	protected void superTransformation(Class<?> definedClass) {		

+	protected void superTransformation(Class<?> definedClass) throws IllegalClassFormatException {		

 		AbstractTeam mySuper = getSuperclass();

 		if (mySuper != null && mySuper.isLoaded()) {

 			boolean superNeedsWeaving = false;

diff --git a/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/transformer/jplis/ObjectTeamsTransformer.java b/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/transformer/jplis/ObjectTeamsTransformer.java
index f97218b..be8edf9 100644
--- a/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/transformer/jplis/ObjectTeamsTransformer.java
+++ b/plugins/org.eclipse.objectteams.otredyn/src/org/eclipse/objectteams/otredyn/transformer/jplis/ObjectTeamsTransformer.java
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.IllegalClassFormatException;
 import java.security.ProtectionDomain;
 import java.util.Collection;
 import java.util.HashSet;
@@ -63,11 +64,16 @@
 	public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
             ProtectionDomain protectionDomain, byte[] classfileBuffer)
 	{
-		return transform(loader, className, className, classBeingRedefined, classfileBuffer);
+		try {
+			return transform(loader, className, className, classBeingRedefined, classfileBuffer);
+		} catch (IllegalClassFormatException e) {
+			e.printStackTrace();
+			return classfileBuffer;
+		}
 	}
 	
 	public byte[] transform(ClassLoader loader, String className, String classId, Class<?> classBeingRedefined,
-            byte[] classfileBuffer) {
+            byte[] classfileBuffer) throws IllegalClassFormatException {
 		if (loader == null)
 			loader = ClassLoader.getSystemClassLoader();
 
@@ -107,6 +113,8 @@
 					clazz.transformAtLoadTime();
 				
 				classfileBuffer = clazz.getBytecode();
+			} catch (IllegalClassFormatException e) {
+				throw e; // expected, propagate to caller (OT/Equinox?)
 			} catch(Throwable t) {
 				t.printStackTrace();
 			}
diff --git a/plugins/org.eclipse.objectteams.runtime/META-INF/MANIFEST.MF b/plugins/org.eclipse.objectteams.runtime/META-INF/MANIFEST.MF
index dba062a..926e0a5 100644
--- a/plugins/org.eclipse.objectteams.runtime/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.objectteams.runtime/META-INF/MANIFEST.MF
@@ -2,13 +2,13 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.objectteams.runtime
-Bundle-Version: 2.5.0.qualifier
+Bundle-Version: 2.6.0.qualifier
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
 Require-Bundle: org.eclipse.jdt.annotation;bundle-version="[1.1.0,2.0.0)";resolution:=optional
-Export-Package: org.eclipse.objectteams.otredyn.runtime;version="2.5.0",
- org.eclipse.objectteams.runtime;version="2.5.0",
- org.objectteams;version="2.5.0"
+Export-Package: org.eclipse.objectteams.otredyn.runtime;version="2.6.0",
+ org.eclipse.objectteams.runtime;version="2.6.0",
+ org.objectteams;version="2.6.0"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-ClassPath: .
 
diff --git a/plugins/org.eclipse.objectteams.runtime/src/org/eclipse/objectteams/runtime/IReweavingTask.java b/plugins/org.eclipse.objectteams.runtime/src/org/eclipse/objectteams/runtime/IReweavingTask.java
index a52c447..b7c574f 100644
--- a/plugins/org.eclipse.objectteams.runtime/src/org/eclipse/objectteams/runtime/IReweavingTask.java
+++ b/plugins/org.eclipse.objectteams.runtime/src/org/eclipse/objectteams/runtime/IReweavingTask.java
@@ -15,6 +15,8 @@
  */
 package org.eclipse.objectteams.runtime;
 
+import java.lang.instrument.IllegalClassFormatException;
+
 import org.eclipse.jdt.annotation.Nullable;
 
 /**
@@ -26,6 +28,8 @@
 	 * Reweave the class represented by this task.
 	 * @param definedClass for convenience the previously defined class can
 	 * 	be passed in as an argument.
+	 * @throws IllegalClassFormatException various problems during bytecode transformation, e.g.,
+	 * 	unexpected / illegal byte codes (like RET) encountered.
 	 */
-	public void reweave(@Nullable Class<?> definedClass);
+	public void reweave(@Nullable Class<?> definedClass) throws IllegalClassFormatException;
 }
diff --git a/releng/map/otdt.map.in b/releng/map/otdt.map.in
index efeed0d..265f76b 100644
--- a/releng/map/otdt.map.in
+++ b/releng/map/otdt.map.in
@@ -19,7 +19,7 @@
 plugin@org.eclipse.objectteams.otdt.doc=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otdt.doc,tag=builds/201606070956

 !plugin@org.eclipse.objectteams.otdt.metrics=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otdt.metrics,tag=builds/201101290806

 

-plugin@org.eclipse.objectteams.runtime=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.runtime,tag=builds/201606070956

+plugin@org.eclipse.objectteams.runtime=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.runtime

 plugin@org.eclipse.objectteams.otre=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otre,tag=builds/201606070956

 plugin@org.eclipse.objectteams.otredyn=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otredyn

 !the following is also referenced in otdt.doc/buildDoc.xml (plugin-name without version):