testing contract annotations

This commit is contained in:
Ilya Klyuchnikov
2014-07-10 10:35:32 +02:00
committed by peter
parent a8717a4746
commit 44ea3071e9
5 changed files with 61 additions and 5 deletions
@@ -35,7 +35,6 @@ public class ClassProcessor extends VirtualFileVisitor {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.bytecodeAnalysis.ClassProcessor");
final static ELattice<Value> valueLattice = new ELattice<Value>(Value.Bot, Value.Top);
final Solver<Key, Value> solver = new Solver<Key, Value>(valueLattice);
final IntIdSolver myIntIdSolver;
@NotNull
@@ -163,7 +162,6 @@ public class ClassProcessor extends VirtualFileVisitor {
void addEquation(Equation<Key, Value> equation) {
try {
//solver.addEquation(equation);
myIntIdSolver.addEquation(enumerate(equation));
}
catch (IOException e) {
@@ -48,7 +48,7 @@ public class Util {
if (value == Value.Top || value == Value.Bot) {
continue;
}
InternalKey key = null;
InternalKey key;
try {
String s = enumerators.internalKeyEnumerator.valueOf(inKey);
key = readInternalKey(s);
@@ -68,6 +68,7 @@ public class Util {
else if (direction instanceof Out && value == Value.NotNull) {
annotations.add(baseAnnKey, new AnnotationData("org.jetbrains.annotations.NotNull", ""));
}
// TODO - sort (normalize) contract clauses
else if (direction instanceof InOut) {
StringBuilder sb = contracts.get(baseAnnKey);
if (sb == null) {
@@ -83,7 +84,9 @@ public class Util {
}
for (Map.Entry<String, StringBuilder> contract : contracts.entrySet()) {
annotations.add(contract.getKey(), new AnnotationData("org.jetbrains.annotations.Contract", contract.getValue().append('"').toString()));
if (!annotations.containsKey(contract.getKey())) {
annotations.add(contract.getKey(), new AnnotationData("org.jetbrains.annotations.Contract", contract.getValue().append('"').toString()));
}
}
return annotations;
}
@@ -26,6 +26,7 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.Contract;
import java.io.File;
import java.io.IOException;
@@ -35,7 +36,7 @@ import java.lang.annotation.Annotation;
* @author lambdamix
*/
public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase {
public static final String ORG_JETBRAINS_ANNOTATIONS_CONTRACT = Contract.class.getName();
private final String myClassesProjectRelativePath = "/classes/" + Test01.class.getPackage().getName().replace('.', '/');
private JavaPsiFacade myJavaPsiFacade;
private InferredAnnotationsManager myInferredAnnotationsManager;
@@ -95,6 +96,18 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase {
ExpectNotNull expectedAnnotation = javaMethod.getAnnotation(ExpectNotNull.class);
PsiAnnotation actualAnnotation = myInferredAnnotationsManager.findInferredAnnotation(psiMethod, AnnotationUtil.NOT_NULL);
assertEquals(expectedAnnotation == null, actualAnnotation == null);
// contracts
ExpectContract expectedContract = javaMethod.getAnnotation(ExpectContract.class);
PsiAnnotation actualContractAnnotation = myInferredAnnotationsManager.findInferredAnnotation(psiMethod, ORG_JETBRAINS_ANNOTATIONS_CONTRACT);
assertEquals(expectedContract == null, actualContractAnnotation == null);
if (expectedAnnotation != null) {
String expectedContractValue = expectedContract.value();
String actualContractValue = AnnotationUtil.getStringAttributeValue(actualContractAnnotation, null);
assertEquals(expectedContractValue, actualContractValue);
}
}
}
@@ -0,0 +1,30 @@
/*
* Copyright 2000-2014 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.
*/
package com.intellij.codeInspection.bytecodeAnalysis;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author lambdamix
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExpectContract {
String value() default "";
}
@@ -15,6 +15,7 @@
*/
package com.intellij.codeInspection.bytecodeAnalysis.data;
import com.intellij.codeInspection.bytecodeAnalysis.ExpectContract;
import com.intellij.codeInspection.bytecodeAnalysis.ExpectNotNull;
/**
@@ -43,4 +44,15 @@ public class Test01 {
static void v(Object o) {
}
@ExpectContract("null->null")
static String toString1(Object o) {
return o == null ? null : o.toString();
}
@ExpectContract("null->!null")
static String toString2(Object o) {
return o == null ? "null" : o.toString();
}
}