MethodScope

Additional

Language
Java
Version
v1.0.1 (Feb 7, 2019)
Created
Dec 10, 2017
Updated
Apr 20, 2019 (Retired)
Owner
Jaewoong Eum (skydoves)
Contributor
Jaewoong Eum (skydoves)
1
Activity
Badge
Generate
Download
Source code

MethodScope


Methodscope automatically generates classes that perform similar function tasks on a per-scope basis for a class.
When similar and repetitive classes need to be created, the work what repetitive inheritance can be decreased.

Including in your project

Gradle

And add below two dependencies code on your module's build.gradle file.

dependencies {
    implementation "com.github.skydoves:methodscope:1.0.1"
    annotationProcessor "com.github.skydoves:methodscope-processor:1.0.1"
}

Usage

@MethodScope

Create a custom scope annotation using @MethodScope annotation.

@MethodScope
public @interface MyScope {
}

Here is the kotlin code example.

@MethodScope
annotation class MyScope

Attach the custom scope annotation on top of a class.
All of the public methods are the base method for the scoped like the init() method.
And designate scoping method using @Scoped annotation with the naming rule what startWith the base method's name.

@MyScope
public class MyClass {
  private String scope;

  public void init() { // this is the base method of the scoped methods.
    this.scope = "initialized";
  }

  @Scoped(MyScope.class)
  public void initMyScope() { // this is a scoped method for @MyScope.
    this.scope = "initialized by @MyScope";
  }
}

After build the project, MyClass_MyScope class will be auto-generated.

MyClass_MyScope myClass_myScope = new MyClass_MyScope();
myClass_myScope.init(); // calls init() and initMyScope() methods.
myClass_myScope.initMyScope(); // calls only initMyScope() method.

Multiple Scoping

Here is how to create multiple scoped class.

@MyScope
@HisScope
@YourScope
public class MyClass {
  private String scope;

  public void init() { // this is the base method of the scoped methods.
    this.scope = "initialized";
  }

  @Scoped(MyScope.class)
  public void initMyScope() { // this is a scoped method for @MyScope.
    this.scope = "initialized by @MyScope";
  }

  @Scoped(YourScope.class)
  public void initMineScopeNotYours() { // this is a scoped method for @YourScope.
    this.scope = "initialized by @YourScope";
  }
}

After build the project, MyClass_MyScope, MyClass_HisScope, MyClass_YourScope classes will be auto-generated.

Scoping with a return type and parameters

Methods that have a return type and parameters can be scoped method.
The base method's return type and parameters are must be the same as the scoped method's one.

@MyScope
abstract public class MyClass {
  private String scope;

  public String init(String text) { // this is the base method of the scoped methods.
    this.scope += text;
    return this.scope;
  }

  @Scoped(MyScope.class)
  public String initMyScope(String text) { // this is a scoped method for @MyScope.
    this.scope += text;
    return this.scope;
  }

Abstract class Scoping

MethodScope supports scopping for the abstract class.
This is more clear because the base method of the scoped methods is being explicitly abstract.

@MyScope
@YourScope
abstract public class MyClass {
  private String scope;

  abstract void init();

  @Scoped(MyScope.class)
  public void initMyScope() { // this is a scoped method for @MyScope.
    this.scope = "initialized by @MyScope";
  }

  @Scoped(YourScope.class)
  public void initMineScopeNotYours() { // this is a scoped method for @YourScope.
    this.scope = "initialized by @YourScope";
  }

Android Project Usage

MethodScope is useful to the Android project for creating similar screens.

Basic example

@MyScope
@TestScope(deeplink = DeepLink("https://github.com/skydoves"))
abstract class MainActivity : AppCompatActivity() {

    private lateinit var hello: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init() // calls the base method. this will call each scoped method in the scoped class.
    }

    open fun init() {
        hello = "hello, "
    }

    @Scoped(MyScope::class)
    fun initMyScope() {
        setContentView(R.layout.activity_main_myscope) // setContentView for MyScope.
        text_message_myscope.text = hello + "MyScope" // changes text of the textView.
    }

    @Scoped(TestScope::class)
    fun initTestScope() {
        setContentView(R.layout.activity_main_testscope) // setContentView for TestScope.
        text_message_testscope.text = hello + "TestScope" // changes text of the textView.
    }
}

And on the AndroidManifest.xml, we should declare new generated activities.

<activity android:name=".MainActivity_MyScope" />
<activity android:name=".MainActivity_TestScope" />

And then we can use the new activities.

val intent = Intent(this, MainActivity_MyScope::class.java)
startActivity(intent)

License

Copyright 2019 skydoves

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.