use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class FormSourceCodeGenerator method _generate.
private void _generate(final LwRootContainer rootContainer, final Module module) throws CodeGenerationException, IncorrectOperationException {
myBuffer = new StringBuffer();
myIsFirstParameterStack = new Stack<>();
final HashMap<LwComponent, String> component2variable = new HashMap<>();
final TObjectIntHashMap<String> class2variableIndex = new TObjectIntHashMap<>();
final HashMap<String, LwComponent> id2component = new HashMap<>();
if (rootContainer.getComponentCount() != 1) {
throw new CodeGenerationException(null, UIDesignerBundle.message("error.one.toplevel.component.required"));
}
final LwComponent topComponent = (LwComponent) rootContainer.getComponent(0);
String id = Utils.findNotEmptyPanelWithXYLayout(topComponent);
if (id != null) {
throw new CodeGenerationException(id, UIDesignerBundle.message("error.nonempty.xy.panels.found"));
}
final PsiClass classToBind = FormEditingUtil.findClassToBind(module, rootContainer.getClassToBind());
if (classToBind == null) {
throw new ClassToBindNotFoundException(UIDesignerBundle.message("error.class.to.bind.not.found", rootContainer.getClassToBind()));
}
final boolean haveCustomCreateComponents = Utils.getCustomCreateComponentCount(rootContainer) > 0;
if (haveCustomCreateComponents) {
if (FormEditingUtil.findCreateComponentsMethod(classToBind) == null) {
throw new CodeGenerationException(null, UIDesignerBundle.message("error.no.custom.create.method"));
}
myBuffer.append(AsmCodeGenerator.CREATE_COMPONENTS_METHOD_NAME).append("();");
}
generateSetupCodeForComponent(topComponent, component2variable, class2variableIndex, id2component, module, classToBind);
generateComponentReferenceProperties(topComponent, component2variable, class2variableIndex, id2component, classToBind);
generateButtonGroups(rootContainer, component2variable, class2variableIndex, id2component, classToBind);
final String methodText = myBuffer.toString();
final PsiManager psiManager = PsiManager.getInstance(module.getProject());
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory();
PsiClass newClass = (PsiClass) classToBind.copy();
cleanup(newClass);
// [anton] the comments are written according to the SCR 26896
final PsiClass fakeClass = elementFactory.createClassFromText("{\n" + "// GUI initializer generated by " + ApplicationNamesInfo.getInstance().getFullProductName() + " GUI Designer\n" + "// >>> IMPORTANT!! <<<\n" + "// DO NOT EDIT OR ADD ANY CODE HERE!\n" + "" + AsmCodeGenerator.SETUP_METHOD_NAME + "();\n" + "}\n" + "\n" + "/** Method generated by " + ApplicationNamesInfo.getInstance().getFullProductName() + " GUI Designer\n" + " * >>> IMPORTANT!! <<<\n" + " * DO NOT edit this method OR call it in your code!\n" + " * @noinspection ALL\n" + " */\n" + "private void " + AsmCodeGenerator.SETUP_METHOD_NAME + "()\n" + "{\n" + methodText + "}\n", null);
final CodeStyleManager formatter = CodeStyleManager.getInstance(module.getProject());
final JavaCodeStyleManager styler = JavaCodeStyleManager.getInstance(module.getProject());
PsiMethod method = (PsiMethod) newClass.add(fakeClass.getMethods()[0]);
// don't generate initializer block if $$$setupUI$$$() is called explicitly from one of the constructors
boolean needInitializer = true;
boolean needSetupUI = false;
for (PsiMethod constructor : newClass.getConstructors()) {
if (containsMethodIdentifier(constructor, method)) {
needInitializer = false;
} else if (haveCustomCreateComponents && hasCustomComponentAffectingReferences(constructor, newClass, rootContainer, null)) {
needInitializer = false;
needSetupUI = true;
}
}
if (needSetupUI) {
for (PsiMethod constructor : newClass.getConstructors()) {
addSetupUICall(constructor, rootContainer, method);
}
}
if (needInitializer) {
newClass.addBefore(fakeClass.getInitializers()[0], method);
}
@NonNls final String grcMethodText = "/** @noinspection ALL */ public javax.swing.JComponent " + AsmCodeGenerator.GET_ROOT_COMPONENT_METHOD_NAME + "() { return " + topComponent.getBinding() + "; }";
generateMethodIfRequired(newClass, method, AsmCodeGenerator.GET_ROOT_COMPONENT_METHOD_NAME, grcMethodText, topComponent.getBinding() != null);
final String loadButtonTextMethodText = getLoadMethodText(AsmCodeGenerator.LOAD_BUTTON_TEXT_METHOD, AbstractButton.class, module);
generateMethodIfRequired(newClass, method, AsmCodeGenerator.LOAD_BUTTON_TEXT_METHOD, loadButtonTextMethodText, myNeedLoadButtonText);
final String loadLabelTextMethodText = getLoadMethodText(AsmCodeGenerator.LOAD_LABEL_TEXT_METHOD, JLabel.class, module);
generateMethodIfRequired(newClass, method, AsmCodeGenerator.LOAD_LABEL_TEXT_METHOD, loadLabelTextMethodText, myNeedLoadLabelText);
newClass = (PsiClass) styler.shortenClassReferences(newClass);
newClass = (PsiClass) formatter.reformat(newClass);
if (!lexemsEqual(classToBind, newClass)) {
classToBind.replace(newClass);
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class GroovyGenerateConstructorHandler method generateMemberPrototypes.
@Override
@NotNull
protected List<? extends GenerationInfo> generateMemberPrototypes(PsiClass aClass, ClassMember[] members) throws IncorrectOperationException {
final List<? extends GenerationInfo> list = super.generateMemberPrototypes(aClass, members);
List<PsiGenerationInfo<GrMethod>> grConstructors = new ArrayList<>();
final Project project = aClass.getProject();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
for (GenerationInfo generationInfo : list) {
final PsiMember constructorMember = generationInfo.getPsiMember();
assert constructorMember instanceof PsiMethod;
final PsiMethod constructor = (PsiMethod) constructorMember;
final PsiCodeBlock block = constructor.getBody();
assert block != null;
final String constructorName = aClass.getName();
final String body = StringUtil.replace(StringUtil.replace(block.getText(), DEF_PSEUDO_ANNO, ""), ";", "");
final PsiParameterList list1 = constructor.getParameterList();
List<String> parametersNames = new ArrayList<>();
List<String> parametersTypes = new ArrayList<>();
for (PsiParameter parameter : list1.getParameters()) {
final String fullName = parameter.getName();
parametersNames.add(StringUtil.trimStart(fullName, DEF_PSEUDO_ANNO));
parametersTypes.add(fullName.startsWith(DEF_PSEUDO_ANNO) ? null : parameter.getType().getCanonicalText());
}
final String[] paramNames = ArrayUtil.toStringArray(parametersNames);
final String[] paramTypes = ArrayUtil.toStringArray(parametersTypes);
assert constructorName != null;
GrMethod grConstructor = factory.createConstructorFromText(constructorName, paramTypes, paramNames, body);
PsiReferenceList throwsList = grConstructor.getThrowsList();
for (PsiJavaCodeReferenceElement element : constructor.getThrowsList().getReferenceElements()) {
throwsList.add(element);
}
codeStyleManager.shortenClassReferences(grConstructor);
grConstructors.add(new GroovyGenerationInfo<>(grConstructor));
}
return grConstructors;
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class JUnit4AnnotatedMethodInJUnit3TestCaseInspection method addAnnotationIfNotPresent.
private static void addAnnotationIfNotPresent(PsiModifierList modifierList, String qualifiedAnnotationName) {
if (modifierList.findAnnotation(qualifiedAnnotationName) != null) {
return;
}
final PsiAnnotation annotation = modifierList.addAnnotation(qualifiedAnnotationName);
final Project project = modifierList.getProject();
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
codeStyleManager.shortenClassReferences(annotation);
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class MultiCatchCanBeSplitInspection method doFixImpl.
private static void doFixImpl(@NotNull PsiElement element) throws IncorrectOperationException {
final PsiElement parent = element.getParent();
if (!(parent instanceof PsiCatchSection)) {
return;
}
final PsiCatchSection catchSection = (PsiCatchSection) parent;
final PsiElement grandParent = catchSection.getParent();
if (!(grandParent instanceof PsiTryStatement)) {
return;
}
final PsiParameter parameter = catchSection.getParameter();
if (parameter == null) {
return;
}
final PsiType type = parameter.getType();
if (!(type instanceof PsiDisjunctionType)) {
return;
}
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(element.getProject());
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(element.getProject());
for (PsiType disjunction : ((PsiDisjunctionType) type).getDisjunctions()) {
final PsiCatchSection copy = (PsiCatchSection) catchSection.copy();
final PsiTypeElement typeElement = assertNotNull(assertNotNull(copy.getParameter()).getTypeElement());
final PsiTypeElement newTypeElement = factory.createTypeElementFromText(disjunction.getCanonicalText(true), catchSection);
final PsiElement replaced = typeElement.replace(newTypeElement);
grandParent.addBefore(copy, catchSection);
styleManager.shortenClassReferences(replaced);
}
catchSection.delete();
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class PsiReplacementUtil method replaceExpressionAndShorten.
public static PsiElement replaceExpressionAndShorten(@NotNull PsiExpression expression, @NotNull @NonNls String newExpressionText) {
final Project project = expression.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiElementFactory factory = psiFacade.getElementFactory();
final PsiExpression newExpression = factory.createExpressionFromText(newExpressionText, expression);
final PsiElement replacementExp = expression.replace(newExpression);
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
javaCodeStyleManager.shortenClassReferences(replacementExp);
final CodeStyleManager styleManager = CodeStyleManager.getInstance(project);
return styleManager.reformat(replacementExp);
}
Aggregations