summaryrefslogtreecommitdiffstats
path: root/tests/src/org/aspectj/testing/Utils.java
blob: 92e0ed833605971591499d88b0fd364aacc22a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
 * Copyright (c) 2006 IBM 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andy Clement - initial API and implementation
 *******************************************************************************/
package org.aspectj.testing;

import java.io.File;

import org.aspectj.apache.bcel.Repository;
import org.aspectj.apache.bcel.classfile.JavaClass;
import org.aspectj.apache.bcel.classfile.Method;
import org.aspectj.apache.bcel.util.ClassPath;
import org.aspectj.apache.bcel.util.SyntheticRepository;
import org.aspectj.apache.bcel.verifier.VerificationResult;
import org.aspectj.apache.bcel.verifier.Verifier;
import org.aspectj.apache.bcel.verifier.VerifierFactory;
import org.aspectj.tools.ajc.Ajc;

/**
 * Not quite the right place for this class..
 */
public class Utils {
	
	private final static boolean debugVerification=false;
	
	/**
	 * Performs verification of a class - the supplied class is expected to exist in the sandbox
	 * directory so typically this is called after a small compile step has been invoked to build it.
	 * @param ajc 
	 */
	public static String verifyClass(Ajc ajc, String clazzname) {
		JavaClass jc = null;
		try {
			jc = getClassFrom(ajc.getSandboxDirectory().getAbsolutePath(),clazzname);
		} catch (ClassNotFoundException cnfe) {
			return "Could not find "+clazzname+" in the sandbox: "+ajc.getSandboxDirectory();
		}
		if (jc==null) return "Could not find class "+clazzname;
		Repository.setRepository(jc.getRepository());
		Verifier v = VerifierFactory.getVerifier("mypackage.MyAspect"); 
		VerificationResult vr = v.doPass1();
		if (debugVerification) System.err.println(vr);
		
		if (vr.getStatus()!=VerificationResult.VERIFIED_OK)
			return "Verification not ok: "+vr;
		vr = v.doPass2();
		if (debugVerification) System.err.println(vr);
		if (vr.getStatus()!=VerificationResult.VERIFIED_OK)
			return "Verification not ok: "+vr;
		Method[] ms = jc.getMethods();
		for (int i = 0; i < ms.length; i++) {
			if (debugVerification) System.err.println("Pass3a for "+ms[i]);
			vr = v.doPass3a(i);
			if (debugVerification) System.err.println(vr);		
			if (vr.getStatus()!=VerificationResult.VERIFIED_OK)
				return "Verification not ok: "+vr;
			if (debugVerification) System.err.println("Pass3b for "+ms[i]);
			vr = v.doPass3b(i);
			if (debugVerification) System.err.println(vr);
			if (vr.getStatus()!=VerificationResult.VERIFIED_OK)
				return "Verification not ok: "+vr;
		}
		return null;
	}
	
	public static JavaClass getClassFrom(String frompath,String clazzname) throws ClassNotFoundException {
		SyntheticRepository repos = createRepos(frompath);
		return repos.loadClass(clazzname);
	}

	public static SyntheticRepository createRepos(String cpentry) {
		ClassPath cp = new ClassPath(
				cpentry+File.pathSeparator+
				System.getProperty("java.class.path"));
		return SyntheticRepository.getInstance(cp);
	}	
}