Browse Source

ASM Usage and extension examples, implemented as test cases and made

available in the sandbox as an Eclipse project.
tags/for_ajdt1_1_12
mkersten 20 years ago
parent
commit
5130e9dbb7

+ 14
- 0
docs/sandbox/api-clients/.classpath View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="/runtime"/>
<classpathentry kind="src" path="/org.aspectj.ajdt.core"/>
<classpathentry kind="src" path="/asm"/>
<classpathentry kind="src" path="/ajde"/>
<classpathentry kind="src" path="/bridge"/>
<classpathentry kind="var" path="JUNIT_HOME/junit.jar"/>
<classpathentry kind="lib" path="/org.aspectj.ajdt.core/out/lib.jar"/>
<classpathentry kind="src" path="/org.eclipse.jdt.core"/>
<classpathentry kind="output" path=""/>
</classpath>

+ 17
- 0
docs/sandbox/api-clients/.project View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>sandbox-api-clients</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 0
- 129
docs/sandbox/api-clients/org/aspectj/samples/AffectedFilesReporter.java View File

@@ -1,129 +0,0 @@
/* @author Wes Isberg */

// START-SAMPLE api-asm-listAffectedFiles Walk model to list affected files

package org.aspectj.samples;

import org.aspectj.tools.ajc.Main;
import org.aspectj.asm.*;
import org.aspectj.bridge.*;

import java.io.*;
import java.util.*;


/**
* Run ajc and list files affected by advice or inter-type declarations.
*
* WARNING: Does not emit info messages for uses-pointcut dependencies.
* @author Wes Isberg
*/
public class AffectedFilesReporter implements Runnable {

/*
* Walk down asm hierarchy, looking for source files,
* and check if any of their children has a relation of
* kind ADVICE or INTER_TYPE_DECLARATION
*/

/**
* Wrapper for ajc that emits list of affected files.
* @param args the String[] of args for ajc,
* optionally prefixed with -to {file}
* @throws IOException if unable to write to {file}
*/
public static void main(String[] args) throws IOException {
Main main = new Main();
PrintStream toConfig = System.out;
FileOutputStream fin = null;
if ((args.length > 1) && ("-to".equals(args[0]))) {
File config = new File(args[1]);
fin = new FileOutputStream(config);
toConfig = new PrintStream(fin, true);
String[] temp = new String[args.length-2];
System.arraycopy(args, 2, temp, 0, temp.length);
args = temp;
}
Runnable runner = new AffectedFilesReporter(toConfig);
main.setCompletionRunner(runner);
// should add -emacssym to args if not already there
main.runMain(args, false);
if (null != fin) {
fin.close();
}
}
final PrintStream sink;
public AffectedFilesReporter(PrintStream sink) {
this.sink = (null == sink ? System.out : sink);
}
public void run() {
IHierarchy hierarchy = AsmManager.getDefault().getHierarchy();
if (null == hierarchy) {
sink.println("# no structure model - use -emacssym option");
return;
}
List /*IProgramElement*/ nodes = new LinkedList();
List /*IProgramElement*/ newNodes = new LinkedList();
// root could be config file or blank root - either way, use kids
nodes.addAll(hierarchy.getRoot().getChildren());
while (0 < nodes.size()) {
for (ListIterator it = nodes.listIterator(); it.hasNext();) {
IProgramElement node = (IProgramElement) it.next();
if (node.getKind().isSourceFileKind()) {
if (isAffected(node)) {
ISourceLocation loc = node.getSourceLocation();
sink.println(loc.getSourceFile().getPath());
}
} else {
// XXX uncertain of structure - traverse all??
newNodes.addAll(node.getChildren());
}
it.remove();
}
nodes.addAll(newNodes);
newNodes.clear();
}
}

/**
* Return true if this file node is affected by any aspects.
* Recursively search children for any effect,
* and halt on first affect found.
* @param node the IProgramElementNode for a source file
* @return true if affected.
*/
private boolean isAffected(final IProgramElement fileNode) {
final IRelationshipMap map =
AsmManager.getDefault().getRelationshipMap();
List /*IProgramElement*/ nodes = new LinkedList();
List /*IProgramElement*/ newNodes = new LinkedList();
nodes.add(fileNode);
while (0 < nodes.size()) {
for (ListIterator iter = nodes.listIterator();
iter.hasNext();) {
IProgramElement node = (IProgramElement) iter.next();
List relations = map.get(node);
if (null != relations) {
for (Iterator riter = relations.iterator();
riter.hasNext();) {
IRelationship.Kind kind =
((IRelationship) riter.next()).getKind();
if ((kind == IRelationship.Kind.ADVICE)
|| (kind == IRelationship.Kind.DECLARE_INTER_TYPE)) {
return true;
}
}
}
iter.remove();
newNodes.addAll(node.getChildren());
}
nodes.addAll(newNodes);
newNodes.clear();
}
return false;
}
}
// END-SAMPLE api-asm-listAffectedFiles

+ 67
- 0
docs/sandbox/api-clients/org/aspectj/samples/AsmHierarchyBuilderExtensionTest.java View File

@@ -0,0 +1,67 @@
/* *******************************************************************
* Copyright (c) 2004 Contributors.
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Common Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Mik Kersten initial implementation
* ******************************************************************/
package org.aspectj.samples;

import java.util.ArrayList;

import org.aspectj.ajde.AjdeTestCase;
import org.aspectj.ajdt.internal.core.builder.AjBuildManager;
import org.aspectj.ajdt.internal.core.builder.AsmHierarchyBuilder;
import org.aspectj.asm.AsmManager;
import org.aspectj.asm.IProgramElement;
import org.aspectj.asm.internal.ProgramElement;
import org.eclipse.jdt.internal.compiler.ast.MessageSend;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;

/**
* This test demonstrates how hierarchy building in the ASM can be extended
* to put additional information in the model, for example method call sites.
*
* @author Mik Kersten
*/
public class AsmHierarchyBuilderExtensionTest extends AjdeTestCase {

private ExtendedAsmHiearchyBuilder builder = new ExtendedAsmHiearchyBuilder();
public void testHiearchyExtension() {
assertNotNull(AsmManager.getDefault().getHierarchy().getRoot());
System.out.println(AsmManager.getDefault().getHierarchy().getRoot().toLongString());
}
protected void setUp() throws Exception {
super.setUp("examples");
AjBuildManager.setAsmHierarchyBuilder(builder); // NOTE that we set our builder here
assertTrue("build success", doSynchronousBuild("../examples/coverage/coverage.lst"));
}
}

class ExtendedAsmHiearchyBuilder extends AsmHierarchyBuilder {
public boolean visit(MessageSend messageSend, BlockScope scope) {
IProgramElement peNode = new ProgramElement(
new String(">>> found call: " + messageSend.toString()),
IProgramElement.Kind.CODE,
null, //makeLocation(messageSend),
0,
"",
new ArrayList());
// peNode.setCorrespondingType(messageSend.typ ieldDeclaration.type.toString());
// peNode.setSourceSignature(genSourceSignature(fieldDeclaration));
((IProgramElement)stack.peek()).addChild(peNode);
stack.push(peNode);
return true;
}
public void endVisit(MessageSend messageSend, BlockScope scope) {
stack.pop();
}

}

+ 81
- 0
docs/sandbox/api-clients/org/aspectj/samples/AsmRelationshipMapUsageTest.java View File

@@ -0,0 +1,81 @@
/* *******************************************************************
* Copyright (c) 2004 Contributors.
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Common Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Mik Kersten initial implementation
* ******************************************************************/

package org.aspectj.samples;

import java.util.Iterator;
import java.util.List;

import org.aspectj.ajde.AjdeTestCase;
import org.aspectj.ajdt.internal.core.builder.AjBuildManager;
import org.aspectj.asm.*;

/**
* Collects join point information for all advised methods and constructors.
*
* @author Mik Kersten
*/
public class AsmRelationshipMapUsageTest extends AjdeTestCase {

public void testFindAdvisedMethods() {
System.out.println("----------------------------------");
System.out.println("Methods affected by advice: ");
HierarchyWalker walker = new HierarchyWalker() {
public void preProcess(IProgramElement node) {
if (node.getKind().equals(IProgramElement.Kind.METHOD)) {
List relations = AsmManager.getDefault().getRelationshipMap().get(node);
if (relations != null) {
for (Iterator it = relations.iterator(); it.hasNext(); ) {
IRelationship relationship = (IRelationship)it.next();
if (relationship.getKind().equals(IRelationship.Kind.ADVICE)) {
System.out.println(
"method: " + node.toString()
+ ", advised by: " + relationship.getTargets());
}
}
}
}
}
};
AsmManager.getDefault().getHierarchy().getRoot().walk(walker);
}
public void testListFilesAffectedByInterTypeDecs() {
System.out.println("----------------------------------");
System.out.println("Files affected by inter type declarations: ");
HierarchyWalker walker = new HierarchyWalker() {
public void preProcess(IProgramElement node) {
if (node.getKind().equals(IProgramElement.Kind.CLASS)) {
List relations = AsmManager.getDefault().getRelationshipMap().get(node);
if (relations != null) {
for (Iterator it = relations.iterator(); it.hasNext(); ) {
IRelationship relationship = (IRelationship)it.next();
if (relationship.getKind().equals(IRelationship.Kind.DECLARE_INTER_TYPE)) {
System.out.println(
"file: " + node.getSourceLocation().getSourceFile().getName()
+ ", declared on by: " + relationship.getTargets());
}
}
}
}
}
};
AsmManager.getDefault().getHierarchy().getRoot().walk(walker);
}
protected void setUp() throws Exception {
super.setUp("examples");
assertTrue("build success", doSynchronousBuild("../examples/spacewar/spacewar/debug.lst"));
}
}


+ 0
- 70
docs/sandbox/api-clients/org/aspectj/samples/JoinPointCollector.java View File

@@ -1,70 +0,0 @@
/* @author Mik Kersten */

// WTI sample broke with API updates in 1.1.1
// START-BROKEN-SAMPLE api-ajde-modelWalker Walk model to collect join point information for advised methods and constructors
package org.aspectj.samples;

import java.util.*;
import org.aspectj.tools.ajc.Main;


import org.aspectj.asm.*;

/**
* Collects join point information for all advised methods and constructors.
*
* @author Mik Kersten
*/
public class JoinPointCollector extends Main {

/**
* @param args
*/
public static void main(String[] args) {
String[] newArgs = new String[args.length +1];
newArgs[0] = "-emacssym";
for (int i = 0; i < args.length; i++) {
newArgs[i+1] = args[i];
}
new JoinPointCollector().runMain(newArgs, false);
}
public void runMain(String[] args, boolean useSystemExit) {
super.runMain(args, useSystemExit);
ModelWalker walker = new ModelWalker() {
public void preProcess(StructureNode node) {
ProgramElementNode p = (ProgramElementNode)node;
// first check if it is a method or constructor
if (p.getProgramElementKind().equals(ProgramElementNode.Kind.METHOD)) {

// now check if it is advsied
for (Iterator it = p.getRelations().iterator(); it.hasNext(); ) {
RelationNode relationNode = (RelationNode)it.next();
Relation relation = relationNode.getRelation();
if (relation == AdviceAssociation.METHOD_RELATION) {
System.out.println("method: " + p.toString() + ", advised by: " + relationNode.getChildren());
}
}
}
// code around the fact that constructor advice relationship is on the type
if (p.getProgramElementKind().equals(ProgramElementNode.Kind.CONSTRUCTOR)) {
for (Iterator it = ((ProgramElementNode)p.getParent()).getRelations().iterator(); it.hasNext(); ) {
RelationNode relationNode = (RelationNode)it.next();
Relation relation = relationNode.getRelation();
if (relation == AdviceAssociation.CONSTRUCTOR_RELATION) {
System.out.println("constructor: " + p.toString() + ", advised by: " + relationNode.getChildren());
}
}
}
}
};

StructureModelManager.getDefault().getStructureModel().getRoot().walk(walker);
}
}
//END-BROKEN-SAMPLE api-ajde-modelWalker


+ 0
- 0
docs/sandbox/api-clients/readme.html View File


Loading…
Cancel
Save