Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for AutoParcel and AutoParcelGson both libraries are used on Android. #2

Merged
merged 5 commits into from
Feb 1, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Generating the correct names based on the library being used. For exa…
…mple for AutoParcel the builder should be annotated with @AutoParcel.Builder and the builder method should return "new AutoParcel_ClassName.Builder();"
vjames19 committed Jan 31, 2016
commit 5c69d2151bcde794a328381cb527058ae74dd43c
Original file line number Diff line number Diff line change
@@ -43,16 +43,7 @@ public void update(AnActionEvent e) {
return;
}

PsiClass targetClass = factory.getTargetClass();

if(targetClass == null) {
e.getPresentation().setEnabled(false);
return;
}

boolean isAnnotated = factory.containsAutoValueAnnotation();
e.getPresentation().setEnabled(isAnnotated);

e.getPresentation().setEnabled(true);
}

private void processClass(final AutoValueFactory factory) {
37 changes: 32 additions & 5 deletions src/com/afcastano/intellij/autovalue/actions/AutoValueFactory.java
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
@@ -21,6 +22,8 @@
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.ImmutableList;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

@@ -41,7 +44,7 @@ public class AutoValueFactory {
private PsiElementFactory factory;
private PsiJavaFile javaFile;
private Project project;
private PsiClass autoValueAnnotationClass;
private PsiAnnotation autoValueAnnotation;
private PsiMethod buildMehtod;

public AutoValueFactory(AnActionEvent e) {
@@ -62,9 +65,19 @@ public AutoValueFactory(AnActionEvent e) {
}

this.targetClass = loadTargetClass(editor, javaFile);

if (targetClass == null) {
throw new RuntimeException("Target class not found");
}

this.autoValueAnnotation = findAutoValueAnnotationClass(targetClass);

if (autoValueAnnotation == null) {
throw new RuntimeException("No auto value annotation detected");
}

this.project = e.getProject();
factory = JavaPsiFacade.getElementFactory(project);

}

public PsiClass getTargetClass() {
@@ -98,7 +111,7 @@ public PsiClass getBuilderClass() {
modifierList.setModifierProperty("public", true);
modifierList.setModifierProperty("static", true);
modifierList.setModifierProperty("abstract", true);
modifierList.addAnnotation("AutoValue.Builder");
modifierList.addAnnotation(autoValueAnnotation.getQualifiedName() + ".Builder");

this.builderClass = newBuilderClass;

@@ -152,8 +165,10 @@ public PsiMethod newBuilderFactoryMethod() {

generatedName = generatedName + getTargetClass().getName();

PsiStatement returnStatement = factory
.createStatementFromText("return new AutoValue_" + generatedName + ".Builder();", getTargetClass());
String autoValueAnnotationName = StringUtil.getShortName(autoValueAnnotation.getQualifiedName());
String returnStatementText = "return new " + autoValueAnnotationName + "_" + generatedName + ".Builder();";

PsiStatement returnStatement = factory.createStatementFromText(returnStatementText, getTargetClass());

builderMethod.getBody().add(returnStatement);
return builderMethod;
@@ -182,6 +197,18 @@ public boolean containsAutoValueAnnotation() {
return false;
}

@Nullable
private PsiAnnotation findAutoValueAnnotationClass(PsiClass targetClass) {
for (String autoValueAnnotationName : SUPPORTED_AUTOVALUE_LIBRARIES) {
PsiAnnotation autoValueAnnotation = getTargetClass().getModifierList().findAnnotation(autoValueAnnotationName);
if (autoValueAnnotation != null) {
return autoValueAnnotation;
}
}

return null;
}

private PsiClass loadTargetClass(Editor editor, PsiJavaFile javaFile) {
int caretOffset = editor.getCaretModel().getOffset();
PsiElement element = javaFile.findElementAt(caretOffset);