Commit b5e67a74 authored by Roman Shevchenko's avatar Roman Shevchenko
Browse files

[java] extracts utility method for producing module statements

parent 6637903b
Showing with 40 additions and 123 deletions
+40 -123
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
......@@ -77,9 +63,7 @@ public class AddRequiredModuleFix extends LocalQuickFixAndIntentionActionOnPsiEl
return false;
}
PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(module.getProject()).getParserFacade();
PsiJavaModule tempModule = parserFacade.createModuleFromText("module " + module.getName() + " { requires " + requiredName + "; }");
Iterable<PsiRequiresStatement> tempModuleRequires = tempModule.getRequires();
PsiRequiresStatement requiresStatement = tempModuleRequires.iterator().next();
PsiStatement requiresStatement = parserFacade.createModuleStatementFromText(PsiKeyword.REQUIRES + ' ' + requiredName);
PsiElement addingPlace = findAddingPlace(module);
if (addingPlace != null) {
......
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
......@@ -33,7 +19,7 @@ import java.util.stream.Collectors;
/**
* @author Pavel.Dolgov
*/
public abstract class MergeModuleStatementsFix<T extends PsiElement> extends LocalQuickFixAndIntentionActionOnPsiElement {
public abstract class MergeModuleStatementsFix<T extends PsiStatement> extends LocalQuickFixAndIntentionActionOnPsiElement {
protected MergeModuleStatementsFix(@NotNull PsiJavaModule javaModule) {
super(javaModule);
......@@ -58,12 +44,8 @@ public abstract class MergeModuleStatementsFix<T extends PsiElement> extends Loc
final List<T> statementsToMerge = getStatementsToMerge(javaModule);
LOG.assertTrue(!statementsToMerge.isEmpty());
final String tempModuleText = PsiKeyword.MODULE + " " + javaModule.getName() + " {" + getReplacementText(statementsToMerge) + "}";
final PsiJavaModule tempModule = JavaPsiFacade.getInstance(project).getElementFactory().createModuleFromText(tempModuleText);
final List<T> tempStatements = getStatementsToMerge(tempModule);
LOG.assertTrue(!tempStatements.isEmpty());
final T replacement = tempStatements.get(0);
final String text = getReplacementText(statementsToMerge);
final PsiStatement replacement = JavaPsiFacade.getInstance(project).getElementFactory().createModuleStatementFromText(text);
final T firstStatement = statementsToMerge.get(0);
final CommentTracker commentTracker = new CommentTracker();
......
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
......@@ -66,9 +52,11 @@ public class MergePackageAccessibilityStatementsFix
protected String getReplacementText(@NotNull List<PsiPackageAccessibilityStatement> statementsToMerge) {
final List<String> moduleNames = getModuleNames(statementsToMerge);
if (!moduleNames.isEmpty()) {
return getKeyword() + " " + myPackageName + " " + PsiKeyword.TO + " " + joinUniqueNames(moduleNames) + ";";
return getKeyword() + ' ' + myPackageName + ' ' + PsiKeyword.TO + ' ' + joinUniqueNames(moduleNames);
}
else {
return getKeyword() + ' ' + myPackageName;
}
return getKeyword() + " " + myPackageName + ";";
}
@NotNull
......
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
......@@ -54,7 +40,7 @@ public class MergeProvidesStatementsFix extends MergeModuleStatementsFix<PsiProv
protected String getReplacementText(@NotNull List<PsiProvidesStatement> statementsToMerge) {
final List<String> implementationNames = getImplementationNames(statementsToMerge);
LOG.assertTrue(!implementationNames.isEmpty());
return PsiKeyword.PROVIDES + " " + myInterfaceName + " " + PsiKeyword.WITH + " " + joinUniqueNames(implementationNames) + ";";
return PsiKeyword.PROVIDES + ' ' + myInterfaceName + ' ' + PsiKeyword.WITH + ' ' + joinUniqueNames(implementationNames);
}
@NotNull
......
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInspection.java19modules;
import com.intellij.analysis.AnalysisScope;
......@@ -203,10 +189,7 @@ public class Java9RedundantRequiresStatementInspection extends GlobalJavaBatchIn
if (!reexportedDependencies.isEmpty()) {
PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(currentModule.getProject()).getParserFacade();
for (String dependencyName : reexportedDependencies) {
PsiJavaModule tempModule =
parserFacade.createModuleFromText("module " + currentModule.getName() + " { requires " + dependencyName + "; }");
Iterable<PsiRequiresStatement> tempModuleRequires = tempModule.getRequires();
PsiRequiresStatement requiresStatement = tempModuleRequires.iterator().next();
PsiStatement requiresStatement = parserFacade.createModuleStatementFromText(PsiKeyword.REQUIRES + ' ' + dependencyName);
currentModule.addAfter(requiresStatement, addingPlace);
}
}
......
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi;
import com.intellij.pom.java.LanguageLevel;
......@@ -247,6 +233,12 @@ public interface PsiJavaParserFacade {
@NotNull
PsiJavaModule createModuleFromText(@NotNull String text);
/**
* Creates a Java module statement from the specified text.
*/
@NotNull
PsiStatement createModuleStatementFromText(@NotNull String text);
/** @deprecated use {@link PsiType#annotate(TypeAnnotationProvider)} (to be removed in IDEA 18) */
PsiType createPrimitiveType(@NotNull String text, @NotNull PsiAnnotation[] annotations) throws IncorrectOperationException;
}
\ No newline at end of file
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.impl;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.lang.PsiBuilder;
import com.intellij.lang.java.parser.*;
import com.intellij.lang.java.parser.DeclarationParser;
import com.intellij.lang.java.parser.JavaParser;
import com.intellij.lang.java.parser.JavaParserUtil;
import com.intellij.lang.java.parser.ReferenceParser;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.util.text.StringUtil;
......@@ -31,6 +20,7 @@ import com.intellij.psi.impl.source.tree.FileElement;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.HashMap;
......@@ -404,6 +394,18 @@ public class PsiJavaParserFacadeImpl implements PsiJavaParserFacade {
return (PsiJavaModule)element;
}
@NotNull
@Override
public PsiStatement createModuleStatementFromText(@NotNull String text) {
String template = "module M { " + text + "; }";
DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(template, MODULE, LanguageLevel.JDK_1_9), null);
PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode());
if (!(element instanceof PsiJavaModule)) throw new IncorrectOperationException("Incorrect module statement '" + text + "'");
PsiStatement statement = PsiTreeUtil.getChildOfType(element, PsiStatement.class);
if (statement == null) throw new IncorrectOperationException("Incorrect module statement '" + text + "'");
return statement;
}
@NotNull
@Override
public PsiType createPrimitiveType(@NotNull String text, @NotNull PsiAnnotation[] annotations) throws IncorrectOperationException {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment