在Android中运行JavaMOP Aspects的异常

时间:2012-05-30 09:53:44

标签: android aspectj

我正在使用Java的MOP工具(JavaMOP)来生成在Android中运行的Aspects。
我正在尝试使用HasNext示例,该示例简单地指定程序始终在迭代器的下一个方法之前调用hasnext方法。

使用JavaMOP生成的方面的代码是:

package mop;
import java.io.*;
import java.util.*;
import javamoprt.*;
import java.lang.ref.*;
import org.aspectj.lang.*;

class HasNextMonitor_Set extends javamoprt.MOPSet {
    protected HasNextMonitor[] elementData;

    public HasNextMonitor_Set(){
        this.size = 0;
        this.elementData = new HasNextMonitor[4];
    }

    public final int size(){
        while(size > 0 && elementData[size-1].MOP_terminated) {
            elementData[--size] = null;
        }
        return size;
    }

    public final boolean add(MOPMonitor e){
        ensureCapacity();
        elementData[size++] = (HasNextMonitor)e;
        return true;
    }

    public final void endObject(int idnum){
        int numAlive = 0;
        for(int i = 0; i < size; i++){
            HasNextMonitor monitor = elementData[i];
            if(!monitor.MOP_terminated){
                monitor.endObject(idnum);
            }
            if(!monitor.MOP_terminated){
                elementData[numAlive++] = monitor;
            }
        }
        for(int i = numAlive; i < size; i++){
            elementData[i] = null;
        }
        size = numAlive;
    }

    public final boolean alive(){
        for(int i = 0; i < size; i++){
            MOPMonitor monitor = elementData[i];
            if(!monitor.MOP_terminated){
                return true;
            }
        }
        return false;
    }

    public final void endObjectAndClean(int idnum){
        int size = this.size;
        this.size = 0;
        for(int i = size - 1; i >= 0; i--){
            MOPMonitor monitor = elementData[i];
            if(monitor != null && !monitor.MOP_terminated){
                monitor.endObject(idnum);
            }
            elementData[i] = null;
        }
        elementData = null;
    }

    public final void ensureCapacity() {
        int oldCapacity = elementData.length;
        if (size + 1 > oldCapacity) {
            cleanup();
        }
        if (size + 1 > oldCapacity) {
            HasNextMonitor[] oldData = elementData;
            int newCapacity = (oldCapacity * 3) / 2 + 1;
            if (newCapacity < size + 1){
                newCapacity = size + 1;
            }
            elementData = Arrays.copyOf(oldData, newCapacity);
        }
    }

    public final void cleanup() {
        int numAlive = 0 ;
        for(int i = 0; i < size; i++){
            HasNextMonitor monitor = (HasNextMonitor)elementData[i];
            if(!monitor.MOP_terminated){
                elementData[numAlive] = monitor;
                numAlive++;
            }
        }
        for(int i = numAlive; i < size; i++){
            elementData[i] = null;
        }
        size = numAlive;
    }

    public final void event_hasnext(Iterator i) {
        int numAlive = 0 ;
        for(int i_1 = 0; i_1 < this.size; i_1++){
            HasNextMonitor monitor = (HasNextMonitor)this.elementData[i_1];
            if(!monitor.MOP_terminated){
                elementData[numAlive] = monitor;
                numAlive++;

                monitor.Prop_1_event_hasnext(i);
                if(monitor.Prop_1_Category_fail) {
                    monitor.Prop_1_handler_fail(i);
                }
            }
        }
        for(int i_1 = numAlive; i_1 < this.size; i_1++){
            this.elementData[i_1] = null;
        }
        size = numAlive;
    }

    public final void event_next(Iterator i) {
        int numAlive = 0 ;
        for(int i_1 = 0; i_1 < this.size; i_1++){
            HasNextMonitor monitor = (HasNextMonitor)this.elementData[i_1];
            if(!monitor.MOP_terminated){
                elementData[numAlive] = monitor;
                numAlive++;

                monitor.Prop_1_event_next(i);
                if(monitor.Prop_1_Category_fail) {
                    monitor.Prop_1_handler_fail(i);
                }
            }
        }
        for(int i_1 = numAlive; i_1 < this.size; i_1++){
            this.elementData[i_1] = null;
        }
        size = numAlive;
    }
}

class HasNextMonitor extends javamoprt.MOPMonitor implements Cloneable, javamoprt.MOPObject {
    public Object clone() {
        try {
            HasNextMonitor ret = (HasNextMonitor) super.clone();
            return ret;
        }
        catch (CloneNotSupportedException e) {
            throw new InternalError(e.toString());
        }
    }

    int Prop_1_state;
    static final int Prop_1_transition_hasnext[] = {1, 1, 2};;
    static final int Prop_1_transition_next[] = {2, 0, 2};;

    boolean Prop_1_Category_fail = false;

    public HasNextMonitor () {
        Prop_1_state = 0;

    }

    public final void Prop_1_event_hasnext(Iterator i) {
        MOP_lastevent = 0;

        Prop_1_state = Prop_1_transition_hasnext[Prop_1_state];
        Prop_1_Category_fail = Prop_1_state == 2;
    }

    public final void Prop_1_event_next(Iterator i) {
        MOP_lastevent = 1;

        Prop_1_state = Prop_1_transition_next[Prop_1_state];
        Prop_1_Category_fail = Prop_1_state == 2;
    }

    public final void Prop_1_handler_fail (Iterator i){
        {
            System.err.println("! hasNext() has not been called" + " before calling next() for an" + " iterator");
            this.reset();
        }

    }

    public final void reset() {
        MOP_lastevent = -1;
        Prop_1_state = 0;
        Prop_1_Category_fail = false;
    }

    public javamoprt.ref.MOPWeakReference MOPRef_i;

    //alive_parameters_0 = [Iterator i]
    public boolean alive_parameters_0 = true;

    public final void endObject(int idnum){
        switch(idnum){
            case 0:
            alive_parameters_0 = false;
            break;
        }
        switch(MOP_lastevent) {
            case -1:
            return;
            case 0:
            //hasnext
            //alive_i
            if(!(alive_parameters_0)){
                MOP_terminated = true;
                return;
            }
            break;

            case 1:
            //next
            //alive_i
            if(!(alive_parameters_0)){
                MOP_terminated = true;
                return;
            }
            break;

        }
        return;
    }

}

public aspect HasNextMonitorAspect implements javamoprt.MOPObject {
    javamoprt.map.MOPMapManager HasNextMapManager;
    public HasNextMonitorAspect(){
        HasNextMapManager = new javamoprt.map.MOPMapManager();
        HasNextMapManager.start();
    }

    // Declarations for the Lock
    static Object HasNext_MOPLock = new Object();

    static boolean HasNext_activated = false;

    // Declarations for Indexing Trees
    static javamoprt.map.MOPBasicRefMapOfMonitor HasNext_i_Map = new javamoprt.map.MOPBasicRefMapOfMonitor(0);
    static javamoprt.ref.MOPWeakReference HasNext_i_Map_cachekey_0 = javamoprt.map.MOPBasicRefMapOfMonitor.NULRef;
    static HasNextMonitor HasNext_i_Map_cachenode = null;

    // Trees for References
    static javamoprt.map.MOPRefMap HasNext_Iterator_RefMap = HasNext_i_Map;

    pointcut MOP_CommonPointCut() : !within(javamoprt.MOPObject+) && !adviceexecution();
    pointcut HasNext_hasnext(Iterator i) : (call(* Iterator.hasNext()) && target(i)) && MOP_CommonPointCut();
    after (Iterator i) : HasNext_hasnext(i) {
    try{
        HasNext_activated = true;
        synchronized(HasNext_MOPLock) {
            HasNextMonitor mainMonitor = null;
            javamoprt.map.MOPMap mainMap = null;
            javamoprt.ref.MOPWeakReference TempRef_i;

            // Cache Retrieval
            if (i == HasNext_i_Map_cachekey_0.get()) {
                TempRef_i = HasNext_i_Map_cachekey_0;

                mainMonitor = HasNext_i_Map_cachenode;
            } else {
                TempRef_i = HasNext_i_Map.getRef(i);
            }

            if (mainMonitor == null) {
                mainMap = HasNext_i_Map;
                mainMonitor = (HasNextMonitor)mainMap.getNode(TempRef_i);

                if (mainMonitor == null) {
                    mainMonitor = new HasNextMonitor();

                    mainMonitor.MOPRef_i = TempRef_i;

                    HasNext_i_Map.putNode(TempRef_i, mainMonitor);
                }

                HasNext_i_Map_cachekey_0 = TempRef_i;
                HasNext_i_Map_cachenode = mainMonitor;
            }

            mainMonitor.Prop_1_event_hasnext(i);
            if(mainMonitor.Prop_1_Category_fail) {
                mainMonitor.Prop_1_handler_fail(i);
            }
        }
        }catch(Exception e){
                    System.out.println("Cause: "+e.getCause().toString());
                    System.out.println("Error:"+e.getLocalizedMessage());
                }
    }

    pointcut HasNext_next(Iterator i) : (call(* Iterator.next()) && target(i)) && MOP_CommonPointCut();
    before (Iterator i) : HasNext_next(i) {
        try{
        HasNext_activated = true;
        synchronized(HasNext_MOPLock) {
            HasNextMonitor mainMonitor = null;
            javamoprt.map.MOPMap mainMap = null;
            javamoprt.ref.MOPWeakReference TempRef_i;

            // Cache Retrieval
            if (i == HasNext_i_Map_cachekey_0.get()) {
                TempRef_i = HasNext_i_Map_cachekey_0;

                mainMonitor = HasNext_i_Map_cachenode;
            } else {
                TempRef_i = HasNext_i_Map.getRef(i);
            }

            if (mainMonitor == null) {
                mainMap = HasNext_i_Map;
                mainMonitor = (HasNextMonitor)mainMap.getNode(TempRef_i);

                if (mainMonitor == null) {
                    mainMonitor = new HasNextMonitor();

                    mainMonitor.MOPRef_i = TempRef_i;

                    HasNext_i_Map.putNode(TempRef_i, mainMonitor);
                }

                HasNext_i_Map_cachekey_0 = TempRef_i;
                HasNext_i_Map_cachenode = mainMonitor;
            }

            mainMonitor.Prop_1_event_next(i);
            if(mainMonitor.Prop_1_Category_fail) {
                mainMonitor.Prop_1_handler_fail(i);
            }
        }
    }catch(Exception e){
                    System.out.println("Cause: "+e.getCause().toString());
                    System.out.println("Error:"+e.getLocalizedMessage());
                }
    }

    after(Iterator i) throwing (Exception e): HasNext_next(i) {
      System.out.println("Threw an exception: " + e);
      }
    after(Iterator i) throwing (Exception e): HasNext_hasnext(i) {
      System.out.println("Threw an exception: " + e);
      }

}

}

我的Android项目中的方法是:

public void method1() {
        Vector<Integer> v = new Vector<Integer>();
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        Iterator<Integer> i = v.iterator();
        int sum = 0;
        if (i.hasNext()) {
            sum += (Integer) i.next();
            sum += (Integer) i.next();
            sum += (Integer) i.next();
            sum += (Integer) i.next();
        }
        System.out.println("sum: " + sum);
}

在使用方面编织Android项目并执行该方法后,应用程序抛出以下异常:

E/AndroidRuntime(2334): FATAL EXCEPTION: main
E/AndroidRuntime(2334): org.aspectj.lang.NoAspectBoundException: Exception while initializing mop_HasNextMonitorAspect: java.lang.ExceptionInInitializerError
E/AndroidRuntime(2334):     at mop.HasNextMonitorAspect.aspectOf(HasNextMonitorAspect.aj:1)
E/AndroidRuntime(2334):     at com.mopapplication.MOPApplicationActivity.method1(Unknown Source)
E/AndroidRuntime(2334):     at com.mopapplication.MOPApplicationActivity$3.onClick(Unknown Source)
E/AndroidRuntime(2334):     at android.view.View.performClick(View.java:3122)
E/AndroidRuntime(2334):     at android.view.View$PerformClick.run(View.java:11942)
E/AndroidRuntime(2334):     at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(2334):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(2334):     at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime(2334):     at android.app.ActivityThread.main(ActivityThread.java:4028)
E/AndroidRuntime(2334):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(2334):     at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(2334):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
E/AndroidRuntime(2334):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
E/AndroidRuntime(2334):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(2334): Caused by: java.lang.ExceptionInInitializerError
E/AndroidRuntime(2334):     at java.lang.management.ManagementFactory.getOperatingSystemMXBean(ManagementFactory.java:409)
E/AndroidRuntime(2334):     at javamoprt.map.MOPMapManager.<init>(Unknown Source)
E/AndroidRuntime(2334):     at mop.HasNextMonitorAspect.<init>(HasNextMonitorAspect.aj:231)
E/AndroidRuntime(2334):     at mop.HasNextMonitorAspect.ajc$postClinit(HasNextMonitorAspect.aj:1)
E/AndroidRuntime(2334):     at mop.HasNextMonitorAspect.<clinit>(HasNextMonitorAspect.aj:228)
E/AndroidRuntime(2334):     ... 13 more
E/AndroidRuntime(2334): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load management: findLibrary returned null
E/AndroidRuntime(2334):     at java.lang.Runtime.loadLibrary(Runtime.java:425)
E/AndroidRuntime(2334):     at java.lang.System.loadLibrary(System.java:554)
E/AndroidRuntime(2334):     at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:50)
E/AndroidRuntime(2334):     at java.security.AccessController.doPrivileged(AccessController.java:116)
E/AndroidRuntime(2334):     at java.security.AccessController.doPrivileged(AccessController.java:85)
E/AndroidRuntime(2334):     at sun.management.ManagementFactory.<clinit>(ManagementFactory.java:467)
E/AndroidRuntime(2334):     ... 18 more

我知道这不是库的问题,因为我在Android项目中包含了AspectJ,JavaMOP和Java运行时库(对于Java运行时库,我只包括必要的类和包,不是整个图书馆。)

知道这是什么问题吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

从堆栈跟踪中我们可以看到您正在使用的库正在尝试加载“管理”库。 该库包含在sun(现在的Oracle)jdk中,但不是android的一部分。 我猜你在android中使用这个lib会不会有运气,因为它依赖于管理器之类的东西,比如未包含在android中的。