0

I'm trying to access the name of a parameter using aspectJ, but I always get arg0 instead of the real name. I found that I should activate java debugging options with -g parameter at AspectJ JoinPoint question but that doesn't work for me...

This is my java code:

private Set<Collection<String>> s;
private Collection<String> c;

public HashCodeProperty() {
    s = new HashSet<Collection<String>>();
    c = new ArrayList<String>();
}

/**
 * method that satisfy property
 */
public void satisfy() {
    c.add("this is ok");
    s.add(c);
    System.out.println("Collection is in Set: " + s.contains(c));
}

/**
 * method that violate the property
 */
public void violate() {
    c.add("this is ok");
    s.add(c);
    c.add("don't do this");
    System.out.println("Collection is in Set: " +s.contains(c));
}

This is my AspectJ code:

pointcut addElementsToHashCodeSet() : call (* Set.add(..));

declare warning: addElementsToHashCodeSet(): "pointcut: addElementsToHashCode()";

after(): addElementsToHashCodeSet() { 
    monitorHashCode.addElementsToHashCode((MethodSignature)thisJoinPoint.getSignature());



public void addElementsToHashCode(MethodSignature methodSignature) { 
    System.out.println("\naddElementsToHashCode.");

    // We need to access to real PARAMETER NAME
    // Then we will concatenate with method and full class name
    String firstParameterName = methodSignature.getParameterNames()[0];
    // Add firstParameterName to an array that will contain all 
    // the name of the collections inserted into the HasSet 
    System.out.println("\nfirstParameterName: "+firstParameterName);
}

Current output:

firstParameterName: arg0

What I need to have as output:

firstParameterName: c

I have this two options:

Java compiler options

AspectJ compiler options

What else do I have to activate?

Thank you very much !

Community
  • 1
  • 1
Rodmar Conde
  • 956
  • 1
  • 12
  • 24

2 Answers2

1

I found a better way of knowing the name (identifier) of the variable with the following point cut:

before(Collection c): addElementsToHashCodeSet() && args(c)

In this way we can get a direct reference to variable c.

Activating debug symbols in eclipse was not necessary.

The required solution can be achieved with the following code:

The Instrumentator:

package fr.imag.ufrima.tat.tp6.aspects;

import java.util.Collection;
import java.util.Set;

import fr.imag.ufrima.tat.tp6.aspects.monitor.MonitorHashCode;

/**
 * Techniques Avancées de Test
 * 
 * @author Rodmar CONDE
 *
 * Instrumentator for validation of HashSet classes.
 * 
 * Secures the usage of HashSet that include Collections in preventing 
 * more elements to be added to the collections once they are added. 
 * 
 * Monitor code is provided in a separate class: MonitorHashCode.
 */
public aspect InstrumentationHashCode {

    private MonitorHashCode monitorHashCode;

    public InstrumentationHashCode() {
        monitorHashCode = new MonitorHashCode();
    }

    pointcut addElementsToHashCodeSet() : call (* Set.add(..));

    declare warning: addElementsToHashCodeSet(): "pointcut: addElementsToHashCode()";

    before(Collection c): addElementsToHashCodeSet() && args(c) { 
        monitorHashCode.addElementsToHashCode(c);                   
    }

    pointcut addElementsToCollection() : call (* Collection.add(..));

    declare warning: addElementsToCollection(): "pointcut: addElementsToCollection()";

    after(String s): addElementsToCollection() && args(s) {
        monitorHashCode.addElementsToCollection(thisJoinPoint.getTarget());     
    }

}

The Monitor:

package fr.imag.ufrima.tat.tp6.aspects.monitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 
 * @author Rodmar CONDE
 * 
 * Monitor used to prevent adding elements to a collection that has already been added to a HashMap
 *
 */
public class MonitorHashCode {
    /**
     *  Stores the system identity hashcode of the collections that are added to the monitored HashCode 
     */
    private List<Integer> collectionsAddedToHashSet;

    public MonitorHashCode() {
        System.out.println("Monitor created.");
        collectionsAddedToHashSet = new ArrayList<Integer>();
    }

    /**
     * Adds the system identity hashcode of the passed collection to the list
     * 
     * @param c Collection to be added to the list
     */
    public void addElementsToHashCode(Collection c) { 
        System.out.println("\naddElementsToHashCode.");

        collectionsAddedToHashSet.add(System.identityHashCode(c));
        System.out.println("\nCollection has been added to HashMap.");
    }

    /**
     * 
     * Before adding the element, search if the collection exists already
     * in the list, if so print an error.
     *
     * @param pointCutTarget
     */
    public void addElementsToCollection(Object pointCutTarget) { 
        System.out.println("\naddElementsToCollection.");

        int systemIdentityHashCode = System.identityHashCode(pointCutTarget);
        boolean isContained = collectionsAddedToHashSet.contains(systemIdentityHashCode);
        System.out.println(String.format("currentCollection: %s systemIdentityHashCode: %d - isContained: %s", pointCutTarget, systemIdentityHashCode, isContained));
        if (isContained) {
            System.out.println("Error: you have already added this collection into a hashmap, you can not add more elements into it!");
        }
    }

}
Rodmar Conde
  • 956
  • 1
  • 12
  • 24
0

The way I read it you are asking for the (internal) name of the first parameter of the Set.add(..)-method. That will never be a name you define, because it's part of the Java-API. What you want is the name of the value passed to that parameter.

The values should be available as Object[] via the JoinPoint.getArgs()-method, but I'm not immediately sure how to get their names. Should be possible via reflection though.

sheltem
  • 3,754
  • 1
  • 34
  • 39
  • Thank you for your help. What I have to to is: inside the method **s.add(c)** I need to access the **c** variable name to save it and monitor it later in another Aspectj. – Rodmar Conde Mar 25 '13 at 16:43