add postfix template for Objects.requireNonNull

This commit is contained in:
Dmitry Batkovich
2017-10-27 16:22:52 +03:00
parent 6e2c225c5e
commit 4c1ca2353e
11 changed files with 116 additions and 34 deletions

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2015 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.template.postfix.templates;
import com.intellij.codeInsight.completion.CompletionInitializationContext;
@@ -64,7 +50,8 @@ public class JavaPostfixTemplateProvider implements PostfixTemplateProvider {
new WhileStatementPostfixTemplate(),
new StreamPostfixTemplate(),
new OptionalPostfixTemplate(),
new LambdaPostfixTemplate());
new LambdaPostfixTemplate(),
new ObjectsRequireNonNullPostfixTemplate());
}
@NotNull

View File

@@ -0,0 +1,24 @@
// 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.template.postfix.templates;
import com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.IS_NOT_PRIMITIVE;
import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.selectorTopmost;
public class ObjectsRequireNonNullPostfixTemplate extends StringBasedPostfixTemplate {
public ObjectsRequireNonNullPostfixTemplate() {
super("reqnonnull", "Objects.requireNonNull(expr)", JavaPostfixTemplatesUtils.minimalLanguageLevelSelector(selectorTopmost(IS_NOT_PRIMITIVE),
LanguageLevel.JDK_1_7));
}
@Nullable
@Override
public String getTemplateString(@NotNull PsiElement element) {
return "java.util.Objects.requireNonNull($expr$)";
}
}

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2016 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.template.postfix.util;
import com.intellij.codeInsight.CodeInsightServicesUtil;
@@ -23,6 +9,7 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Conditions;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiExpressionTrimRenderer;
@@ -45,16 +32,21 @@ public abstract class JavaPostfixTemplatesUtils {
}
public static PostfixTemplateExpressionSelector atLeastJava8Selector(final PostfixTemplateExpressionSelector selector) {
return minimalLanguageLevelSelector(selector, LanguageLevel.JDK_1_8);
}
public static PostfixTemplateExpressionSelector minimalLanguageLevelSelector(@NotNull PostfixTemplateExpressionSelector selector,
@NotNull LanguageLevel minimalLevel) {
return new PostfixTemplateExpressionSelector() {
@Override
public boolean hasExpression(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
return PsiUtil.isLanguageLevel8OrHigher(context) && selector.hasExpression(context, copyDocument, newOffset);
return PsiUtil.getLanguageLevel(context).isAtLeast(minimalLevel) && selector.hasExpression(context, copyDocument, newOffset);
}
@NotNull
@Override
public List<PsiElement> getExpressions(@NotNull PsiElement context, @NotNull Document document, int offset) {
return PsiUtil.isLanguageLevel8OrHigher(context)
return PsiUtil.getLanguageLevel(context).isAtLeast(minimalLevel)
? selector.getExpressions(context, document, offset)
: Collections.emptyList();
}
@@ -66,7 +58,7 @@ public abstract class JavaPostfixTemplatesUtils {
}
};
}
public static PostfixTemplateExpressionSelector selectorTopmost() {
return selectorTopmost(Conditions.alwaysTrue());
}

View File

@@ -0,0 +1,5 @@
class Foo {
void m(int i) {
i.reqnonnull<caret>
}
}

View File

@@ -0,0 +1,5 @@
class Foo {
void m(int i) {
i.reqnonnull
}
}

View File

@@ -0,0 +1,5 @@
class Foo {
void m(String s) {
s.reqnonnull<caret>
}
}

View File

@@ -0,0 +1,5 @@
class Foo {
void m(String s) {
java.util.Objects.requireNonNull(s)
}
}

View File

@@ -0,0 +1,44 @@
// 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.java.codeInsight.template.postfix.templates;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.pom.java.LanguageLevel;
import org.jetbrains.annotations.NotNull;
public class ObjectsRequireNonNullPostfixTemplateTest extends PostfixTemplateTestCase {
private LanguageLevel myDefaultLanguageLevel;
@Override
protected void setUp() throws Exception {
super.setUp();
LanguageLevelProjectExtension levelProjectExtension = LanguageLevelProjectExtension.getInstance(myFixture.getProject());
myDefaultLanguageLevel = levelProjectExtension.getLanguageLevel();
levelProjectExtension.setLanguageLevel(LanguageLevel.JDK_1_7);
}
@Override
protected void tearDown() throws Exception {
try {
LanguageLevelProjectExtension.getInstance(myFixture.getProject()).setLanguageLevel(myDefaultLanguageLevel);
myDefaultLanguageLevel = null;
}
finally {
//noinspection ThrowFromFinallyBlock
super.tearDown();
}
}
public void testSimple() {
doTest();
}
public void testPrimitive() {
doTest();
}
@NotNull
@Override
protected String getSuffix() {
return "reqnonnull";
}
}

View File

@@ -0,0 +1,5 @@
public class Foo {
void m(Object o) {
<spot>Objects.requireNonNull(o)</spot>$key
}
}

View File

@@ -0,0 +1,5 @@
public class Foo {
void m(Object o) {
<spot>o</spot>$key
}
}

View File

@@ -0,0 +1,5 @@
<html>
<body>
Wraps object with Objects.requireNonNull.
</body>
</html>