diff --git a/src/refval/RefValAnnotatedTypeFactory.java b/src/refval/RefValAnnotatedTypeFactory.java index af0fc2426..578ba96a9 100644 --- a/src/refval/RefValAnnotatedTypeFactory.java +++ b/src/refval/RefValAnnotatedTypeFactory.java @@ -56,7 +56,7 @@ public class RefValAnnotatedTypeFactory extends BaseAnnotatedTypeFactory { * For each Java type is present in the target program, typeNamesMap maps * String of the type to the TypeMirror. */ - private final Map typeNamesMap = new HashMap(); + private final Map typeNamesMap = new HashMap<>(); public RefValAnnotatedTypeFactory(BaseTypeChecker checker) { super(checker); @@ -124,18 +124,18 @@ public RefValQualifierHierarchy(MultiGraphFactory f, AnnotationMirror bottom) { */ private boolean isSubtypeWithRoots(AnnotationMirror rhs, AnnotationMirror lhs) { - Set rTypeNamesSet = new HashSet(Arrays.asList(RefValUtils + Set rTypeNamesSet = new HashSet<>(Arrays.asList(RefValUtils .getTypeNames(rhs))); - Set lTypeNamesSet = new HashSet(Arrays.asList(RefValUtils + Set lTypeNamesSet = new HashSet<>(Arrays.asList(RefValUtils .getTypeNames(lhs))); - Set rRootsSet = new HashSet(Arrays.asList(RefValUtils + Set rRootsSet = new HashSet<>(Arrays.asList(RefValUtils .getTypeNameRoots(rhs))); - Set lRootsSet = new HashSet(Arrays.asList(RefValUtils + Set lRootsSet = new HashSet<>(Arrays.asList(RefValUtils .getTypeNameRoots(lhs))); - Set combinedTypeNames = new HashSet(); + Set combinedTypeNames = new HashSet<>(); combinedTypeNames.addAll(rTypeNamesSet); combinedTypeNames.addAll(lTypeNamesSet); - Set combinedRoots = new HashSet(); + Set combinedRoots = new HashSet<>(); combinedRoots.addAll(rRootsSet); combinedRoots.addAll(lRootsSet); @@ -144,11 +144,7 @@ private boolean isSubtypeWithRoots(AnnotationMirror rhs, AnnotationMirror lhs) { AnnotationMirror refinedCombinedAnno = refineRefVal(combinedAnno); AnnotationMirror refinedLhs = refineRefVal(lhs); - if (AnnotationUtils.areSame(refinedCombinedAnno, refinedLhs)) { - return true; - } else { - return false; - } + return AnnotationUtils.areSame(refinedCombinedAnno, refinedLhs); } /** @@ -162,15 +158,11 @@ private boolean isSubtypeWithRoots(AnnotationMirror rhs, AnnotationMirror lhs) { * @return true is rhs is subtype of lhs, otherwise return false. */ private boolean isSubtypeWithoutRoots(AnnotationMirror rhs, AnnotationMirror lhs) { - Set rTypeNamesSet = new HashSet(Arrays.asList(RefValUtils + Set rTypeNamesSet = new HashSet<>(Arrays.asList(RefValUtils .getTypeNames(rhs))); - Set lTypeNamesSet = new HashSet(Arrays.asList(RefValUtils + Set lTypeNamesSet = new HashSet<>(Arrays.asList(RefValUtils .getTypeNames(lhs))); - if (lTypeNamesSet.containsAll(rTypeNamesSet)) { - return true; - } else { - return false; - } + return lTypeNamesSet.containsAll(rTypeNamesSet); } @Override @@ -180,7 +172,8 @@ public boolean isSubtype(AnnotationMirror rhs, AnnotationMirror lhs) { return isSubtypeWithRoots(rhs, lhs); // return isSubtypeWithoutRoots(rhs, lhs); } else { - // if (rhs != null && lhs != null) + // Remove typeNames and typeNameRoots from @RefVal + // to make it in the subtype hierarchy if (AnnotationUtils.areSameByName(rhs, REFVAL)) { rhs = REFVAL; } else if (AnnotationUtils.areSameByName(lhs, REFVAL)) { @@ -219,9 +212,8 @@ public Void visitNewClass(NewClassTree node, AnnotatedTypeMirror type) { @Override public Void visitLiteral(LiteralTree node, AnnotatedTypeMirror type) { if (!node.getKind().equals(Kind.NULL_LITERAL)) { - AnnotatedTypeMirror annoType = type; - AnnotationMirror refValType = RefValUtils.generateRefValAnnoFromLiteral(annoType, - processingEnv); + AnnotationMirror refValType = RefValUtils.generateRefValAnnoFromLiteral(type, + processingEnv); type.replaceAnnotation(refValType); } return super.visitLiteral(node, type); @@ -253,33 +245,33 @@ public Void visitMethodInvocation(MethodInvocationTree node, AnnotatedTypeMirror */ public AnnotationMirror refineRefVal(AnnotationMirror type) { String[] typeNameRoots = RefValUtils.getTypeNameRoots(type); - Set refinedRoots = new HashSet(); + Set refinedRoots = new HashSet<>(); - if (typeNameRoots.length == 0) { - - } else if (typeNameRoots.length == 1) { - refinedRoots.add(typeNameRoots[0]); - } else { - List rootsList = new ArrayList(Arrays.asList(typeNameRoots)); - while (rootsList.size() != 0) { - TypeMirror decType = getTypeMirror(rootsList.get(0)); - if (!isComparable(decType, rootsList)) { - refinedRoots.add(rootsList.get(0)); - rootsList.remove(0); + if (typeNameRoots.length != 0) { + if (typeNameRoots.length == 1) { + refinedRoots.add(typeNameRoots[0]); + } else { + List rootsList = new ArrayList<>(Arrays.asList(typeNameRoots)); + while (rootsList.size() != 0) { + TypeMirror decType = getTypeMirror(rootsList.get(0)); + if (!isComparable(decType, rootsList)) { + refinedRoots.add(rootsList.get(0)); + rootsList.remove(0); + } } } } String[] typeNames = RefValUtils.getTypeNames(type); Arrays.sort(typeNames); - Set refinedTypeNames = new HashSet(); + Set refinedTypeNames = new HashSet<>(); if (refinedRoots.size() == 0) { - refinedTypeNames = new HashSet(Arrays.asList(typeNames)); + refinedTypeNames = new HashSet<>(Arrays.asList(typeNames)); return RefValUtils.createRefValAnnotation(refinedTypeNames, processingEnv); } else { for (String typeName : typeNames) { - if (typeName == "") { + if (typeName.equals("")) { continue; } TypeMirror decType = getTypeMirror(typeName); @@ -312,7 +304,7 @@ private void replaceArrayComponentATM(AnnotatedArrayType arrayAtm) { private boolean isComparable(TypeMirror decType, List rootsList) { for (int i = 1; i < rootsList.size(); i++) { - if (rootsList.get(i) == "") { + if (rootsList.get(i).equals("")) { continue; } TypeMirror comparedDecType = getTypeMirror(rootsList.get(i)); @@ -330,7 +322,7 @@ private boolean isComparable(TypeMirror decType, List rootsList) { private boolean shouldPresent(TypeMirror decType, Set refinedRoots) { for (String refinedRoot : refinedRoots) { - if (refinedRoot == "") { + if (refinedRoot.equals("")) { continue; } TypeMirror comparedDecType = getTypeMirror(refinedRoot); @@ -344,7 +336,7 @@ private boolean shouldPresent(TypeMirror decType, Set refinedRoots) { } private TypeMirror getTypeMirror(String typeName) { - if (this.typeNamesMap.keySet().contains(typeName)) { + if (this.typeNamesMap.containsKey(typeName)) { return this.typeNamesMap.get(typeName); } else { return elements.getTypeElement(convertToReferenceType(typeName)).asType(); diff --git a/src/refval/RefValChecker.java b/src/refval/RefValChecker.java index 54b7eb561..ffc8f8ace 100644 --- a/src/refval/RefValChecker.java +++ b/src/refval/RefValChecker.java @@ -1,18 +1,12 @@ package refval; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.util.Elements; - import org.checkerframework.common.basetype.BaseAnnotatedTypeFactory; -import org.checkerframework.javacutil.AnnotationBuilder; import checkers.inference.BaseInferrableChecker; import checkers.inference.InferenceChecker; import checkers.inference.InferrableChecker; import checkers.inference.SlotManager; import checkers.inference.model.ConstraintManager; -import refval.qual.RefVal; -import refval.qual.UnknownRefVal; /** * Checker for RefVal type system. @@ -21,19 +15,6 @@ * */ public class RefValChecker extends BaseInferrableChecker { - public AnnotationMirror REFVAL, UNKNOWNREFVAL; - - @Override - public void initChecker() { - super.initChecker(); - setAnnotations(); - } - - protected void setAnnotations() { - final Elements elements = processingEnv.getElementUtils(); - REFVAL = AnnotationBuilder.fromClass(elements, RefVal.class); - UNKNOWNREFVAL = AnnotationBuilder.fromClass(elements, UnknownRefVal.class); - } @Override public RefValVisitor createVisitor(InferenceChecker ichecker, BaseAnnotatedTypeFactory factory, @@ -50,9 +31,8 @@ public RefValAnnotatedTypeFactory createRealTypeFactory() { public RefValInferenceAnnotatedTypeFactory createInferenceATF(InferenceChecker inferenceChecker, InferrableChecker realChecker, BaseAnnotatedTypeFactory realTypeFactory, SlotManager slotManager, ConstraintManager constraintManager) { - RefValInferenceAnnotatedTypeFactory refValInferenceTypeFactory = new RefValInferenceAnnotatedTypeFactory( + return new RefValInferenceAnnotatedTypeFactory( inferenceChecker, realChecker.withCombineConstraints(), realTypeFactory, realChecker, slotManager, constraintManager); - return refValInferenceTypeFactory; } -} \ No newline at end of file +} diff --git a/src/refval/RefValInferenceAnnotatedTypeFactory.java b/src/refval/RefValInferenceAnnotatedTypeFactory.java index 98f9cd4ea..15e01af53 100644 --- a/src/refval/RefValInferenceAnnotatedTypeFactory.java +++ b/src/refval/RefValInferenceAnnotatedTypeFactory.java @@ -52,8 +52,8 @@ public AnnotatedDeclaredType getBoxedType(AnnotatedPrimitiveType type) { AnnotationMirror am = RefValUtils.createRefValAnnotation(typeElt.asType().toString(), this.processingEnv); AnnotatedDeclaredType dt = fromElement(typeElt); - ConstantSlot cs = InferenceMain.getInstance().getSlotManager().createConstantSlot(am); - dt.addAnnotation(InferenceMain.getInstance().getSlotManager().getAnnotation(cs)); + ConstantSlot cs = slotManager.createConstantSlot(am); + dt.addAnnotation(slotManager.getAnnotation(cs)); dt.addAnnotation(cs.getValue()); return dt; } @@ -66,8 +66,8 @@ public AnnotatedPrimitiveType getUnboxedType(AnnotatedDeclaredType type) this.processingEnv); AnnotatedPrimitiveType pt = (AnnotatedPrimitiveType) AnnotatedTypeMirror.createType( primitiveType, this, false); - ConstantSlot cs = InferenceMain.getInstance().getSlotManager().createConstantSlot(am); - pt.addAnnotation(InferenceMain.getInstance().getSlotManager().getAnnotation(cs)); + ConstantSlot cs = slotManager.createConstantSlot(am); + pt.addAnnotation(slotManager.getAnnotation(cs)); pt.addAnnotation(cs.getValue()); return pt; } diff --git a/src/refval/RefValInferenceTreeAnnotator.java b/src/refval/RefValInferenceTreeAnnotator.java index f6fb7eedc..3aa8c4a6a 100644 --- a/src/refval/RefValInferenceTreeAnnotator.java +++ b/src/refval/RefValInferenceTreeAnnotator.java @@ -127,7 +127,6 @@ public Void visitMethodInvocation(MethodInvocationTree methodInvocationTree, private void replaceATM(AnnotatedTypeMirror atm, AnnotationMirror refValAM) { final ConstantSlot cs = slotManager.createConstantSlot(refValAM); - slotManager.createConstantSlot(refValAM); AnnotationBuilder ab = new AnnotationBuilder(realTypeFactory.getProcessingEnv(), VarAnnot.class); ab.setValue("value", cs.getId()); AnnotationMirror varAnno = ab.build(); @@ -137,7 +136,7 @@ private void replaceATM(AnnotatedTypeMirror atm, AnnotationMirror refValAM) { /** * Add the bytecode default RefVal annotation for component type of the given {@link AnnotatedArrayType}. * - *

For multi-dimensional array, this method will recursively add bytecode default RefVal annotation to array's component type. + *

For multi-dimensional array, this method will recursively add bytecode default RefVal annotation to array's component type. * * @param arrayAtm the given {@link AnnotatedArrayType}, whose component type will be added the bytecode default. */ @@ -148,7 +147,7 @@ private void replaceArrayComponentATM(AnnotatedArrayType arrayAtm) { replaceATM(componentAtm, componentAnno); if (componentAtm.getKind() == TypeKind.ARRAY) { - //if component is also an array type, then recursively annotate its component also. + // if component is also an array type, then recursively annotate its component also. replaceArrayComponentATM((AnnotatedArrayType) componentAtm); } } diff --git a/src/refval/solvers/classic/RefValSolver.java b/src/refval/solvers/classic/RefValSolver.java index bf599964c..5786145bc 100644 --- a/src/refval/solvers/classic/RefValSolver.java +++ b/src/refval/solvers/classic/RefValSolver.java @@ -71,7 +71,7 @@ public InferenceResult solve(Map configuration, List solutions = new ArrayList<>(); try { if (refValSolvers.size() > 0) { - solutions = solveInparallel(refValSolvers); + solutions = solveInParallel(refValSolvers); } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); @@ -80,7 +80,7 @@ public InferenceResult solve(Map configuration, return getMergedResultFromSolutions(processingEnvironment, solutions); } - private List solveInparallel(List refValSolvers) + private List solveInParallel(List refValSolvers) throws InterruptedException, ExecutionException { ExecutorService service = Executors.newFixedThreadPool(refValSolvers.size()); diff --git a/src/refval/util/RefValUtils.java b/src/refval/util/RefValUtils.java index 359f5b7f6..dc6772d07 100644 --- a/src/refval/util/RefValUtils.java +++ b/src/refval/util/RefValUtils.java @@ -36,13 +36,8 @@ public static String[] getTypeNameRoots(AnnotationMirror type) { private static String[] getReferenceValue(AnnotationMirror type, String valueName) { List allTypesList = AnnotationUtils.getElementValueArray(type, valueName, String.class, true); - // types in this list is org.checkerframework.framework.util.AnnotationBuilder. String[] allTypesInArray = new String[allTypesList.size()]; - int i = 0; - for (Object o : allTypesList) { - allTypesInArray[i] = o.toString(); - i++; - } + allTypesList.toArray(allTypesInArray); return allTypesInArray; } @@ -56,11 +51,7 @@ public static AnnotationMirror createRefValAnnotationForByte(String[] refValType private static AnnotationMirror createRefValAnnotation(final Set refValTypes, final AnnotationBuilder builder) { String[] refValTypesInArray = new String[refValTypes.size()]; - int i = 0; - for (String refValType : refValTypes) { - refValTypesInArray[i] = refValType.toString(); - i++; - } + refValTypes.toArray(refValTypesInArray); builder.setValue("typeNames", refValTypesInArray); return builder.build(); } @@ -68,11 +59,7 @@ private static AnnotationMirror createRefValAnnotation(final Set refValT private static AnnotationMirror createRefValAnnotationWithoutName(final Set roots, final AnnotationBuilder builder) { String[] refValTypesInArray = new String[roots.size()]; - int i = 0; - for (String refValType : roots) { - refValTypesInArray[i] = refValType.toString(); - i++; - } + roots.toArray(refValTypesInArray); builder.setValue("typeNameRoots", refValTypesInArray); return builder.build(); } @@ -80,7 +67,6 @@ private static AnnotationMirror createRefValAnnotationWithoutName(final Set refValTypes, ProcessingEnvironment processingEnv) { AnnotationBuilder builder = new AnnotationBuilder(processingEnv, RefVal.class); - return createRefValAnnotation(refValTypes, builder); } @@ -107,18 +93,11 @@ public static AnnotationMirror createRefValAnnotationWithRoots(Set refVa private static AnnotationMirror createRefValAnnotationWithRoots(final Set refValTypes, final Set refValTypesRoots, final AnnotationBuilder builder) { String[] refValTypesInArray = new String[refValTypes.size()]; - int i = 0; - for (String refValType : refValTypes) { - refValTypesInArray[i] = refValType.toString(); - i++; - } + refValTypes.toArray(refValTypesInArray); String[] refValTypesRootInArray = new String[refValTypesRoots.size()]; - int j = 0; - for (String refValTypesRoot : refValTypesRoots) { - refValTypesRootInArray[j] = refValTypesRoot.toString(); - j++; - } + refValTypesRoots.toArray(refValTypesRootInArray); + if (refValTypesRootInArray.length > 0) { builder.setValue("typeNameRoots", refValTypesRootInArray); } @@ -133,29 +112,25 @@ public static AnnotationMirror generateRefValAnnoFromNewClass(AnnotatedTypeMirro ProcessingEnvironment processingEnv) { TypeMirror tm = type.getUnderlyingType(); String className = tm.toString(); - AnnotationMirror refValType = createRefValAnnotation(convert(className), processingEnv); - return refValType; + return createRefValAnnotation(convert(className), processingEnv); } public static AnnotationMirror generateRefValAnnoFromByteCode(AnnotatedTypeMirror type, ProcessingEnvironment processingEnv) { TypeMirror tm = type.getUnderlyingType(); String className = tm.toString(); - AnnotationMirror refValType = createRefValAnnotationForByte(convert(className), - processingEnv); - return refValType; + return createRefValAnnotationForByte(convert(className), processingEnv); } public static AnnotationMirror generateRefValAnnoFromLiteral(AnnotatedTypeMirror type, ProcessingEnvironment processingEnv) { - String refValTypeInArray[] = convert(type.getUnderlyingType().toString()); - AnnotationMirror refValType = createRefValAnnotation(refValTypeInArray, processingEnv); - return refValType; + String[] refValTypeInArray = convert(type.getUnderlyingType().toString()); + return createRefValAnnotation(refValTypeInArray, processingEnv); } public static AnnotationMirror generateRefValAnnoFromLiteral(LiteralTree node, ProcessingEnvironment processingEnv) { - String refValTypeInArray[] = { "" }; + String[] refValTypeInArray = { "" }; switch (node.getKind()) { case STRING_LITERAL: refValTypeInArray = convert(String.class.toString().split(" ")[1]); @@ -184,8 +159,7 @@ public static AnnotationMirror generateRefValAnnoFromLiteral(LiteralTree node, default: throw new BugInCF("Unknown literal tree: " + node.getKind().toString()); } - AnnotationMirror refValType = createRefValAnnotation(refValTypeInArray, processingEnv); - return refValType; + return createRefValAnnotation(refValTypeInArray, processingEnv); } public static String[] convert(String... typeName) { @@ -194,9 +168,8 @@ public static String[] convert(String... typeName) { public static AnnotationMirror createRefValAnnotation(String typeName, ProcessingEnvironment processingEnv) { - Set typeNames = new HashSet(); + Set typeNames = new HashSet<>(); typeNames.add(typeName); - AnnotationMirror am = RefValUtils.createRefValAnnotation(typeNames, processingEnv); - return am; + return RefValUtils.createRefValAnnotation(typeNames, processingEnv); } }