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 @Override
public ASTNode getNode() { public ASTNode getNode() {
if (null == myASTNode) { if (null == myASTNode) {
myASTNode = getOrCreateMyPsiMethod().getNode(); final PsiElement myPsiMethod = getOrCreateMyPsiMethod();
myASTNode = null == myPsiMethod ? null : myPsiMethod.getNode();
} }
return myASTNode; return myASTNode;
} }
Expand All @@ -185,34 +186,41 @@ private String getAllModifierProperties(LightModifierList modifierList) {
} }


private PsiMethod rebuildMethodFromString() { private PsiMethod rebuildMethodFromString() {
final StringBuilder methodTextDeclaration = new StringBuilder(); PsiMethod result;
methodTextDeclaration.append(getAllModifierProperties((LightModifierList) getModifierList())); try {
PsiType returnType = getReturnType(); final StringBuilder methodTextDeclaration = new StringBuilder();
if (null != returnType) { methodTextDeclaration.append(getAllModifierProperties((LightModifierList) getModifierList()));
methodTextDeclaration.append(returnType.getCanonicalText()).append(' '); PsiType returnType = getReturnType();
} if (null != returnType) {
methodTextDeclaration.append(getName()); methodTextDeclaration.append(returnType.getCanonicalText()).append(' ');
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(getName());
} methodTextDeclaration.append('(');
methodTextDeclaration.append(')'); if (getParameterList().getParametersCount() > 0) {
methodTextDeclaration.append('{').append(" ").append('}'); 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()); result = elementFactory.createMethodFromText(methodTextDeclaration.toString(), getContainingClass());
if (null != getBody()) { if (null != getBody()) {
methodFromText.getBody().replace(getBody()); result.getBody().replace(getBody());
}
} catch (Exception ex) {
result = null;
} }
return methodFromText; return result;
} }


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


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


public String toString() { public String toString() {
Expand Down

0 comments on commit 7967509

Please sign in to comment.