1818import de .uka .ilkd .key .logic .op .IProgramMethod ;
1919import de .uka .ilkd .key .logic .op .ProgramMethod ;
2020import de .uka .ilkd .key .logic .op .ProgramVariable ;
21+ import de .uka .ilkd .key .parser .Location ;
22+ import de .uka .ilkd .key .util .parsing .LocatableException ;
2123
2224import org .key_project .logic .SyntaxElement ;
2325import org .key_project .util .collection .IdentityHashSet ;
@@ -177,7 +179,8 @@ private void validateNew(New _new) {
177179 // This also disallows things like "a.new B()" which would not like this. However,
178180 // KeY cannot deal with this anyway, so we can do the easy check here.
179181 throw new FinalViolationException (
180- "Call to non-static inner class " + classType + " leaks 'this' to the constructor" ,
182+ "Constructor call to non-static inner class " + classType .getFullName () +
183+ " leaks 'this' to the constructor" ,
181184 _new );
182185 }
183186
@@ -204,12 +207,14 @@ private void validateMethodReference(MethodReference methodReference) {
204207
205208 if (hasThisArgument ) {
206209 throw new FinalViolationException (
207- "Method call " + methodReference + " leaks 'this' to called method." ,
210+ "Method call to " + methodReference . getName () + " leaks 'this' to called method." ,
208211 methodReference );
209212 }
210213
211214 if (calledOnThis ) {
212215 IProgramMethod method = findMethod (methodReference );
216+ assert !method .isConstructor ()
217+ : "Constructor calls should have been handled by the New handler above." ;
213218 if (method .isStatic () || method .isConstructor ()) {
214219 // local static methods are acutally fine ...
215220 // constructor calls are also fine
@@ -219,7 +224,9 @@ private void validateMethodReference(MethodReference methodReference) {
219224 if (!method .isFinal () && !method .isPrivate ()
220225 && !((ClassType ) enclosingClass .getJavaType ()).isFinal ()) {
221226 throw new FinalViolationException (
222- "Method called on 'this' that is not effectively final." , methodReference );
227+ "Method to " + method .getFullName () +
228+ " called on 'this' that is not effectively final." ,
229+ methodReference );
223230 }
224231 validate (method );
225232 }
@@ -266,25 +273,24 @@ private void validateFieldReference(FieldReference fieldReference) {
266273 validateChildren (fieldReference );
267274 }
268275
269- static class FinalViolationException extends RuntimeException {
270-
271- private final PositionInfo position ;
276+ static class FinalViolationException extends LocatableException {
272277
273278 public FinalViolationException (String message ) {
274279 this (message , null );
275280 }
276281
277282 public FinalViolationException (String message , SyntaxElement syntaxElement ) {
278- super (message );
279- if (syntaxElement instanceof SourceElement sourceElement ) {
280- this .position = sourceElement .getPositionInfo ();
281- } else {
282- this .position = null ;
283- }
283+ super (message , computeLocation (syntaxElement ));
284284 }
285285
286- public PositionInfo getPositionInfo () {
287- return position ;
286+ private static Location computeLocation (SyntaxElement syntaxElement ) {
287+ if (syntaxElement instanceof SourceElement sourceElement ) {
288+ PositionInfo posInfo = sourceElement .getPositionInfo ();
289+ var uri = posInfo .getURI ().orElse (null );
290+ var pos = posInfo .getStartPosition ();
291+ return new Location (uri , pos );
292+ }
293+ return Location .UNDEFINED ;
288294 }
289295 }
290296}
0 commit comments