/*
* Copyright 2015 The Error Prone Authors.
*
* 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.
*/
package com.google.errorprone.bugpatterns.threadsafety;
import static org.junit.Assume.assumeTrue;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.errorprone.util.RuntimeVersion;
import java.util.Arrays;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** {@link ImmutableChecker}Test */
@RunWith(JUnit4.class)
public class ImmutableCheckerTest {
private final CompilationTestHelper compilationHelper =
CompilationTestHelper.newInstance(ImmutableChecker.class, getClass());
@Test
public void basicFields() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"import com.google.common.collect.ImmutableList;",
"@Immutable class Test {",
" final int a = 42;",
" final String b = null;",
" final java.lang.String c = null;",
" final com.google.common.collect.ImmutableList<String> d = null;",
" final ImmutableList<Integer> e = null;",
" final Deprecated dep = null;",
" final Class<?> clazz = Class.class;",
"}")
.doTest();
}
@Test
public void interfacesMutableByDefault() {
compilationHelper
.addSourceLines("I.java", "interface I {}")
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable class Test {",
" // BUG: Diagnostic contains:"
+ " 'I' is not annotated with @com.google.errorprone.annotations.Immutable",
" private final I i = new I() {};",
"}")
.doTest();
}
@Test
public void annotationsAreImmutable() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable @interface Test {}")
.doTest();
}
@Test
public void customAnnotationsMightBeMutable() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable @interface Test {}")
.addSourceLines(
"MyTest.java",
"import com.google.errorprone.annotations.Immutable;",
"import java.lang.annotation.Annotation;",
"@Immutable final class MyTest implements Test {",
" // BUG: Diagnostic contains: non-final",
" public Object[] xs = {};",
" public Class<? extends Annotation> annotationType() {",
" return null;",
" }",
"}")
.doTest();
}
@Ignore("b/25630189") // don't check annotations for immutability yet
@Test
public void customImplementionsOfImplicitlyImmutableAnnotationsMustBeImmutable() {
compilationHelper
.addSourceLines("Anno.java", "@interface Anno {}")
.addSourceLines(
"MyAnno.java",
"import java.lang.annotation.Annotation;",
"final class MyAnno implements Anno {",
" // BUG: Diagnostic contains:",
" public Object[] xs = {};",
" public Class<? extends Annotation> annotationType() {",
" return null;",
" }",
"}")
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable class Test {",
" private final Anno anno = new MyAnno();",
"}")
.doTest();
}
@Test
public void customAnnotationsSubtype() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable @interface Test {}")
.addSourceLines(
"MyTest.java",
"import java.lang.annotation.Annotation;",
"final class MyTest implements Test {",
" // BUG: Diagnostic contains: non-final field 'xs'",
" public Object[] xs = {};",
" public Class<? extends Annotation> annotationType() {",
" return null;",
" }",
"}")
.doTest();
}
@Test
public void annotationsDefaultToImmutable() {
compilationHelper
.addSourceLines(
"Test.java",
"import javax.lang.model.element.ElementKind;",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable class Test {",
" private final Override override = null;",
"}")
.doTest();
}
@Test
public void enumsDefaultToImmutable() {
compilationHelper
.addSourceLines(
"Test.java",
"import javax.lang.model.element.ElementKind;",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable class Test {",
" private final ElementKind ek = null;",
"}")
.doTest();
}
@Test
public void enumsMayBeImmutable() {
compilationHelper
.addSourceLines(
"Kind.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable enum Kind { A, B, C; }")
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable class Test {",
" private final Kind k = null;",
"}")
.doTest();
}
@Test
public void mutableArray() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable class Test {",
" // BUG: Diagnostic contains:",
" final int[] xs = {42};",
"}")
.doTest();
}
@Test
public void annotatedImmutableInterfaces() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable interface Test {}")
.doTest();
}
@Test
public void immutableInterfaceField() {
compilationHelper
.addSourceLines(
"MyInterface.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable interface MyInterface {}")
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable class Test {",
" final MyInterface i = null;",
"}")
.doTest();
}
@Test
public void deeplyImmutableArguments() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Immutable;",
"import com.google.common.collect.ImmutableList;",
"@Immutable class Test {",
" final ImmutableList<ImmutableList<ImmutableList<String>>> l = null;",
"}")
.doTest();
}
@Test
public void mutableNonFinalField() {
compilationHelper
.addSour
没有合适的资源?快使用搜索试试~ 我知道了~
Error Prone是一个Java静态分析工具
共2000个文件
java:1577个
md:408个
xml:14个
需积分: 5 0 下载量 128 浏览量
2023-08-18
15:47:39
上传
评论
收藏 4.86MB ZIP 举报
温馨提示
Error Prone是一个Java静态分析工具,用于捕获常见编程 编译时的错误。
资源推荐
资源详情
资源评论
收起资源包目录
Error Prone是一个Java静态分析工具 (2000个子文件)
ImmutableCheckerTest.java 103KB
StatementSwitchToExpressionSwitchTest.java 94KB
SuggestedFixesTest.java 72KB
ReturnMissingNullableTest.java 69KB
GuardedByCheckerTest.java 62KB
BuiltInCheckerSuppliers.java 61KB
UnusedVariableTest.java 47KB
InlinerTest.java 41KB
NullnessPropagationTest.java 37KB
UTemplater.java 36KB
SuggesterTest.java 35KB
ThreadSafeCheckerTest.java 34KB
TemplatingTest.java 33KB
CheckReturnValueTest.java 32KB
NullnessInferenceTest.java 30KB
PreferredInterfaceTypeTest.java 28KB
PlaceholderUnificationVisitor.java 27KB
AbstractReturnValueIgnored.java 27KB
ReturnValueIgnoredTest.java 27KB
ScannerSupplierTest.java 26KB
CanIgnoreReturnValueSuggesterTest.java 26KB
UnnecessaryDefaultInEnumSwitchTest.java 25KB
CompileTimeConstantCheckerTest.java 25KB
ValidatorTest.java 24KB
ErrorProneCompilerIntegrationTest.java 24KB
ParameterNameTest.java 23KB
DoNotCallCheckerTest.java 23KB
UnsafeWildcardTest.java 23KB
TimeUnitMismatch.java 22KB
DefaultCharset.java 22KB
CheckReturnValueWellKnownLibrariesTest.java 21KB
Template.java 21KB
MissingFail.java 20KB
FromTemporalAccessor.java 20KB
MatchersTest.java 20KB
DoNotUseRuleChainTest.java 19KB
ImpossibleNullComparisonTest.java 19KB
DefaultCharsetTest.java 19KB
ApiDiffCheckerTest.java 19KB
TruthIncompatibleTypeTest.java 19KB
ExpectedExceptionCheckerTest.java 19KB
ErrorProneJavaCompilerTest.java 18KB
JUnit4TestNotRunTest.java 18KB
MixedMutabilityReturnTypeTest.java 18KB
AnnotationPositionTest.java 17KB
ClassNewInstanceTest.java 17KB
WakelockReleasedDangerouslyTest.java 17KB
FieldMissingNullableTest.java 17KB
PreferJavaTimeOverload.java 17KB
NarrowingCompoundAssignmentTest.java 17KB
JdkObsolete.java 16KB
GuardedByBinderTest.java 16KB
UnificationTest.java 16KB
CompilationTestHelperTest.java 16KB
ImmutableSetForContainsTest.java 16KB
InterruptedExceptionSwallowedTest.java 16KB
OptionalOfRedundantMethodTest.java 16KB
WildcardImportTest.java 16KB
ImportStatementsTest.java 16KB
ConstantPatternCompileTest.java 15KB
DurationToLongTimeUnitTest.java 15KB
MissingSuperCallTest.java 15KB
ConstantPatternCompile.java 15KB
UndefinedEqualsTest.java 15KB
PreferJavaTimeOverloadTest.java 15KB
UnusedMethodTest.java 15KB
ErrorProneJavacPluginTest.java 15KB
StringSplitterTest.java 15KB
DiagnosticTestHelper.java 15KB
BugCheckerRefactoringTestHelper.java 15KB
CanIgnoreReturnValueSuggester.java 15KB
FieldCanBeLocalTest.java 15KB
ReferenceEqualityTest.java 14KB
CompilationTestHelper.java 14KB
AlreadyCheckedTest.java 14KB
FormatStringAnnotationCheckerTest.java 14KB
AnnotationMatcherTest.java 14KB
DoNotMockCheckerTest.java 14KB
ProtoDurationGetSecondsGetNanoTest.java 13KB
DateCheckerTest.java 13KB
MethodCanBeStaticTest.java 13KB
FloggerRequiredModifiersTest.java 13KB
ParameterMissingNullableTest.java 13KB
NoCanIgnoreReturnValueOnClassesTest.java 13KB
AbstractMockChecker.java 13KB
ArgumentSelectionDefectCheckerTest.java 13KB
InvalidJavaTimeConstant.java 13KB
MemberNameTest.java 13KB
StringSplitter.java 13KB
UseCorrectAssertInTestsTest.java 13KB
FunctionalInterfaceClashTest.java 13KB
StrictFormatStringValidation.java 13KB
DurationTemporalUnitTest.java 12KB
BadImportTest.java 12KB
TryFailThrowable.java 12KB
FormatStringTest.java 12KB
ExpressionTemplate.java 12KB
ModifiedButNotUsedTest.java 12KB
ParameterTest.java 12KB
NullnessPropagationTransferCases2.java 12KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
hello_中年人
- 粉丝: 7
- 资源: 324
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功