2424import spoon .reflect .declaration .CtElement ;
2525import spoon .reflect .declaration .CtType ;
2626import spoon .reflect .reference .CtExecutableReference ;
27+ import spoon .reflect .reference .CtTypeParameterReference ;
2728import spoon .reflect .reference .CtTypeReference ;
2829
2930/**
@@ -92,12 +93,14 @@ public static List<GhostState> ghostStatesOf(TypeChecker tc, String className) {
9293
9394 private static List <GhostState > ghostStatesOf (Context context , CtTypeReference <?> type ,
9495 List <CtTypeReference <?>> supertypes ) {
95- // a state set can be registered more than once, so states are told apart by name
96+ // a state set can be registered more than once, so states are told apart by name, and states are kept by
97+ // simple class name, so those of another class with the same simple name are left out
9698 Map <String , GhostState > ghostStates = new LinkedHashMap <>();
9799 for (CtTypeReference <?> t : Stream .concat (Stream .of (type ), supertypes .stream ()).toList ()) {
98100 List <GhostState > declared = context .getGhostState (Utils .getSimpleName (t .getQualifiedName ()));
99101 if (declared != null )
100- declared .forEach (g -> ghostStates .putIfAbsent (g .getQualifiedName (), g ));
102+ declared .stream ().filter (g -> t .getQualifiedName ().equals (g .getPrefix ()))
103+ .forEach (g -> ghostStates .putIfAbsent (g .getQualifiedName (), g ));
101104 }
102105 return List .copyOf (ghostStates .values ());
103106 }
@@ -172,16 +175,22 @@ private StateMachine build(CtTypeReference<?> type, List<CtTypeReference<?>> sup
172175 continue ;
173176 boolean constructor = CtExecutableReference .CONSTRUCTOR_NAME .equals (function .getName ());
174177 if (className .equals (function .getTargetClass ()) && constructor ) {
178+ if (abstractType (type ))
179+ continue ; // cannot be suggested as a new object
175180 Optional <Set <State >> created = createdStates (function );
176181 created .ifPresent (s -> constructors .put (function , s ));
177182 continue ;
178183 }
184+ if (className .equals (function .getTargetClass ()) && !appliesToOwnType (function , type , supertypes )) {
185+ complete = false ;
186+ continue ;
187+ }
179188 if (!className .equals (function .getTargetClass ())) {
180189 if (constructor || !inherited .contains (function .getTargetClass ()))
181190 continue ;
182191 if (overridden (function , className ))
183192 continue ; // the class's own refinement for it is the one in the machine
184- if (!inheritedFrom (function , type , supertypes )) {
193+ if (!resolvesTo (function , type , supertypes )) {
185194 complete = false ;
186195 continue ;
187196 }
@@ -205,6 +214,24 @@ private boolean subclassChangesState(String className, CtTypeReference<?> type,
205214 .map (c -> tc .getFactory ().Type ().createReference (c )).anyMatch (other -> other .isSubtypeOf (type ));
206215 }
207216
217+ private static boolean abstractType (CtTypeReference <?> type ) {
218+ CtType <?> declaration = type .getTypeDeclaration ();
219+ return declaration == null || declaration .isInterface () || declaration .isAbstract ();
220+ }
221+
222+ /**
223+ * Whether the verifier checks a call to {@code method}, specified for {@code type}, on an object of that type. It
224+ * looks the refinement up by the receiver's type, which needs the parameter types to match those of the method it
225+ * resolves to, and otherwise by the type that declares that method. A parameter whose type is a type variable may
226+ * not match, so then {@code type} must declare the method.
227+ */
228+ private boolean appliesToOwnType (RefinedFunction method , CtTypeReference <?> type ,
229+ List <CtTypeReference <?>> supertypes ) {
230+ boolean concreteParameters = method .getArguments ().stream ()
231+ .noneMatch (a -> a .getType () instanceof CtTypeParameterReference );
232+ return concreteParameters || resolvesTo (method , type , supertypes );
233+ }
234+
208235 /** Whether {@code className} has its own refinement for {@code method}, with the same parameter types */
209236 private boolean overridden (RefinedFunction method , String className ) {
210237 String name = Utils .getSimpleName (method .getName ());
@@ -219,13 +246,11 @@ private static List<String> parameterTypes(RefinedFunction function) {
219246 }
220247
221248 /**
222- * Whether the verifier checks a call to {@code method}, specified for a supertype, on an object of {@code type}
223- * that has no refinement of its own for it. Like the verifier, it uses the refinement for the nearest type that
224- * declares the method. Methods are matched by name and number of parameters, so an overload can hide an inherited
225- * method, which only leaves the machine incomplete.
249+ * Whether the nearest type in the hierarchy of {@code type} that declares {@code method} is the one it is specified
250+ * for, which is where the verifier looks up the refinement of a call. Methods are matched by name and number of
251+ * parameters, so an overload can hide an inherited method, which only leaves the machine incomplete.
226252 */
227- private boolean inheritedFrom (RefinedFunction method , CtTypeReference <?> type ,
228- List <CtTypeReference <?>> supertypes ) {
253+ private boolean resolvesTo (RefinedFunction method , CtTypeReference <?> type , List <CtTypeReference <?>> supertypes ) {
229254 String name = Utils .getSimpleName (method .getName ());
230255 int arity = method .getArguments ().size ();
231256 for (CtTypeReference <?> t : Stream .concat (Stream .of (type ), supertypes .stream ()).toList ()) {
0 commit comments