Skip to content

Commit

Permalink
catch exception on rebuild #523
Browse files Browse the repository at this point in the history
  • Loading branch information
mplushnikov committed Nov 4, 2018
1 parent 01d876b commit 7967509
Showing 1 changed file with 32 additions and 23 deletions.
Expand Up @@ -163,7 +163,8 @@ public String getText() {
@Override
public ASTNode getNode() {
if (null == myASTNode) {
myASTNode = getOrCreateMyPsiMethod().getNode();
final PsiElement myPsiMethod = getOrCreateMyPsiMethod();
myASTNode = null == myPsiMethod ? null : myPsiMethod.getNode();
}
return myASTNode;
}
Expand All @@ -185,34 +186,41 @@ private String getAllModifierProperties(LightModifierList modifierList) {
}

private PsiMethod rebuildMethodFromString() {
final StringBuilder methodTextDeclaration = new StringBuilder();
methodTextDeclaration.append(getAllModifierProperties((LightModifierList) getModifierList()));
PsiType returnType = getReturnType();
if (null != returnType) {
methodTextDeclaration.append(returnType.getCanonicalText()).append(' ');
}
methodTextDeclaration.append(getName());
methodTextDeclaration.append('(');
if (getParameterList().getParametersCount() > 0) {
for (PsiParameter parameter : getParameterList().getParameters()) {
methodTextDeclaration.append(parameter.getType().getCanonicalText()).append(' ').append(parameter.getName()).append(',');
PsiMethod result;
try {
final StringBuilder methodTextDeclaration = new StringBuilder();
methodTextDeclaration.append(getAllModifierProperties((LightModifierList) getModifierList()));
PsiType returnType = getReturnType();
if (null != returnType) {
methodTextDeclaration.append(returnType.getCanonicalText()).append(' ');
}
methodTextDeclaration.deleteCharAt(methodTextDeclaration.length() - 1);
}
methodTextDeclaration.append(')');
methodTextDeclaration.append('{').append(" ").append('}');
methodTextDeclaration.append(getName());
methodTextDeclaration.append('(');
if (getParameterList().getParametersCount() > 0) {
for (PsiParameter parameter : getParameterList().getParameters()) {
methodTextDeclaration.append(parameter.getType().getCanonicalText()).append(' ').append(parameter.getName()).append(',');
}
methodTextDeclaration.deleteCharAt(methodTextDeclaration.length() - 1);
}
methodTextDeclaration.append(')');
methodTextDeclaration.append('{').append(" ").append('}');

final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(getManager().getProject());
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(getManager().getProject());

final PsiMethod methodFromText = elementFactory.createMethodFromText(methodTextDeclaration.toString(), getContainingClass());
if (null != getBody()) {
methodFromText.getBody().replace(getBody());
result = elementFactory.createMethodFromText(methodTextDeclaration.toString(), getContainingClass());
if (null != getBody()) {
result.getBody().replace(getBody());
}
} catch (Exception ex) {
result = null;
}
return methodFromText;
return result;
}

@Override
public PsiElement copy() {
return getOrCreateMyPsiMethod().copy();
final PsiElement myPsiMethod = getOrCreateMyPsiMethod();
return null == myPsiMethod ? null : myPsiMethod.copy();
}

private PsiElement getOrCreateMyPsiMethod() {
Expand All @@ -225,7 +233,8 @@ private PsiElement getOrCreateMyPsiMethod() {
@NotNull
@Override
public PsiElement[] getChildren() {
return getOrCreateMyPsiMethod().getChildren();
final PsiElement myPsiMethod = getOrCreateMyPsiMethod();
return null == myPsiMethod ? PsiElement.EMPTY_ARRAY : myPsiMethod.getChildren();
}

public String toString() {
Expand Down

0 comments on commit 7967509

Please sign in to comment.