From 0037c6fa844eee6b3a8b8e73d3c78d0d3730beca Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Tue, 20 Feb 2024 16:27:21 -0800 Subject: [PATCH] Rename `isAccessible` to `reflectiveCall`; add overloads to reduce diffs --- src/docs/manual/dev.html | 6 +- src/docs/manual/index.html | 2 +- .../randoop/contract/CheckRepContract.java | 4 +- .../generation/HelperSequenceCreator.java | 9 +- .../java/randoop/main/GenInputsAbstract.java | 2 +- .../java/randoop/operation/MethodCall.java | 88 +++++++++++-------- .../randoop/operation/TypedOperation.java | 14 ++- .../randoop/reflection/OperationModel.java | 8 +- .../randoop/reflection/ReflectionManager.java | 3 +- .../java/randoop/test/ExceptionCheck.java | 2 +- .../randoop/test/ExpectedExceptionCheck.java | 47 +++++----- .../condition/OperationSpecificationTest.java | 7 +- .../SpecificationTranslatorTest.java | 8 +- .../randoop/operation/EnumReflectionTest.java | 27 +++--- .../operation/OperationParserTests.java | 2 +- .../randoop/operation/TypedOperationTest.java | 3 +- .../randoop/reflection/AccessibilityTest.java | 19 ++-- .../reflection/SignatureParserTest.java | 2 +- .../java/randoop/sequence/DevExampleCode.java | 8 +- .../randoop/types/CaptureConversionTest.java | 25 ++---- .../java/randoop/types/TypeVariableTest.java | 4 +- 21 files changed, 143 insertions(+), 147 deletions(-) diff --git a/src/docs/manual/dev.html b/src/docs/manual/dev.html index 3c0cb11180..87ea127ea7 100644 --- a/src/docs/manual/dev.html +++ b/src/docs/manual/dev.html @@ -1090,8 +1090,8 @@

Creating sequences

   TypedOperation newOb = TypedOperation.createPrimitiveInitialization(ConcreteTypes.STRING_TYPE, "hi!");
-  TypedOperation addFirst = TypedOperation.forMethod(LinkedList.class.getMethod("addFirst", Object.class), AccessibilityPredicate.IS_PUBLIC).substitute(substLL);
-  TypedOperation size = TypedOperation.forMethod(LinkedList.class.getMethod("size"), AccessibilityPredicate.IS_PUBLIC).substitute(substLL);
+  TypedOperation addFirst = TypedOperation.forMethod(LinkedList.class.getMethod("addFirst", Object.class)).substitute(substLL);
+  TypedOperation size = TypedOperation.forMethod(LinkedList.class.getMethod("size")).substitute(substLL);
 

@@ -1114,7 +1114,7 @@

Creating sequences

-   TypedOperation syncA = TypedOperation.forMethod(Collections.class.getMethod("synchronizedSet", Set.class), AccessibilityPredicate.IS_PUBLIC);
+   TypedOperation syncA = TypedOperation.forMethod(Collections.class.getMethod("synchronizedSet", Set.class));
    Substitution<ReferenceType> substA = new Substitution(syncA.getTypeParameters(), (ReferenceType)ConcreteTypes.STRING_TYPE);
    TypedOperation syncS = syncA.substitute(substA);
 
diff --git a/src/docs/manual/index.html b/src/docs/manual/index.html index d18f38547d..aa2032ea8e 100644 --- a/src/docs/manual/index.html +++ b/src/docs/manual/index.html @@ -1013,7 +1013,7 @@

Command-line options

File that contains fully-qualified field names to be excluded from test generation. An accessible field is used unless it is omitted by this or the --omit-field option.
  • --only-test-public-members=boolean. - Restrict tests to only include public members of classes. + Restrict tests to only call public members of classes.

    When this is false, the setting of --junit-package-name and package accessibility is used to determine which members will be used in tests. [default: false] diff --git a/src/main/java/randoop/contract/CheckRepContract.java b/src/main/java/randoop/contract/CheckRepContract.java index a7d5004ebc..da79fd0456 100644 --- a/src/main/java/randoop/contract/CheckRepContract.java +++ b/src/main/java/randoop/contract/CheckRepContract.java @@ -8,7 +8,6 @@ import randoop.main.RandoopBug; import randoop.operation.TypedClassOperation; import randoop.operation.TypedOperation; -import randoop.reflection.AccessibilityPredicate; import randoop.types.JavaTypes; import randoop.types.TypeTuple; @@ -53,8 +52,7 @@ public CheckRepContract(Method checkRepMethod) { assert Modifier.isPublic(modifiers); assert !Modifier.isStatic(modifiers); assert checkRepMethod.getParameterTypes().length == 0; - // accessibility predicate shouldn't matter for generating output type - this.operation = TypedOperation.forMethod(checkRepMethod, AccessibilityPredicate.IS_ANY); + this.operation = TypedOperation.forMethod(checkRepMethod); if (operation.getOutputType().equals(JavaTypes.BOOLEAN_TYPE)) { this.returnsBoolean = true; } else if (operation.getOutputType().equals(JavaTypes.VOID_TYPE)) { diff --git a/src/main/java/randoop/generation/HelperSequenceCreator.java b/src/main/java/randoop/generation/HelperSequenceCreator.java index 5c1ff5304e..7e3f4d75fc 100644 --- a/src/main/java/randoop/generation/HelperSequenceCreator.java +++ b/src/main/java/randoop/generation/HelperSequenceCreator.java @@ -416,8 +416,7 @@ private static TypedOperation getEnumSetCreation(ParameterizedType creationType) } catch (NoSuchMethodException e) { throw new RandoopBug("Can't find \"noneOf\" method for EnumSet: ", e); } - // accessibility predicate shouldn't matter for accessible method - MethodCall op = new MethodCall(method, true); + MethodCall op = new MethodCall(method); List paramTypes = Collections.singletonList(JavaTypes.CLASS_TYPE); return new TypedClassOperation(op, creationType, new TypeTuple(paramTypes), creationType); } @@ -437,8 +436,7 @@ private static TypedOperation getAddOperation( } catch (NoSuchMethodException e) { throw new RandoopBug("Can't find add() method for " + collectionType, e); } - // accessibility predicate shouldn't matter - MethodCall op = new MethodCall(addMethod, true); + MethodCall op = new MethodCall(addMethod); List arguments = new ArrayList<>(2); arguments.add(collectionType); arguments.add(elementType); @@ -464,8 +462,7 @@ private static TypedOperation getCollectionAddAllOperation(ReferenceType element } catch (NoSuchMethodException e) { throw new RandoopBug("Can't find Collections.addAll method", e); } - // accessibility predicate shouldn't matter - MethodCall op = new MethodCall(method, true); + MethodCall op = new MethodCall(method); assert method.getTypeParameters().length == 1 : "method should have one type parameter"; List paramTypes = new ArrayList<>(2); ParameterizedType collectionType = JDKTypes.COLLECTION_TYPE.instantiate(elementType); diff --git a/src/main/java/randoop/main/GenInputsAbstract.java b/src/main/java/randoop/main/GenInputsAbstract.java index 93a2d38204..f0d5b0be1f 100644 --- a/src/main/java/randoop/main/GenInputsAbstract.java +++ b/src/main/java/randoop/main/GenInputsAbstract.java @@ -227,7 +227,7 @@ protected GenInputsAbstract( public static Path omit_field_list = null; /** - * Restrict tests to only include public members of classes. + * Restrict tests to only call public members of classes. * *

    When this is false, the setting of {@code --junit-package-name} and package accessibility is * used to determine which members will be used in tests. diff --git a/src/main/java/randoop/operation/MethodCall.java b/src/main/java/randoop/operation/MethodCall.java index b321801667..7b92711eca 100644 --- a/src/main/java/randoop/operation/MethodCall.java +++ b/src/main/java/randoop/operation/MethodCall.java @@ -11,7 +11,6 @@ import randoop.ExecutionOutcome; import randoop.Globals; import randoop.NormalExecution; -import randoop.reflection.AccessibilityPredicate; import randoop.reflection.ReflectionPredicate; import randoop.sequence.Variable; import randoop.types.Type; @@ -46,13 +45,14 @@ public final class MethodCall extends CallableOperation { private final boolean isStatic; /** - * True if the method is accessible. If {@code --only-test-public-members} is set to true, a - * method is considered accessible iff it's public. Additionally, if {@code --junit-package-name} - * is null, a method is considered accessible iff it's public. Otherwise, a method is considered + * True if the method should be called reflectively. (This is generally because the method is not + * accessible from the JUnit code.) If {@code --only-test-public-members} is set to true, a method + * is considered accessible iff it's public. Additionally, if {@code --junit-package-name} is + * null, a method is considered accessible iff it's public. Otherwise, a method is considered * accessible if it is either public or accessible relative to the given package name specified by - * {@code --junit-package-name} + * {@code --junit-package-name}. */ - private boolean isAccessible; + private boolean callReflectively; /** * getMethod returns Method object of this MethodCall. @@ -64,19 +64,30 @@ public Method getMethod() { } /** - * MethodCall creates an object corresponding to the given reflective method. + * Creates an object corresponding to a call to the given method. + * + *

    In generated tests, the method will be called normally, not reflectively. + * + * @param method the reflective method object + */ + public MethodCall(Method method) { + this(method, false); + } + + /** + * Creates an object corresponding to a call to the given method. * * @param method the reflective method object - * @param isAccessible boolean indicating if the method is accessible + * @param callReflectively if true, the method should be called reflectively */ - public MethodCall(Method method, boolean isAccessible) { + public MethodCall(Method method, boolean callReflectively) { if (method == null) { throw new IllegalArgumentException("method should not be null."); } this.method = method; this.isStatic = Modifier.isStatic(method.getModifiers() & Modifier.methodModifiers()); - this.isAccessible = isAccessible; + this.callReflectively = callReflectively; } /** @@ -110,7 +121,30 @@ public void appendCode( // The name of a variable (in the test that Randoop outputs) that holds the Method object. String methodVar = getVariableNameForMethodObject(); - if (!isAccessible) { + if (!callReflectively) { + if (isStatic()) { + // In the generated Java code, the "receiver" (before the method name) for a static method + // call is the class name. + sb.append(declaringType.getCanonicalName().replace('$', '.')); + } else { + // In the generated Java code, the receiver is an expression. + String receiverVar = isStatic() ? null : inputVars.get(0).getName(); + Type receiverFormalType = inputTypes.get(0); + if (receiverFormalType.isPrimitive()) { + sb.append("((") + .append(receiverFormalType.getFqName()) + .append(")") + .append(receiverVar) + .append(")"); + } else { + sb.append(receiverVar); + } + } + + sb.append("."); + sb.append(methodName); + } else { + // TODO: For brevity here, perhaps abstract this `if` statement into a separate method. if (!Globals.makeAccessibleCode.containsKey(methodVar)) { StringBuilder makeMethodAccessibleBuilder = new StringBuilder(); makeMethodAccessibleBuilder @@ -133,31 +167,7 @@ public void appendCode( .append(System.lineSeparator()); Globals.makeAccessibleCode.put(methodVar, makeMethodAccessibleBuilder.toString()); } - } - String receiverVar = isStatic() ? null : inputVars.get(0).getName(); - if (isAccessible) { - if (isStatic()) { - // In the generated Java code, the "receiver" (before the method name) is the class name. - sb.append(declaringType.getCanonicalName().replace('$', '.')); - } else { - // In this branch, isAcessible == false. - // In the generated Java code, the receiver is an expression. - Type receiverFormalType = inputTypes.get(0); - if (receiverFormalType.isPrimitive()) { - sb.append("((") - .append(receiverFormalType.getFqName()) - .append(")") - .append(receiverVar) - .append(")"); - } else { - sb.append(receiverVar); - } - } - - sb.append("."); - sb.append(methodName); - } else { if (!outputType.isVoid()) { // Cast because the return type of `invoke()` is Object. sb.append("(").append(outputType.getFqName()).append(") "); @@ -167,9 +177,9 @@ public void appendCode( StringJoiner arguments = new StringJoiner(", ", "(", ")"); // In a reflective call, a receiver is always passed (even if it's null). - int startIndex = !isAccessible || isStatic() ? 0 : 1; + int startIndex = callReflectively || isStatic() ? 0 : 1; for (int i = startIndex; i < inputVars.size(); i++) { - if (i == 0 && !isAccessible && isStatic()) { + if (i == 0 && callReflectively && isStatic()) { // There is no harm to passing inputVars.get(0), but pass // null to emphasize that the first (receiver) argument is ignored. sb.append("null"); @@ -355,8 +365,8 @@ public static TypedClassOperation parse(String signature) throws OperationParseE throw new OperationParseException(msg); } } - // accessibility predicate shouldn't matter for generating output type - return TypedClassOperation.forMethod(m, AccessibilityPredicate.IS_ANY); + + return TypedClassOperation.forMethod(m); } /** diff --git a/src/main/java/randoop/operation/TypedOperation.java b/src/main/java/randoop/operation/TypedOperation.java index 63d56a518f..deb587eb2d 100644 --- a/src/main/java/randoop/operation/TypedOperation.java +++ b/src/main/java/randoop/operation/TypedOperation.java @@ -364,6 +364,16 @@ public static TypedClassOperation forConstructor(Constructor constructor) { return new TypedClassOperation(op, declaringType, inputTypes, declaringType); } + /** + * Constructs a {@link TypedOperation} for a method object. + * + * @param method the reflective method object + * @return the typed operation for the given method + */ + public static TypedClassOperation forMethod(Method method) { + return forMethod(method, AccessibilityPredicate.IS_ANY); + } + /** * Constructs a {@link TypedOperation} for a method object. * @@ -386,7 +396,7 @@ public static TypedClassOperation forMethod( } List paramTypes = new ArrayList<>(methodParamTypes.size() + 1); - MethodCall op = new MethodCall(method, accessibilityPredicate.isAccessible(method)); + MethodCall op = new MethodCall(method, !accessibilityPredicate.isAccessible(method)); ClassOrInterfaceType declaringType = ClassOrInterfaceType.forClass(method.getDeclaringClass()); if (!op.isStatic()) { paramTypes.add(declaringType); @@ -430,7 +440,7 @@ private static TypedClassOperation getAnonEnumOperation( continue; } List paramTypes = new ArrayList<>(mGenericParamTypes.length + 1); - MethodCall op = new MethodCall(publicMethod, true); + MethodCall op = new MethodCall(publicMethod); if (!op.isStatic()) { paramTypes.add(enumType); } diff --git a/src/main/java/randoop/reflection/OperationModel.java b/src/main/java/randoop/reflection/OperationModel.java index c0bf551b52..fae38498c9 100644 --- a/src/main/java/randoop/reflection/OperationModel.java +++ b/src/main/java/randoop/reflection/OperationModel.java @@ -621,8 +621,8 @@ private void addClassTypes( succeeded++; } catch (Throwable e) { System.out.printf( - "Cannot get methods for %s specified via --testclass or --classlist" - + " due to exception:%n%s%n", + "Cannot get methods for %s specified via " + + "--testclass or --classlist due to exception:%n%s%n", c.getName(), UtilPlume.stackTraceToString(e)); } } @@ -632,8 +632,8 @@ private void addClassTypes( succeeded++; } catch (Throwable e) { System.out.printf( - "Cannot get methods for %s specified via --testclass or --classlist" - + " due to exception:%n%s%n", + "Cannot get methods for %s specified via " + + "--testclass or --classlist due to exception:%n%s%n", c.getName(), UtilPlume.stackTraceToString(e)); } } diff --git a/src/main/java/randoop/reflection/ReflectionManager.java b/src/main/java/randoop/reflection/ReflectionManager.java index 5365150156..f7ae4ace5b 100644 --- a/src/main/java/randoop/reflection/ReflectionManager.java +++ b/src/main/java/randoop/reflection/ReflectionManager.java @@ -156,7 +156,8 @@ public void apply(ClassVisitor visitor, Class c) { m.setAccessible(true); applyTo(visitor, m); } catch (Exception e) { - // Method cannot be reflectively called + // Method cannot be reflectively called. + // TODO: some kind of error should be thrown here (or at least logging?). } } else if (isAccessible(m)) { if (classIsAccessible || Modifier.isStatic(m.getModifiers())) { diff --git a/src/main/java/randoop/test/ExceptionCheck.java b/src/main/java/randoop/test/ExceptionCheck.java index 28debb44dc..ca9f492abf 100644 --- a/src/main/java/randoop/test/ExceptionCheck.java +++ b/src/main/java/randoop/test/ExceptionCheck.java @@ -112,7 +112,7 @@ public final String toCodeStringPostStatement() { } /** - * Appends code for catch block behavior corresponding to expected exception. + * Appends code for catch block behavior corresponding to an expected exception. * * @param b the string builder to which code text is to be added * @param catchClassName the name of the exception to be caught diff --git a/src/main/java/randoop/test/ExpectedExceptionCheck.java b/src/main/java/randoop/test/ExpectedExceptionCheck.java index d954c4d457..6609fa9226 100644 --- a/src/main/java/randoop/test/ExpectedExceptionCheck.java +++ b/src/main/java/randoop/test/ExpectedExceptionCheck.java @@ -17,7 +17,7 @@ public class ExpectedExceptionCheck extends ExceptionCheck { /** A boolean indicating the accessibility of the method. */ - private boolean isAccessible; + private boolean callReflectively; /** * Creates check that enforces expectation that an exception is thrown by the statement at the @@ -32,9 +32,9 @@ public class ExpectedExceptionCheck extends ExceptionCheck { * @param catchClassName the name of exception to be caught */ public ExpectedExceptionCheck( - Throwable exception, int statementIndex, String catchClassName, boolean isAccessible) { + Throwable exception, int statementIndex, String catchClassName, boolean callReflectively) { super(exception, statementIndex, catchClassName); - this.isAccessible = isAccessible; + this.callReflectively = callReflectively; } /** @@ -44,28 +44,29 @@ public ExpectedExceptionCheck( */ @Override protected void appendTryBehavior(StringBuilder b) { - String message; - String assertion; - if (exception.getClass().isAnonymousClass()) { - message = "Expected anonymous exception"; + String assertionMessage; + if (callReflectively) { + assertionMessage = "Expected exception of type java.lang.reflect.InvocationTargetException"; } else { - String exceptionMessage; - try { - exceptionMessage = "; message: " + toAscii(exception.getMessage()); - } catch (Throwable t) { - exceptionMessage = " whose getMessage() throws an exception"; + String message; + if (exception.getClass().isAnonymousClass()) { + message = "Expected anonymous exception"; + } else { + String exceptionMessage; + try { + exceptionMessage = "; message: " + toAscii(exception.getMessage()); + } catch (Throwable t) { + exceptionMessage = " whose getMessage() throws an exception"; + } + message = "Expected exception of type " + getExceptionName() + exceptionMessage; } - message = "Expected exception of type " + getExceptionName() + exceptionMessage; + assertionMessage = StringsPlume.escapeJava(message); } - if (isAccessible) { - assertion = "org.junit.Assert.fail(\"" + StringsPlume.escapeJava(message) + "\")"; - } else { - assertion = - "org.junit.Assert.fail(\"" - + "Expected exception of type java.lang.reflect.InvocationTargetException" - + "\")"; - } - b.append(Globals.lineSep).append(" ").append(assertion).append(";").append(Globals.lineSep); + b.append(Globals.lineSep) + .append(" org.junit.Assert.fail(\"") + .append(assertionMessage) + .append("\");") + .append(Globals.lineSep); } /** @@ -89,7 +90,7 @@ protected void appendTryBehavior(StringBuilder b) { */ @Override protected void appendCatchBehavior(StringBuilder b, String catchClassName) { - if (!isAccessible) { + if (callReflectively) { b.append("catch (") .append("java.lang.reflect.InvocationTargetException") .append(" e) {") diff --git a/src/test/java/randoop/condition/OperationSpecificationTest.java b/src/test/java/randoop/condition/OperationSpecificationTest.java index 4fc05610b9..8ee550bdd3 100644 --- a/src/test/java/randoop/condition/OperationSpecificationTest.java +++ b/src/test/java/randoop/condition/OperationSpecificationTest.java @@ -25,7 +25,6 @@ import randoop.condition.specification.ThrowsCondition; import randoop.operation.TypedClassOperation; import randoop.operation.TypedOperation; -import randoop.reflection.AccessibilityPredicate; import randoop.sequence.ExecutableSequence; import randoop.sequence.Sequence; import randoop.sequence.Variable; @@ -193,8 +192,7 @@ private ExecutableSequence createCategorySequence(int value) throws NoSuchMethod Constructor reflectionConstructor = c.getConstructor(int.class); TypedClassOperation constructorOp = TypedOperation.forConstructor(reflectionConstructor); Method method = c.getDeclaredMethod("category", int.class); - TypedClassOperation methodOp = - TypedOperation.forMethod(method, AccessibilityPredicate.IS_PUBLIC); + TypedClassOperation methodOp = TypedOperation.forMethod(method); methodOp.setExecutableSpecification(getMethodSpecification(method)); Sequence sequence = new Sequence(); @@ -212,8 +210,7 @@ private ExecutableSequence createCategorySequence(int value) throws NoSuchMethod private ExecutableSequence createBadnessSequence() throws NoSuchMethodException { Class c = ClassWithConditions.class; Method method = c.getDeclaredMethod("badness", ClassWithConditions.Range.class, int.class); - TypedClassOperation methodOp = - TypedOperation.forMethod(method, AccessibilityPredicate.IS_PUBLIC); + TypedClassOperation methodOp = TypedOperation.forMethod(method); methodOp.setExecutableSpecification(getBadnessConditions(method)); Sequence sequence = new Sequence(); diff --git a/src/test/java/randoop/condition/SpecificationTranslatorTest.java b/src/test/java/randoop/condition/SpecificationTranslatorTest.java index 76214f9213..2fcad61902 100644 --- a/src/test/java/randoop/condition/SpecificationTranslatorTest.java +++ b/src/test/java/randoop/condition/SpecificationTranslatorTest.java @@ -26,7 +26,6 @@ import randoop.condition.specification.OperationSpecification; import randoop.operation.TypedClassOperation; import randoop.operation.TypedOperation; -import randoop.reflection.AccessibilityPredicate; import randoop.sequence.ExecutableSequence; import randoop.sequence.Sequence; import randoop.sequence.Variable; @@ -62,9 +61,7 @@ public void testPrintWriterAppend() { assertEquals("x2.equals(x0)", Util.replaceWords("result.equals(receiver)", replacements)); String conditionText = "result.equals(receiver)"; - Sequence sequence = - createPrintWriterSequence( - TypedOperation.forMethod(method, AccessibilityPredicate.IS_PUBLIC)); + Sequence sequence = createPrintWriterSequence(TypedOperation.forMethod(method)); List postConditions = new ArrayList<>(); Method conditionMethod = @@ -163,8 +160,7 @@ public void testSignatureFromFile() { SpecificationCollection collection = SpecificationCollection.create(specList); ExecutableSpecification execSpec = collection.getExecutableSpecification(method); - TypedClassOperation appendOp = - TypedOperation.forMethod(method, AccessibilityPredicate.IS_PUBLIC); + TypedClassOperation appendOp = TypedOperation.forMethod(method); appendOp.setExecutableSpecification(execSpec); Sequence sequence = createPrintWriterSequence(appendOp); ExecutableSequence eseq = new ExecutableSequence(sequence); diff --git a/src/test/java/randoop/operation/EnumReflectionTest.java b/src/test/java/randoop/operation/EnumReflectionTest.java index d54fb11392..9685133510 100644 --- a/src/test/java/randoop/operation/EnumReflectionTest.java +++ b/src/test/java/randoop/operation/EnumReflectionTest.java @@ -68,7 +68,7 @@ public void simpleEnumTest() { for (Method m : exclude) { assertFalse( "method " + m.toGenericString() + " should not occur in simple enum", - actual.contains(createMethodCall(m, declaringType, true))); + actual.contains(createMethodCall(m, declaringType))); } } @@ -134,11 +134,11 @@ public void innerEnumWithMethodsTest() { include.add(createEnumOperation(e)); } for (Method m : c.getDeclaredMethods()) { - if (Modifier.isPublic(m.getModifiers())) { // extract only public methods + if (Modifier.isPublic(m.getModifiers())) { if (!m.getName().equals("values") && !m.getName().equals("valueOf")) { - include.add(createMethodCall(m, enumType, true)); + include.add(createMethodCall(m, enumType)); } else { - exclude.add(createMethodCall(m, enumType, true)); + exclude.add(createMethodCall(m, enumType)); } } } @@ -180,16 +180,16 @@ public void enumAsPredicateTest() { if (opSet == null) { opSet = new HashSet<>(); } - opSet.add(createMethodCall(m, enumType, true)); + opSet.add(createMethodCall(m, enumType)); overrideMap.put(m.getName(), opSet); } } for (Method m : c.getDeclaredMethods()) { - if (Modifier.isPublic(m.getModifiers())) { // extract only public methods + if (Modifier.isPublic(m.getModifiers())) { if (!m.getName().equals("values") && !m.getName().equals("valueOf")) { - include.add(createMethodCall(m, enumType, true)); + include.add(createMethodCall(m, enumType)); } else { - exclude.add(createMethodCall(m, enumType, true)); + exclude.add(createMethodCall(m, enumType)); } } } @@ -198,7 +198,7 @@ public void enumAsPredicateTest() { Set opSet = overrideMap.get(publicMethod.getName()); if (opSet != null) { TypedClassOperation actualEnumOp = - createMethodCall(publicMethod, enumType, true) + createMethodCall(publicMethod, enumType) .substitute(interfaceType.getTypeSubstitution()); include.add(actualEnumOp); } @@ -264,7 +264,7 @@ public void valueEnum() { } for (Method publicMethod : coin.getMethods()) { - TypedOperation mc = createMethodCall(publicMethod, declaringType, true); + TypedOperation mc = createMethodCall(publicMethod, declaringType); if (publicMethod.getName().equals("value")) { assertTrue( "enum method " + publicMethod.toGenericString() + " should occur", actual.contains(mc)); @@ -303,7 +303,7 @@ public void abstractMethodEnum() { } for (Method publicMethod : op.getMethods()) { - TypedOperation mc = createMethodCall(publicMethod, declaringType, true); + TypedOperation mc = createMethodCall(publicMethod, declaringType); if (overrides.contains(publicMethod.getName())) { assertTrue("enum method " + mc + " should occur", actual.contains(mc)); count++; @@ -338,9 +338,8 @@ private TypedClassOperation createConstructorCall(Constructor con) return new TypedClassOperation(op, declaringType, new TypeTuple(paramTypes), declaringType); } - private TypedClassOperation createMethodCall( - Method m, ClassOrInterfaceType declaringType, boolean isAccessible) { - MethodCall op = new MethodCall(m, isAccessible); + private TypedClassOperation createMethodCall(Method m, ClassOrInterfaceType declaringType) { + MethodCall op = new MethodCall(m); List paramTypes = new ArrayList<>(); paramTypes.add(declaringType); for (java.lang.reflect.Type t : m.getGenericParameterTypes()) { diff --git a/src/test/java/randoop/operation/OperationParserTests.java b/src/test/java/randoop/operation/OperationParserTests.java index 7e64d8ff27..d2882b1aea 100644 --- a/src/test/java/randoop/operation/OperationParserTests.java +++ b/src/test/java/randoop/operation/OperationParserTests.java @@ -117,7 +117,7 @@ public void testRMethod() { } TypeTuple inputTypes = new TypeTuple(paramTypes); Type outputType = Type.forType(publicMethod.getGenericReturnType()); - checkParse(new MethodCall(publicMethod, true), declaringType, inputTypes, outputType); + checkParse(new MethodCall(publicMethod), declaringType, inputTypes, outputType); } } diff --git a/src/test/java/randoop/operation/TypedOperationTest.java b/src/test/java/randoop/operation/TypedOperationTest.java index c348bf9798..cb1ed68c3f 100644 --- a/src/test/java/randoop/operation/TypedOperationTest.java +++ b/src/test/java/randoop/operation/TypedOperationTest.java @@ -6,7 +6,6 @@ import java.lang.reflect.Method; import org.junit.Test; -import randoop.reflection.AccessibilityPredicate; import randoop.types.TypeVariable; public class TypedOperationTest { @@ -20,7 +19,7 @@ public void testOperationParameterTypes() { fail("failed to load method ParameterInput.m()"); throw new Error("unreachable"); } - TypedClassOperation operation = TypedOperation.forMethod(m, AccessibilityPredicate.IS_PUBLIC); + TypedClassOperation operation = TypedOperation.forMethod(m); assertFalse( "operation has type parameters: " + operation, operation.getTypeParameters().isEmpty()); TypedClassOperation capOp = operation.applyCaptureConversion(); diff --git a/src/test/java/randoop/reflection/AccessibilityTest.java b/src/test/java/randoop/reflection/AccessibilityTest.java index 74a2faaeb9..ecea216e1b 100644 --- a/src/test/java/randoop/reflection/AccessibilityTest.java +++ b/src/test/java/randoop/reflection/AccessibilityTest.java @@ -131,7 +131,7 @@ public void testStandardPackagePrivateAccessibility() throws ClassNotFoundExcept for (Method m : expectedMethods) { assertTrue( "method " + m.getName() + " should occur", - actual.contains(createMethodCall(m, declaringType, accessibility.isAccessible(m)))); + actual.contains(createMethodCall(m, declaringType, !accessibility.isAccessible(m)))); } } catch (RandoopTypeException e) { fail("Type error: " + e.getMessage()); @@ -229,7 +229,7 @@ public void testPublicOnlyPackagePrivateAccessibility() throws ClassNotFoundExce for (Method m : expectedMethods) { assertFalse( "method " + m.getName() + " should occur", - actual.contains(createMethodCall(m, declaringType, true))); + actual.contains(createMethodCall(m, declaringType))); } } catch (RandoopTypeException e) { fail("Type error: " + e.getMessage()); @@ -326,7 +326,7 @@ public void testStandardAccessibility() { for (Method m : expectedMethods) { assertTrue( "method " + m.getName() + " should occur", - actual.contains(createMethodCall(m, declaringType, accessibility.isAccessible(m)))); + actual.contains(createMethodCall(m, declaringType, !accessibility.isAccessible(m)))); } } catch (RandoopTypeException e) { fail("Type error: " + e.getMessage()); @@ -436,8 +436,8 @@ public void testPublicOnlyAccessibility() { assertTrue( "method " + m.getName() + " should occur", actual.contains( - createMethodCall( - m, declaringType, true))); // every method is public in expectedMethods + // every method is public in expectedMethods + createMethodCall(m, declaringType))); } for (Constructor co : expectedConstructors) { @@ -560,10 +560,15 @@ private TypedOperation createConstructorCall(Constructor con) throws RandoopT return new TypedClassOperation(op, declaringType, new TypeTuple(paramTypes), declaringType); } + private TypedOperation createMethodCall(Method m, ClassOrInterfaceType declaringType) + throws RandoopTypeException { + return createMethodCall(m, declaringType, false); + } + private TypedOperation createMethodCall( - Method m, ClassOrInterfaceType declaringType, boolean isAccessible) + Method m, ClassOrInterfaceType declaringType, boolean reflectiveCall) throws RandoopTypeException { - MethodCall op = new MethodCall(m, isAccessible); + MethodCall op = new MethodCall(m, reflectiveCall); List paramTypes = new ArrayList<>(); if (!Modifier.isStatic(m.getModifiers() & Modifier.methodModifiers())) { paramTypes.add(declaringType); diff --git a/src/test/java/randoop/reflection/SignatureParserTest.java b/src/test/java/randoop/reflection/SignatureParserTest.java index baadb78ea0..c4e54e7d2d 100644 --- a/src/test/java/randoop/reflection/SignatureParserTest.java +++ b/src/test/java/randoop/reflection/SignatureParserTest.java @@ -70,7 +70,7 @@ private void checkParse(String inputString) throws SignatureParseException { TypedClassOperation operation = (accessibleObject instanceof Constructor) ? TypedOperation.forConstructor((Constructor) accessibleObject) - : TypedOperation.forMethod((Method) accessibleObject, AccessibilityPredicate.IS_PUBLIC); + : TypedOperation.forMethod((Method) accessibleObject); String expectedString = inputString.replace(" ", "").replace(".", ""); String signatureString = operation.getRawSignature().toString(); diff --git a/src/test/java/randoop/sequence/DevExampleCode.java b/src/test/java/randoop/sequence/DevExampleCode.java index 9014aa9bc9..c6bffc03c4 100644 --- a/src/test/java/randoop/sequence/DevExampleCode.java +++ b/src/test/java/randoop/sequence/DevExampleCode.java @@ -38,14 +38,10 @@ public void devDocExampleTest() { TypedOperation newOb = TypedOperation.createPrimitiveInitialization(JavaTypes.STRING_TYPE, "hi!"); TypedOperation addFirst = - TypedOperation.forMethod( - LinkedList.class.getMethod("addFirst", Object.class), - AccessibilityPredicate.IS_PUBLIC) + TypedOperation.forMethod(LinkedList.class.getMethod("addFirst", Object.class)) .substitute(substLL); TypedOperation size = - TypedOperation.forMethod( - LinkedList.class.getMethod("size"), AccessibilityPredicate.IS_PUBLIC) - .substitute(substLL); + TypedOperation.forMethod(LinkedList.class.getMethod("size")).substitute(substLL); // Call to operation with wildcard in TreeSet InstantiatedType treeSetType = JDKTypes.TREE_SET_TYPE.instantiate(JavaTypes.STRING_TYPE); diff --git a/src/test/java/randoop/types/CaptureConversionTest.java b/src/test/java/randoop/types/CaptureConversionTest.java index 961c8c2291..9b846dbdbd 100644 --- a/src/test/java/randoop/types/CaptureConversionTest.java +++ b/src/test/java/randoop/types/CaptureConversionTest.java @@ -9,7 +9,6 @@ import org.junit.BeforeClass; import org.junit.Test; import randoop.operation.TypedOperation; -import randoop.reflection.AccessibilityPredicate; import randoop.types.test.CaptureTestClass; import randoop.types.test.Container; import randoop.types.test.Gibberish; @@ -31,23 +30,13 @@ public static void setup() { listOperations = new ArrayList<>(); containerOperations = new ArrayList<>(); try { - listOperations.add( - TypedOperation.forMethod(c.getMethod("a", List.class), AccessibilityPredicate.IS_PUBLIC)); - listOperations.add( - TypedOperation.forMethod(c.getMethod("b", List.class), AccessibilityPredicate.IS_PUBLIC)); - listOperations.add( - TypedOperation.forMethod(c.getMethod("c", List.class), AccessibilityPredicate.IS_PUBLIC)); - listOperations.add( - TypedOperation.forMethod(c.getMethod("d", List.class), AccessibilityPredicate.IS_PUBLIC)); - containerOperations.add( - TypedOperation.forMethod( - c.getMethod("a", Container.class), AccessibilityPredicate.IS_PUBLIC)); - containerOperations.add( - TypedOperation.forMethod( - c.getMethod("b", Container.class), AccessibilityPredicate.IS_PUBLIC)); - containerOperations.add( - TypedOperation.forMethod( - c.getMethod("c", Container.class), AccessibilityPredicate.IS_PUBLIC)); + listOperations.add(TypedOperation.forMethod(c.getMethod("a", List.class))); + listOperations.add(TypedOperation.forMethod(c.getMethod("b", List.class))); + listOperations.add(TypedOperation.forMethod(c.getMethod("c", List.class))); + listOperations.add(TypedOperation.forMethod(c.getMethod("d", List.class))); + containerOperations.add(TypedOperation.forMethod(c.getMethod("a", Container.class))); + containerOperations.add(TypedOperation.forMethod(c.getMethod("b", Container.class))); + containerOperations.add(TypedOperation.forMethod(c.getMethod("c", Container.class))); } catch (NoSuchMethodException e) { fail("didn't find method: " + e.getMessage()); } diff --git a/src/test/java/randoop/types/TypeVariableTest.java b/src/test/java/randoop/types/TypeVariableTest.java index d7ee7a0e06..ce0e147bf7 100644 --- a/src/test/java/randoop/types/TypeVariableTest.java +++ b/src/test/java/randoop/types/TypeVariableTest.java @@ -11,7 +11,6 @@ import org.junit.Test; import randoop.operation.TypedClassOperation; import randoop.operation.TypedOperation; -import randoop.reflection.AccessibilityPredicate; import randoop.types.test.VariablesInput; public class TypeVariableTest { @@ -29,8 +28,7 @@ public int compare(Method o1, Method o2) { Collections.addAll(methods, c.getDeclaredMethods()); for (Method m : methods) { if (!m.getName().equals("$jacocoInit")) { - TypedClassOperation operation = - TypedOperation.forMethod(m, AccessibilityPredicate.IS_PUBLIC).applyCaptureConversion(); + TypedClassOperation operation = TypedOperation.forMethod(m).applyCaptureConversion(); ReferenceType parameterType = (ReferenceType) operation.getInputTypes().get(1); TypeVariable variable;