Question

i am new to aspectj.

i have an android appplication with an activity. That activity does not show any menu when i press menu button. I found that, application owner have not overridden the method onCreateOptionsMenu(Menu menu) SO now i want to override this method in that application activity class. But i have been restricted to not to change their original code, and make changes via aspect . SO i want to know that is there any way through which i can add this method to their code through aspectj's pointcut.

Here is my android file MainActivity.java:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;



      public class MainActivity extends Activity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.sampleactivity);

            }


        }

I want following method to add in above code via pointcut :

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sampleactivity, menu);
        return true;
    }

SO if possible suggest me any pointcut to achive above things.

Thank u in advance . .

Was it helpful?

Solution

Basically I agree with @pskink's comments: If you can just extend a class and override its methods this is preferable to using "heavy artillery" such as AspectJ. From what I understand you want to manipulate another app for which you do not have the source code and into which you cannot inject your subclass with the overridden method. If this is correct, I do understand the need for AspectJ. So here is the answer to your question:

Well, I do not use Android and am too lazy to download the full Android JDK just in order to demonstrate the solution, but I just created a fake example.

API base class:

package org.base.api;

public class Activity {
    protected void onCreate(Object obj) {
        System.out.println("onCreate default implementation");
    }

    public boolean onCreateOptionsMenu(Object obj) {
        System.out.println("onCreateOptionsMenu default implementation");
        return false;
    }
}

Driver application:

As I said, I am assuming the app to be be commercial, so you cannot change its behaviour by conventional OOP means. The app overrides one API method, but not the one you want.

package com.commercial.app;

import org.base.api.Activity;

public class CommercialAppActivity extends Activity {
    @Override
    protected void onCreate (Object obj) {
        System.out.println("onCreate implementation overridden by commercial app");
    }

    public static void main(String[] args) {
        System.out.println("Starting commercial app");
        CommercialAppActivity activity = new CommercialAppActivity();
        activity.onCreate(null);
        activity.onCreateOptionsMenu("menu");
        System.out.println("Stopping commercial app");
    }
}

Sample output:

Starting commercial app
onCreate implementation overridden by commercial app
onCreateOptionsMenu default implementation
Stopping commercial app

Aspect introducing desired method via ITD (inter-type definition):

package de.scrum_master.aspect;

import com.commercial.app.CommercialAppActivity;

public aspect AppManipulatorAspect {
    public boolean CommercialAppActivity.onCreateOptionsMenu(Object obj) {
        System.out.println("onCreateOptionsMenu implementation overridden by aspect");
        return false;
    }
}

Sample output with spect:

Starting commercial app
onCreate implementation overridden by commercial app
onCreateOptionsMenu implementation overridden by aspect
Stopping commercial app

Tadaa! It should be straightforward to transfer this to your situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top