summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorMarcel Bruch2011-10-04 17:02:56 (EDT)
committer Marcel Bruch2011-10-04 17:02:56 (EDT)
commit4e36f32cbdce37c14a4c381ee28cbc72f37a74d3 (patch)
tree9fafcc0142ddd6291072507553f176b2558a780a
parent13bcedde5a6a3abc814c50b44308e29a9a238241 (diff)
downloadorg.eclipse.recommenders-4e36f32cbdce37c14a4c381ee28cbc72f37a74d3.zip
org.eclipse.recommenders-4e36f32cbdce37c14a4c381ee28cbc72f37a74d3.tar.gz
org.eclipse.recommenders-4e36f32cbdce37c14a4c381ee28cbc72f37a74d3.tar.bz2
-rw-r--r--org.eclipse.recommenders.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/utils/LocalNamesCollectorNaiveEdition.java88
-rw-r--r--org.eclipse.recommenders.tests.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/LocalNamesCollectorTest.java96
-rw-r--r--org.eclipse.recommenders.tests.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/NaiveLocalNamesCollectorTest.java64
3 files changed, 152 insertions, 96 deletions
diff --git a/org.eclipse.recommenders.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/utils/LocalNamesCollectorNaiveEdition.java b/org.eclipse.recommenders.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/utils/LocalNamesCollectorNaiveEdition.java
new file mode 100644
index 0000000..a3ecc93
--- /dev/null
+++ b/org.eclipse.recommenders.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/utils/LocalNamesCollectorNaiveEdition.java
@@ -0,0 +1,88 @@
+/**
+ * Copyright (c) 2010 Darmstadt University of Technology.
+ * 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:
+ * Marcel Bruch - initial API and implementation.
+ */
+package org.eclipse.recommenders.internal.commons.analysis.utils;
+
+import java.util.Collection;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import com.ibm.wala.classLoader.IBytecodeMethod;
+import com.ibm.wala.shrikeCT.InvalidClassFileException;
+import com.ibm.wala.ssa.IR;
+import com.ibm.wala.ssa.SSAInstruction;
+
+public class LocalNamesCollectorNaiveEdition {
+
+ public static final String UNKNOWN = "unnamed";
+ private final Multimap<Integer/* value number */, String> names = HashMultimap.create();
+ private IR ir;
+
+ public LocalNamesCollectorNaiveEdition(final IR ir) throws InvalidClassFileException {
+ storeIR(ir);
+ collectParameterNames();
+ collectLocalValueNames();
+ }
+
+ private void storeIR(final IR ir) {
+ this.ir = ir;
+ }
+
+ private void collectParameterNames() {
+ for (final int parameterValueNumber : ir.getParameterValueNumbers()) {
+ final String[] localNames = ir.getLocalNames(0, parameterValueNumber);
+ storeLocalNamesForValueNumber(parameterValueNumber, localNames);
+ }
+ }
+
+ private void storeLocalNamesForValueNumber(final int valueNumber, final String[] localNames) {
+ if (localNames != null) {
+ for (final String name : localNames) {
+ storeLocalNameForValueNumber(valueNumber, name);
+ }
+ }
+ }
+
+ private boolean storeLocalNameForValueNumber(final int valueNumber, final String name) {
+ if (name != null) {
+ return names.put(valueNumber, name);
+ }
+ return false;
+ }
+
+ private void collectLocalValueNames() throws InvalidClassFileException {
+ final SSAInstruction[] instructions = ir.getInstructions();
+ for (int instructionIndex = 0; instructionIndex < instructions.length; instructionIndex++) {
+ final SSAInstruction instruction = instructions[instructionIndex];
+ if (isNullOrHasNoDef(instruction)) {
+ continue;
+ }
+ final int defValueNumber = instruction.getDef();
+ final int bytecodeIndex = ((IBytecodeMethod) ir.getMethod()).getBytecodeIndex(instructionIndex);
+ final String[] irIndexlocalNames = ir.getLocalNames(instructionIndex, defValueNumber);
+ final String[] bcIndexlocalNames = ir.getLocalNames(bytecodeIndex, defValueNumber);
+ final String[] justForDebug = ir.getLocalNames(instructions.length - 1, defValueNumber);
+
+ storeLocalNamesForValueNumber(defValueNumber, irIndexlocalNames);
+ }
+ }
+
+ private boolean isNullOrHasNoDef(final SSAInstruction instruction) {
+ return instruction == null || !instruction.hasDef();
+ }
+
+ public Collection<Integer> getValues() {
+ return names.keySet();
+ }
+
+ public Collection<String> getNames() {
+ return names.values();
+ }
+}
diff --git a/org.eclipse.recommenders.tests.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/LocalNamesCollectorTest.java b/org.eclipse.recommenders.tests.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/LocalNamesCollectorTest.java
deleted file mode 100644
index d1815bf..0000000
--- a/org.eclipse.recommenders.tests.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/LocalNamesCollectorTest.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/**
- * Copyright (c) 2010, 2011 Darmstadt University of Technology.
- * 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:
- * Marcel Bruch - Initial API and implementation
- */
-package org.eclipse.recommenders.internal.commons.analysis;
-
-import java.util.Collection;
-
-import names.Names__Alias01;
-import names.Names__Alias02;
-import names.Names__Field_Constructor;
-import names.Names__Field_Method_Uninitialized;
-import names.Names__Field_To_Temp_Local;
-import names.Names__Local_Constructor;
-import names.Names__Local_Defined_By_Constructor;
-import names.Names__Local_Defined_By_ReturnValue;
-import names.Names__Local_Method;
-import names.Names__Local_With_Branch;
-import names.Names__MultiNames;
-import names.Names__Param_Method_Call;
-
-import org.eclipse.recommenders.internal.commons.analysis.utils.LocalNamesCollector;
-import org.eclipse.recommenders.tests.commons.analysis.utils.BundleClassloaderBasedClassHierarchy;
-import org.eclipse.recommenders.tests.commons.analysis.utils.WalaTestUtils;
-import org.junit.Test;
-
-import com.ibm.wala.classLoader.IMethod;
-import com.ibm.wala.ipa.callgraph.AnalysisCache;
-import com.ibm.wala.ipa.cha.IClassHierarchy;
-import com.ibm.wala.ssa.IR;
-
-public class LocalNamesCollectorTest {
-
- private static IClassHierarchy cha = BundleClassloaderBasedClassHierarchy
- .newInstance(LocalNamesCollectorTest.class);
- private final AnalysisCache cache = new AnalysisCache();
- private LocalNamesCollector reference;
- // private LocalNamesCollector2 sut;
- private final Class<?>[] testcases = new Class[] { Names__Field_To_Temp_Local.class, Names__Alias01.class,
- Names__MultiNames.class, Names__Alias02.class, Names__Field_Constructor.class,
- Names__Field_Method_Uninitialized.class, Names__Local_Constructor.class,
- Names__Local_Defined_By_Constructor.class, Names__Local_Defined_By_ReturnValue.class,
- Names__Local_Method.class, Names__Local_With_Branch.class, Names__Param_Method_Call.class, };
- private IR ir;
-
- @Test
- public void testSameResults() throws Exception {
- for (final Class<?> clazz : testcases) {
- testSameResults(clazz);
- }
- }
-
- private void testSameResults(final Class clazz) {
- setupNameCollectors(clazz);
- verifySameNamesAndValues();
- }
-
- private void setupNameCollectors(final Class<?> testCase) {
- final IMethod method = WalaTestUtils.lookupTestMethod(cha, testCase);
- ir = cache.getIR(method);
- reference = new LocalNamesCollector(ir);
- // sut = new LocalNamesCollector2(ir);
- }
-
- private void verifySameNamesAndValues() {
- verifySameNames();
- verifySameValues();
- for (final int value : reference.getValues()) {
- // final String actual = sut.getName(value);
- final String expected = reference.getName(value);
- // assertEquals(expected, actual);
- }
- }
-
- private void verifySameNames() {
- final Collection<String> expected = reference.getNames();
- // final Collection<String> actual = sut.getNames();
- // final Collection diff = CollectionUtils.disjunction(actual,
- // expected);
- // assertTrue(diff + " not empty", diff.isEmpty());
- }
-
- private void verifySameValues() {
- final Collection<Integer> expected = reference.getValues();
- // final Collection<Integer> actual = sut.getValues();
- // final Collection diff = CollectionUtils.disjunction(actual,
- // expected);
- // assertTrue(diff + " not empty", diff.isEmpty());
- }
-}
diff --git a/org.eclipse.recommenders.tests.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/NaiveLocalNamesCollectorTest.java b/org.eclipse.recommenders.tests.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/NaiveLocalNamesCollectorTest.java
new file mode 100644
index 0000000..e6ef08b
--- /dev/null
+++ b/org.eclipse.recommenders.tests.commons.analysis/src/org/eclipse/recommenders/internal/commons/analysis/NaiveLocalNamesCollectorTest.java
@@ -0,0 +1,64 @@
+/**
+ * Copyright (c) 2010, 2011 Darmstadt University of Technology.
+ * 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:
+ * Marcel Bruch - Initial API and implementation
+ */
+package org.eclipse.recommenders.internal.commons.analysis;
+
+import static java.lang.String.format;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import names.Names__Field_To_Temp_Local;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.eclipse.recommenders.internal.commons.analysis.utils.LocalNamesCollectorNaiveEdition;
+import org.eclipse.recommenders.tests.commons.analysis.utils.BundleClassloaderBasedClassHierarchy;
+import org.eclipse.recommenders.tests.commons.analysis.utils.WalaTestUtils;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+import com.ibm.wala.classLoader.IMethod;
+import com.ibm.wala.ipa.callgraph.AnalysisCache;
+import com.ibm.wala.ipa.cha.IClassHierarchy;
+import com.ibm.wala.ssa.IR;
+
+public class NaiveLocalNamesCollectorTest {
+
+ private static IClassHierarchy cha = BundleClassloaderBasedClassHierarchy
+ .newInstance(NaiveLocalNamesCollectorTest.class);
+ private final AnalysisCache cache = new AnalysisCache();
+
+ private IR ir;
+ private IMethod method;
+ private LocalNamesCollectorNaiveEdition sut;
+
+ @Test
+ public void testSimpleCase() {
+ setupNameCollectors(Names__Field_To_Temp_Local.class);
+ verifyLocalNames("this", "tmp");
+ }
+
+ @SuppressWarnings("unchecked")
+ private void verifyLocalNames(final String... expectedNames) {
+ final Collection<String> expected = Lists.newArrayList(expectedNames);
+ final Collection<String> actual = sut.getNames();
+ final Collection<String> diff = CollectionUtils.disjunction(actual, expected);
+ final String msg = format("Expected diff to be empty but found: %s", diff);
+ assertTrue(msg, diff.isEmpty());
+
+ }
+
+ private void setupNameCollectors(final Class<?> testCase) {
+ method = WalaTestUtils.lookupTestMethod(cha, testCase);
+ ir = cache.getIR(method);
+ sut = new LocalNamesCollectorNaiveEdition(ir);
+ }
+
+}