Skip to content

Commit

Permalink
Merge of code changed proposed by EasyPark
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkucherenko committed Jun 23, 2020
1 parent 5728b3e commit 7efce64
Show file tree
Hide file tree
Showing 20 changed files with 469 additions and 159 deletions.
117 changes: 106 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ include ':modules:autoproxy:autoproxy-processor'
## Step #3: Declare proxy class specifics

```java
@AutoProxy
@AutoProxy(flags = AutoProxy.Flags.ALL)
public interface MvpView {
/** Returns NULL if predicate returns False. */
@AutoProxy.Yield(Returns.NULL)
Expand Down Expand Up @@ -220,71 +220,96 @@ public abstract class Proxy_MvpView implements MvpView {
}

public final Observable<Boolean> dummyCall(final List<String> generic) {
if (!predicate( Methods.DUMMYCALL, generic )) {
if (!predicate( M.DUMMYCALL, generic )) {
// @com.olku.annotations.AutoProxy.Yield(adapter=com.olku.generators.RetRxGenerator.class, value="empty")
return Observable.empty();
}
return this.inner.dummyCall(generic);
}

public final Observable<Boolean> dummyCall(final String message, final List<String> args) {
if (!predicate( Methods.DUMMYCALL, message, args )) {
if (!predicate( M.DUMMYCALL, message, args )) {
// @com.olku.annotations.AutoProxy.Yield
throw new UnsupportedOperationException("cannot resolve return value.");
}
return this.inner.dummyCall(message, args);
}

public final Observable<Boolean> dummyCall(final String message, final Object... args) {
if (!predicate( Methods.DUMMYCALL, message, args )) {
if (!predicate( M.DUMMYCALL, message, args )) {
// @com.olku.annotations.AutoProxy.Yield(adapter=com.olku.generators.RetRxGenerator.class, value="error")
return Observable.error(new UnsupportedOperationException("unsupported method call"));
}
return this.inner.dummyCall(message, args);
}

public final double numericCall() {
if (!predicate( Methods.NUMERICCALL )) {
if (!predicate( M.NUMERICCALL )) {
// @com.olku.annotations.AutoProxy.Yield("0")
return 0;
}
return this.inner.numericCall();
}

public final boolean booleanCall() {
if (!predicate( Methods.BOOLEANCALL )) {
if (!predicate( M.BOOLEANCALL )) {
// @com.olku.annotations.AutoProxy.Yield("false")
return false;
}
return this.inner.booleanCall();
}

public final boolean dispatchDeepLink(@NonNull final Uri deepLink) {
if (!predicate( Methods.DISPATCHDEEPLINK, deepLink )) {
if (!predicate( M.DISPATCHDEEPLINK, deepLink )) {
// @com.olku.annotations.AutoProxy.Yield("direct")
// direct call, ignore predicate result
}
return this.inner.dispatchDeepLink(deepLink);
}

public final Observable<Boolean> startHearthAnimation() {
if (!predicate( Methods.STARTHEARTHANIMATION )) {
if (!predicate( M.STARTHEARTHANIMATION )) {
// @com.olku.annotations.AutoProxy.Yield(adapter=com.olku.generators.JustRxGenerator.class, value="true")
return Observable.just(true);
}
return this.inner.startHearthAnimation();
}

@StringDef({Methods.BOOLEANCALL, Methods.DISPATCHDEEPLINK, Methods.DUMMYCALL, Methods.NUMERICCALL, Methods.STARTHEARTHANIMATION})
public @interface Methods {
@StringDef({M.BOOLEANCALL, M.DISPATCHDEEPLINK_DEEPLINK, M.DUMMYCALL, M.DUMMYCALL_GENERIC, M.DUMMYCALL_MESSAGE_ARGS, M.NUMERICCALL, M.STARTHEARTHANIMATION})
public @interface M {
/**
* {@link #booleanCall()}
*/
String BOOLEANCALL = "booleanCall";

String DISPATCHDEEPLINK = "dispatchDeepLink";
/**
* {@link #dispatchDeepLink(android.net.Uri)}
*/
String DISPATCHDEEPLINK_DEEPLINK = "dispatchDeepLink_deepLink";

/**
* {@link #dummyCall()}
*/
String DUMMYCALL = "dummyCall";

/**
* {@link #dummyCall(java.util.List<java.lang.String>)}
*/
String DUMMYCALL_GENERIC = "dummyCall_generic";

/**
* {@link #dummyCall(java.lang.String, java.lang.Object[])}
*/
String DUMMYCALL_MESSAGE_ARGS = "dummyCall_message_args";

/**
* {@link #numericCall()}
*/
String NUMERICCALL = "numericCall";

/**
* {@link #startHearthAnimation()}
*/
String STARTHEARTHANIMATION = "startHearthAnimation";
}
}
Expand All @@ -311,6 +336,76 @@ public abstract class Proxy_MvpView implements MvpView {

```

## Customization of Generated Code

By providing special flags you can customize output of AutoProxy generator:

```kotlin
@AutoProxy(flags = AutoProxy.Flags.ALL)
abstract class KotlinAbstractMvpView {
/* ... */
}
```

Outputs:

```java
public abstract <T> T afterCall(@M @NonNull final String methodName, final T result);

/**
* Copy this declaration to fix method demands for old APIs:
*
* <pre>
* package java.util.function;
*
* public interface BiFunction&lt;T, U, R&gt; {
* R apply(T t, U u);
* }
* </pre>
*/
public static KotlinAbstractMvpView create(final KotlinAbstractMvpView instance,
final BiFunction<String, Object[], Boolean> action) {
return new Proxy_KotlinAbstractMvpView(instance) {

@Override
public boolean predicate(final String methodName, final Object... args) {
return action.apply(methodName, args);
}

@Override
public <T> T afterCall(final String methodName, final T result) {
return result;
};
};
}

public <T> T dispatchByName(@M @NonNull final String methodName, final Object... args) {
final Object result;
if(M.BOOLEANCALL.equals(methodName)) {
return (T)(result = this.inner.booleanCall());
}
if(M.DISPATCHDEEPLINK_DEEPLINK.equals(methodName)) {
return (T)(result = this.inner.dispatchDeepLink((android.net.Uri)args[0] /*deepLink*/));
}
if(M.DUMMYCALL.equals(methodName)) {
return (T)(result = this.inner.dummyCall());
}
if(M.DUMMYCALL_GENERIC.equals(methodName)) {
return (T)(result = this.inner.dummyCall((java.util.List<java.lang.String>)args[0] /*generic*/));
}
if(M.DUMMYCALL_MESSAGE_ARGS.equals(methodName)) {
return (T)(result = this.inner.dummyCall((java.lang.String)args[0] /*message*/, (java.lang.Object[])args[1] /*args*/));
}
if(M.NUMERICCALL.equals(methodName)) {
return (T)(result = this.inner.numericCall());
}
if(M.STARTHEARTHANIMATION.equals(methodName)) {
return (T)(result = this.inner.startHearthAnimation());
}
return (T)null;
}
```

# Troubles

http://www.vogella.com/tutorials/GitSubmodules/article.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ abstract class Common implements AutoProxyClassGenerator {
int AFTER_CALL = 0x0002;
/** Compose callByName(...) method that maps string name to a method call. */
int MAPPING = 0x004;

/** Compose all additional methods. */
int ALL = CREATOR | AFTER_CALL | MAPPING;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ public interface AutoProxyClassGenerator {
@NonNull
String getErrors();

/** Get file Name. */
@NonNull
String getName();

/** Get generated elements. */
@NonNull
List<Element> getOriginating();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.olku.annotations;

import androidx.annotation.*;
import androidx.annotation.StringDef;

import java.lang.annotation.Retention;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.olku.annotations;

import androidx.annotation.*;
import androidx.annotation.StringDef;

import java.lang.annotation.Retention;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.olku.generators;

import androidx.annotation.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.olku.annotations.RetBool;
import com.squareup.javapoet.MethodSpec;
import com.sun.tools.javac.code.Type;

import com.olku.annotations.RetBool;

/** Compose return types for boolean. */
public class RetBoolGenerator implements ReturnsPoet {
@NonNull
Expand All @@ -15,7 +15,7 @@ public static RetBoolGenerator getInstance() {
}

public boolean compose(@NonNull final Type returnType,
@RetBool final String type,
@Nullable @RetBool final String type,
@NonNull final MethodSpec.Builder builder) {
if (RetBool.FALSE.equals(type)) {
builder.addStatement("return false");
Expand All @@ -29,6 +29,6 @@ public boolean compose(@NonNull final Type returnType,
}

private static final class Singleton {
static final RetBoolGenerator INSTANCE = new RetBoolGenerator();
/* package */ static final RetBoolGenerator INSTANCE = new RetBoolGenerator();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.olku.generators;

import androidx.annotation.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.olku.annotations.RetNumber;
import com.squareup.javapoet.MethodSpec;
import com.sun.tools.javac.code.Type;

import com.olku.annotations.RetNumber;

import java.util.Map;
import java.util.TreeMap;

Expand All @@ -30,7 +30,7 @@ public static RetNumberGenerator getInstance() {
}

public boolean compose(@NonNull final Type returnType,
@NonNull @RetNumber final String type,
@Nullable @RetNumber final String type,
@NonNull final MethodSpec.Builder builder) {
final Class<?> output = PRIMITIVES.get(returnType.toString());

Expand All @@ -56,6 +56,6 @@ public boolean compose(@NonNull final Type returnType,
}

private static final class Singleton {
static final RetNumberGenerator INSTANCE = new RetNumberGenerator();
/* package */ static final RetNumberGenerator INSTANCE = new RetNumberGenerator();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.olku.generators;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.olku.annotations.Returns;
import com.squareup.javapoet.MethodSpec;
import com.sun.tools.javac.code.Type;

import com.olku.annotations.Returns;

/** Compose return types for boolean. */
public class ReturnsGenerator implements ReturnsPoet {
@NonNull
Expand All @@ -15,7 +15,7 @@ public static ReturnsGenerator getInstance() {
}

public boolean compose(@NonNull final Type returnType,
@NonNull @Returns final String type,
@Nullable @Returns final String type,
@NonNull final MethodSpec.Builder builder) {
// empty string
if (Returns.EMPTY.equals(type)) {
Expand Down Expand Up @@ -52,6 +52,6 @@ public boolean compose(@NonNull final Type returnType,
}

private static final class Singleton {
static final ReturnsGenerator INSTANCE = new ReturnsGenerator();
/* package */ static final ReturnsGenerator INSTANCE = new ReturnsGenerator();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.olku.generators;

import androidx.annotation.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.squareup.javapoet.MethodSpec;
import com.sun.tools.javac.code.Type;
Expand All @@ -9,6 +10,6 @@
public interface ReturnsPoet {
/** Compose return statement for provided method based on return type and modifier. */
boolean compose(@NonNull final Type returnType,
@NonNull final String modifier,
@Nullable final String modifier,
@NonNull final MethodSpec.Builder builder);
}
2 changes: 1 addition & 1 deletion autoproxy-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {

/* CODE GENERATION */
implementation 'com.google.auto.service:auto-service:1.0-rc7'
kapt 'com.google.auto.service:auto-service:1.0-rc6'
kapt 'com.google.auto.service:auto-service:1.0-rc7'
implementation 'com.squareup:javapoet:1.13.0'

implementation "androidx.annotation:annotation:${supportVersion}"
Expand Down
Loading

0 comments on commit 7efce64

Please sign in to comment.