/*
 * Copyright 2013 Charles University in Prague
 * Copyright 2013 Vojtech Horky
 * 
 * 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 cz.cuni.mff.d3s.spl.instrumentation.javassist;

import javassist.ByteArrayClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.LoaderClassPath;
import javassist.NotFoundException;

public class Utils {
	public static final String INSTRUMENTATION_IDENTIFIERS_PREFIX = "_______SPL_instrumentation_";
	
	public static CtClass getClassFromBytecode(ClassLoader loader,
			String classname, byte[] bytecode) throws NotFoundException {
		ClassPool pool = ClassPool.getDefault();
		pool.insertClassPath(new ByteArrayClassPath(classname, bytecode));
		pool.insertClassPath(new LoaderClassPath(loader));
		CtClass cc = pool.get(classname);
		cc.defrost();
		return cc;
	}
	
}

@@ -705,21 +705,19 @@
 ort java
-ssist
+.io
 .ByteArr
@@ -718,26 +718,29 @@
 yteArray
-ClassPath;
+InputStream;%0A
 %0Aimport 
@@ -1123,16 +1123,20 @@
 l pool =
+ new
  ClassPo
@@ -1141,19 +1141,8 @@
 Pool
-.getDefault
 ();%0A
@@ -1168,25 +1168,22 @@
 ath(new 
-ByteArray
+Loader
 ClassPat
@@ -1184,27 +1184,96 @@
 assPath(
-classname, 
+loader));%0A%09%09%0A%09%09ByteArrayInputStream bytecodeAsStream = new ByteArrayInputStream(
 bytecode
@@ -1277,100 +1277,373 @@
 ode)
-)
 ;%0A%09%09
-pool.insertClassPath(new LoaderClassPath(loader));%0A%09%09CtClass cc = pool.get(classn
+%0A%09%09try %7B%0A%09%09%09/*%0A%09%09%09 * We need to define the class explicitly.%0A%09%09%09 * Adding ByteArrayClassPath to the pool classpath causes that%0A%09%09%09 * following calls would work with the original class definition%0A%09%09%09 * (that happens despite working with root class pool and%0A%09%09%09 * with any combination of defrost/detach).%0A%09%09%09 */%0A%09%09%09CtClass cc = pool.makeClass(bytecodeAsStre
 am
-e
 );%0A
+%09
 %09%09cc
@@ -1656,16 +1656,17 @@
 st();%0A%09%09
+%09
 return c
@@ -1668,15 +1668,172 @@
 urn cc;%0A
+%09%09%7D catch (Exception e) %7B%0A%09%09%09/* This would hardly happen, but anyway. */%0A%09%09%09throw new NotFoundException(%22Making class from byte%5B%5D bytecode failed.%22, e);%0A%09%09%7D%0A
 %09%7D%0A%09%0A%7D%0A

Load CtClass really from the provided bytecode