use of com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo in project intellij-community by JetBrains.
the class ChangeSignatureBaseTest method doTest.
protected void doTest(@Nullable String returnType, @Nullable final String[] parameters, @Nullable final String[] exceptions, boolean delegate) {
GenParams genParams = parameters == null ? new SimpleParameterGen() : method -> {
ParameterInfoImpl[] parameterInfos = new ParameterInfoImpl[parameters.length];
for (int i = 0; i < parameters.length; i++) {
PsiType type = myFactory.createTypeFromText(parameters[i], method);
parameterInfos[i] = new ParameterInfoImpl(-1, "p" + (i + 1), type);
}
return parameterInfos;
};
GenExceptions genExceptions = exceptions == null ? new SimpleExceptionsGen() : method -> {
ThrownExceptionInfo[] exceptionInfos = new ThrownExceptionInfo[exceptions.length];
for (int i = 0; i < exceptions.length; i++) {
PsiType type = myFactory.createTypeFromText(exceptions[i], method);
exceptionInfos[i] = new JavaThrownExceptionInfo(-1, (PsiClassType) type);
}
return exceptionInfos;
};
doTest(null, null, returnType, genParams, genExceptions, delegate);
}
use of com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo in project intellij-community by JetBrains.
the class ChangeSignaturePropagationTest method generateExceptionInfos.
private static ThrownExceptionInfo[] generateExceptionInfos(PsiMethod method, ThrownExceptionInfo[] newExceptions) {
final PsiClassType[] exceptions = method.getThrowsList().getReferencedTypes();
ThrownExceptionInfo[] result = new ThrownExceptionInfo[exceptions.length + newExceptions.length];
for (int i = 0; i < exceptions.length; i++) {
result[i] = new JavaThrownExceptionInfo(i);
}
System.arraycopy(newExceptions, 0, result, exceptions.length, newExceptions.length);
return result;
}
use of com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo in project intellij-community by JetBrains.
the class CreateParameterForFieldIntention method addParameter.
private static void addParameter(final GrField selectedValue, final GrMethod constructor, final Project project) {
List<GrParameterInfo> parameters = new ArrayList<>();
GrParameter[] constructorParameters = constructor.getParameters();
for (int i = 0; i < constructorParameters.length; i++) {
parameters.add(new GrParameterInfo(constructorParameters[i], i));
}
final String[] suggestedNames = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.PARAMETER, selectedValue.getName(), null, null).names;
final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(constructor, Collections.<String>emptyList(), false);
String parameterName = ContainerUtil.find(suggestedNames, name -> !nameValidator.validateName(name, false).isEmpty());
if (parameterName == null) {
parameterName = nameValidator.validateName(suggestedNames[0], true);
}
parameters.add(new GrParameterInfo(parameterName, "null", "", selectedValue.getTypeGroovy(), -1, false));
PsiClassType[] exceptionTypes = constructor.getThrowsList().getReferencedTypes();
ThrownExceptionInfo[] thrownExceptionInfos = new ThrownExceptionInfo[exceptionTypes.length];
for (int i = 0; i < exceptionTypes.length; i++) {
new JavaThrownExceptionInfo(i, exceptionTypes[i]);
}
final GrChangeInfoImpl grChangeInfo = new GrChangeInfoImpl(constructor, null, null, constructor.getName(), parameters, thrownExceptionInfos, false);
final String finalParameterName = parameterName;
final GrChangeSignatureProcessor processor = new GrChangeSignatureProcessor(project, grChangeInfo) {
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
super.performRefactoring(usages);
final GrOpenBlock block = constructor.getBlock();
LOG.assertTrue(block != null);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final String text;
if (StringUtil.equals(selectedValue.getName(), finalParameterName)) {
text = "this." + selectedValue.getName() + " = " + finalParameterName;
} else {
text = selectedValue.getName() + " = " + finalParameterName;
}
final GrStatement assignment = factory.createStatementFromText(text);
final GrStatement statement = block.addStatementBefore(assignment, null);
final GrReferenceExpression ref = (GrReferenceExpression) ((GrAssignmentExpression) statement).getLValue();
if (!PsiManager.getInstance(project).areElementsEquivalent(ref.resolve(), selectedValue)) {
PsiUtil.qualifyMemberReference(ref, selectedValue, selectedValue.getName());
}
}
};
processor.run();
}
Aggregations