Skip to content

Commit

Permalink
Ignores android.os.Parcelable and java.util.* from interface method g…
Browse files Browse the repository at this point in the history
…eneration. afcastano#14
  • Loading branch information
afcastano committed Feb 7, 2017
1 parent 1ed53c5 commit 4dbc805
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 11 deletions.
2 changes: 1 addition & 1 deletion AutoValuePlugin.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="jdk" jdkName="IntelliJ IDEA Community Edition IC-143.382.35" jdkType="IDEA JDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Release 20170208
----------------
* 2017-02-08: Ignores `android.os.Parcelable` and `java.util.*` from interface method generation. [#14](https://github.com/afcastano/AutoValuePlugin/issues/14)

Release 20170203
-------
* 2017-02-03: Ignores `toBuilder` based on return type. [#15](https://github.com/afcastano/AutoValuePlugin/issues/15)
Expand Down
2 changes: 1 addition & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<id>com.afcastano.intellij.autovalue</id>
<name>AutoValue plugin</name>
<version>1.0.1</version>
<version>1.0.2</version>
<vendor email="[email protected]" url="https://github.com/afcastano/AutoValuePlugin">AutoValue plugin</vendor>

<description><![CDATA[
Expand Down
Binary file modified dist/AutoValuePlugin.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AutoValueFactory {
"com.google.auto.value.AutoValue",
"auto.parcel.AutoParcel",
"auto.parcelgson.AutoParcelGson"
);
);

private PsiType builderType;
private PsiClass builderClass;
Expand Down Expand Up @@ -435,7 +435,7 @@ public boolean containsCreateMethod() {
@NotNull
public List<PsiMethod> getAbstractGetters(PsiClass targetClass) {
List<PsiMethod> abstractGetters = new ArrayList<>();
abstractGetters.addAll(implementingInterfaceGetters(targetClass));
abstractGetters.addAll(getGettersFromAllExtendedInterfaces(targetClass));

for (PsiMethod psiMethod : targetClass.getMethods()) {
abstractGetters = removeMethodByName(psiMethod.getName(), abstractGetters);
Expand Down Expand Up @@ -471,14 +471,17 @@ private boolean containsMethodByName(String name, List<PsiMethod> methods) {
return false;
}

private List<PsiMethod> implementingInterfaceGetters(PsiClass targetClass) {
private List<PsiMethod> getGettersFromAllExtendedInterfaces(PsiClass targetClass) {
List<PsiMethod> abstractGetters = new ArrayList<>();
PsiClass[] interfaces = targetClass.getInterfaces();

for (PsiClass interf : interfaces) {
List<PsiMethod> newGetters = getInterfaceGetters(interf);
for (PsiClass currentInterface : interfaces) {
if (isBlackListedInterface(currentInterface)) {
continue;
}
List<PsiMethod> interfaceGetters = gettersFromInterface(currentInterface);

for (PsiMethod method : newGetters) {
for (PsiMethod method : interfaceGetters) {
//If exists don't add it twice
if (containsMethodByName(method.getName(), abstractGetters)) {
continue;
Expand All @@ -492,10 +495,20 @@ private List<PsiMethod> implementingInterfaceGetters(PsiClass targetClass) {
return abstractGetters;
}

private List<PsiMethod> getInterfaceGetters(PsiClass interfaceClass) {
private boolean isBlackListedInterface(PsiClass psiInterface) {
String qualifiedName = psiInterface.getQualifiedName();
if(qualifiedName == null) {
return false;
}

return qualifiedName.equals("android.os.Parcelable")
|| qualifiedName.startsWith("java.util.");
}

private List<PsiMethod> gettersFromInterface(PsiClass interfaceClass) {
List<PsiMethod> abstractGetters = new ArrayList<>();

abstractGetters.addAll(implementingInterfaceGetters(interfaceClass));
abstractGetters.addAll(getGettersFromAllExtendedInterfaces(interfaceClass));

for (PsiMethod psiMethod : interfaceClass.getMethods()) {
//If exists don't add it twice
Expand Down Expand Up @@ -541,4 +554,4 @@ public static GetterProperties fromGetter(PsiMethod getter) {
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ public void testGenerateBuilderWithInterfaceCorrectly() {
AUTOVALUE);
}

public void testGenerateBuilderWithInterfaceIgnoringBlacklistedCorrectly() {
utils.runGenerateBuilderActions("generatebuilder/withinterfaceignoresblacklisted/BasicTestFile_expected.java",
"generatebuilder/withinterfaceignoresblacklisted/BasicTestFile.java",
"test/Interface1.java",
"java/util/Map.java",
"android/os/Parcelable.java",
AUTOVALUE);
}

public void testGenerateBuilderWithInterfaceHierarchy() {
utils.runGenerateBuilderActions("generatebuilder/withinterfacehierarchy/BasicTestFile_expected.java",
"generatebuilder/withinterfacehierarchy/BasicTestFile.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,13 @@ public void testGenerateBuilderWithInterfaceHierarchy() {
AUTOVALUE);
}

public void testGenerateBuilderWithInterfaceIgnoringBlacklisted() {
utils.runGenerateCreateMethodActions("generatecreatemethod/withinterfaceignoresblacklisted/BasicTestFile_expected.java",
"generatecreatemethod/withinterfaceignoresblacklisted/BasicTestFile.java",
"test/Interface1.java",
"java/util/Map.java",
"android/os/Parcelable.java",
AUTOVALUE);
}

}
5 changes: 5 additions & 0 deletions testData/android/os/Parcelable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package android.os;

public interface Parcelable {
int parcelableMethod();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import com.google.auto.value.AutoValue;
import test.Interface1;
import android.os.Parcelable;
import java.util.Map;

@AutoValue
public abstract class BasicTestFile implements Interface1, Parcelable, Map<String, String> {

<caret>
public abstract String value();

public Integer notAbstract() {
return 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import com.google.auto.value.AutoValue;
import test.Interface1;
import android.os.Parcelable;
import java.util.Map;

@AutoValue
public abstract class BasicTestFile implements Interface1, Parcelable, Map<String, String> {


public abstract String value();

public Integer notAbstract() {
return 0;
}

public static Builder builder() {
return new AutoValue_BasicTestFile.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder method1Interface1(int method1Interface1);

public abstract Builder method2Interface1(int method2Interface1);

public abstract Builder value(String value);

public abstract BasicTestFile build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import com.google.auto.value.AutoValue;
import test.Interface1;
import android.os.Parcelable;
import java.util.Map;

@AutoValue
public abstract class BasicTestFile implements Interface1, Parcelable, Map<String, String> {

<caret>
public abstract String value();

public int method1Interface2() {
return 3;
}

public Integer notAbstract() {
return 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import com.google.auto.value.AutoValue;
import test.Interface1;
import android.os.Parcelable;
import java.util.Map;

@AutoValue
public abstract class BasicTestFile implements Interface1, Parcelable, Map<String, String> {


public abstract String value();

public int method1Interface2() {
return 3;
}

public Integer notAbstract() {
return 0;
}

public static BasicTestFile create(int method1Interface1, int method2Interface1, String value) {
return new AutoValue_BasicTestFile(method1Interface1, method2Interface1, value);
}

}
5 changes: 5 additions & 0 deletions testData/java/util/Map.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package java.util;

public interface Map<K,V> {
String mapMethod();
}

0 comments on commit 4dbc805

Please sign in to comment.