another example program requested by Amer Diwan

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@2847 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
sjfink 2008-05-25 19:23:31 +00:00
parent 8c83635562
commit 796eb7a879
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/com.ibm.wala.core.tests/src/com/ibm/wala/examples/analysis/GetLoadedFields.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.ibm.wala.examples.analysis.GetLoadedFields"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-classpath c:/temp/testdata/JLex.jar"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="com.ibm.wala.core.tests"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx500M"/>
</launchConfiguration>

View File

@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.examples.analysis;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import com.ibm.wala.classLoader.CodeScanner;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.shrikeCT.InvalidClassFileException;
import com.ibm.wala.types.FieldReference;
import com.ibm.wala.util.collections.HashMapFactory;
import com.ibm.wala.util.config.AnalysisScopeReader;
/**
* This is a simple example WALA application.
*
* For each method in the standard libraries, it maps the IMethod to the set of fields the method reads.
*
* @author sfink
*/
public class GetLoadedFields {
private final static ClassLoader MY_CLASSLOADER = GetLoadedFields.class.getClassLoader();
/**
* Use the 'CountParameters' launcher to run this program with the appropriate classpath
* @throws InvalidClassFileException
*/
public static void main(String[] args) throws IOException, ClassHierarchyException, InvalidClassFileException {
// build an analysis scope representing the standard libraries, excluding no classes
AnalysisScope scope = AnalysisScopeReader.read("primordial.txt", null, MY_CLASSLOADER);
// build a class hierarchy
System.err.print("Build class hierarchy...");
IClassHierarchy cha = ClassHierarchy.make(scope);
System.err.println("Done");
int nMethods = 0;
int nFields = 0;
Map<IMethod, Collection<FieldReference>> method2Field = HashMapFactory.make();
for (IClass c : cha) {
for (IMethod m : c.getDeclaredMethods()) {
nMethods++;
Collection<FieldReference> fields = CodeScanner.getFieldsRead(m);
nFields += fields.size();
method2Field.put(m, fields);
}
}
System.out.println(nMethods + " methods");
System.out.println((float)nFields/(float)nMethods + " fields read per method");
}
}