Skip to content

Commit

Permalink
修正:
Browse files Browse the repository at this point in the history
       动态新增类的时候会报Write access is allowed inside write-action only
  • Loading branch information
zzz40500 committed Nov 8, 2016
1 parent dbde81d commit d5a16b4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 98 deletions.
49 changes: 27 additions & 22 deletions src/main/java/org/gsonformat/intellij/ConvertBridge.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.gsonformat.intellij;

import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiClassReferenceType;
import org.apache.http.util.TextUtils;
import org.gsonformat.intellij.action.ClassProvider;
import org.gsonformat.intellij.action.DataWriter;
import org.gsonformat.intellij.common.StringUtils;
import org.gsonformat.intellij.common.Utils;
Expand Down Expand Up @@ -410,26 +410,31 @@ private void handleVirgoMode(JSONObject json, List<String> fieldList) {
}

private void handleNormal(JSONObject json, List<String> generateFiled) {
if (targetClass == null) {
try {
targetClass = (PsiClass) new ClassProvider(project, file).execute(generateClassName).getResultObject();
} catch (Throwable throwable) {
handlePathError(throwable);
}
}
if (targetClass != null) {
generateClassEntity.setPsiClass(targetClass);
try {
generateClassEntity.addAllFields(createFields(json, generateFiled, generateClassEntity));
operator.setVisible(false);
DataWriter dataWriter = new DataWriter(file, project, targetClass);
dataWriter.execute(generateClassEntity);
Config.getInstant().saveCurrentPackPath(packageName);
operator.dispose();
} catch (Exception e) {
throw e;
WriteCommandAction.runWriteCommandAction(project, new Runnable() {
@Override
public void run() {
if (targetClass == null) {
try {
targetClass = PsiClassUtil.getPsiClass(file, project, generateClassName);
} catch (Throwable throwable) {
handlePathError(throwable);
}
}
if (targetClass != null) {
generateClassEntity.setPsiClass(targetClass);
try {
generateClassEntity.addAllFields(createFields(json, generateFiled, generateClassEntity));
operator.setVisible(false);
DataWriter dataWriter = new DataWriter(file, project, targetClass);
dataWriter.execute(generateClassEntity);
Config.getInstant().saveCurrentPackPath(packageName);
operator.dispose();
} catch (Exception e) {
throw e;
}
}
}
}
});
}

private List<String> collectGenerateFiled(JSONObject json) {
Expand Down Expand Up @@ -661,8 +666,8 @@ private FieldEntity handleJSONArray(ClassEntity parentClass, JSONArray jsonArray
FieldEntity fieldEntity;
if (jsonArray.length() > 0) {
Object item = jsonArray.get(0);
if(item instanceof JSONObject){
item=getJsonObject(jsonArray);
if (item instanceof JSONObject) {
item = getJsonObject(jsonArray);
}
fieldEntity = listTypeByValue(parentClass, key, item, deep);
} else {
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/org/gsonformat/intellij/action/ClassProvider.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gsonformat.intellij.common;

import com.intellij.ide.util.DirectoryUtil;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
Expand Down Expand Up @@ -128,8 +129,8 @@ public static PsiClass getPsiClass(PsiFile psiFile, Project project, String gene
}
}
}
psiClass = JavaDirectoryService.getInstance().createClass(psiDirectory, className);

psiClass = JavaDirectoryService.getInstance().createClass(psiDirectory, className);
FileEditorManager manager = FileEditorManager.getInstance(project);
manager.openFile(psiClass.getContainingFile().getVirtualFile(), true, true);
}
Expand Down
86 changes: 47 additions & 39 deletions src/main/java/org/gsonformat/intellij/ui/FieldsDialog.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gsonformat.intellij.ui;

import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElementFactory;
Expand Down Expand Up @@ -113,46 +114,53 @@ public void actionPerformed(ActionEvent e) {
private void onOK() {

this.setAlwaysOnTop(false);
if (psiClass == null) {
try {
psiClass = PsiClassUtil.getPsiClass(file, project, generateClassStr);
} catch (Throwable throwable) {
throwable.printStackTrace();
operator.showError(ConvertBridge.Error.DATA_ERROR);
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
throwable.printStackTrace(printWriter);
printWriter.close();
operator.setErrorInfo(writer.toString());
operator.setVisible(true);
operator.showError(ConvertBridge.Error.PATH_ERROR);
}
}
if (psiClass != null) {
String[] arg = generateClassStr.split("\\.");
if (arg.length > 1) {
Config.getInstant().setEntityPackName(generateClassStr.substring(0, generateClassStr.length() - arg[arg.length - 1].length()));
Config.getInstant().save();
}
try {
setVisible(false);
DataWriter dataWriter = new DataWriter(file, project, psiClass);
dataWriter.execute(classEntity);
Config.getInstant().saveCurrentPackPath(StringUtils.getPackage(generateClassStr));
operator.dispose();
dispose();
} catch (Exception e) {
e.printStackTrace();
operator.showError(ConvertBridge.Error.PARSE_ERROR);
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.close();
operator.setErrorInfo(writer.toString());
operator.setVisible(true);
dispose();
WriteCommandAction.runWriteCommandAction(project, new Runnable() {

@Override
public void run() {
if (psiClass == null) {
try {
psiClass = PsiClassUtil.getPsiClass(file, project, generateClassStr);
} catch (Throwable throwable) {
throwable.printStackTrace();
operator.showError(ConvertBridge.Error.DATA_ERROR);
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
throwable.printStackTrace(printWriter);
printWriter.close();
operator.setErrorInfo(writer.toString());
operator.setVisible(true);
operator.showError(ConvertBridge.Error.PATH_ERROR);
}
}

if (psiClass != null) {
String[] arg = generateClassStr.split("\\.");
if (arg.length > 1) {
Config.getInstant().setEntityPackName(generateClassStr.substring(0, generateClassStr.length() - arg[arg.length - 1].length()));
Config.getInstant().save();
}
try {
setVisible(false);
DataWriter dataWriter = new DataWriter(file, project, psiClass);
dataWriter.execute(classEntity);
Config.getInstant().saveCurrentPackPath(StringUtils.getPackage(generateClassStr));
operator.dispose();
dispose();
} catch (Exception e) {
e.printStackTrace();
operator.showError(ConvertBridge.Error.PARSE_ERROR);
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.close();
operator.setErrorInfo(writer.toString());
operator.setVisible(true);
dispose();
}
}
}
}
});
}

private void onCancel() {
Expand Down

0 comments on commit d5a16b4

Please sign in to comment.