View | Details | Raw Unified | Return to bug 238484 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java (+154 lines)
Lines 41678-41682 Link Here
41678
			"Zork cannot be resolved to a type\n" + 
41678
			"Zork cannot be resolved to a type\n" + 
41679
			"----------\n");
41679
			"----------\n");
41680
}
41680
}
41681
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=238484
41682
public void test1350() {
41683
	this.runConformTest(
41684
			new String[] {
41685
				"X.java", // =================
41686
				"import java.io.IOException;\n" + 
41687
				"\n" + 
41688
				"interface TreeVisitor<T, U> {\n" + 
41689
				"	public T visit(U location);\n" + 
41690
				"}\n" + 
41691
				"\n" + 
41692
				"interface TreeVisitable<U> {\n" + 
41693
				"	public <T> T visit(TreeVisitor<T, U> visitor) throws IOException;\n" + 
41694
				"}\n" + 
41695
				"\n" + 
41696
				"abstract class Param implements TreeVisitable<Param> {\n" + 
41697
				"	public final Param lookforParam(final String name) {\n" + 
41698
				"		TreeVisitor<Param, Param> visitor = new TreeVisitor<Param, Param>() {\n" + 
41699
				"			public Param visit(Param location) {\n" + 
41700
				"				return null;\n" + 
41701
				"			}\n" + 
41702
				"		};\n" + 
41703
				"		return visit(visitor);\n" + 
41704
				"	}\n" + 
41705
				"\n" + 
41706
				"	public abstract <T> T visit(TreeVisitor<T, Param> visitor);\n" + 
41707
				"}\n" + 
41708
				"\n" + 
41709
				"class StructParam extends Param {\n" + 
41710
				"	public <T> T visit(TreeVisitor<T, Param> visitor) {\n" + 
41711
				"		return null;\n" + 
41712
				"	}\n" + 
41713
				"}\n" + 
41714
				"\n" + 
41715
				"public class X {\n" + 
41716
				"	public static void main(String[] args) {\n" + 
41717
				"		StructParam p = new StructParam();\n" + 
41718
				"		p.lookforParam(\"abc\");\n" + 
41719
				"		System.out.println(\"done\");\n" + 
41720
				"	}\n" + 
41721
				"}\n", // =================
41722
			},
41723
			"done");
41724
}
41725
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=238484 - variation
41726
public void test1351() {
41727
	this.runConformTest(
41728
			new String[] {
41729
				"X.java", // =================
41730
				"import java.io.IOException;\n" + 
41731
				"\n" + 
41732
				"interface IFoo {\n" + 
41733
				"	<T> T foo(T t) throws IOException;\n" + 
41734
				"}\n" + 
41735
				"interface JFoo {\n" + 
41736
				"	<T> T foo(T t) throws Exception;\n" + 
41737
				"}\n" + 
41738
				"abstract class Foo implements IFoo, JFoo {}\n" + 
41739
				"\n" + 
41740
				"public class X {\n" + 
41741
				"	public static void main(String[] args) {\n" + 
41742
				"		Foo f = createFoo();\n" + 
41743
				"		try {\n" + 
41744
				"			f.foo(null);\n" + 
41745
				"		} catch(IOException e) {\n" + 
41746
				"		}\n" + 
41747
				"		System.out.println(\"done\");\n" + 
41748
				"	}\n" + 
41749
				"	static Foo createFoo() {\n" + 
41750
				"		return new Foo() {\n" + 
41751
				"			public <T> T foo(T t) {\n" + 
41752
				"				return t;\n" + 
41753
				"			}\n" + 
41754
				"		};\n" + 
41755
				"	}\n" + 
41756
				"}\n", // =================
41757
			},
41758
			"done");
41759
}
41760
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=238484 - variation
41761
public void test1352() {
41762
	this.runConformTest(
41763
			new String[] {
41764
				"X.java", // =================
41765
				"import java.io.IOException;\n" + 
41766
				"\n" + 
41767
				"interface IFoo<U> {\n" + 
41768
				"	<T> T foo(T t) throws IOException;\n" + 
41769
				"}\n" + 
41770
				"interface JFoo<U> {\n" + 
41771
				"	<T> T foo(T t) throws Exception;\n" + 
41772
				"}\n" + 
41773
				"abstract class Foo implements IFoo<String>, JFoo<String> {}\n" + 
41774
				"\n" + 
41775
				"public class X {\n" + 
41776
				"	public static void main(String[] args) {\n" + 
41777
				"		Foo f = createFoo();\n" + 
41778
				"		try {\n" + 
41779
				"			f.foo(null);\n" + 
41780
				"		} catch(IOException e) {\n" + 
41781
				"		}\n" + 
41782
				"		System.out.println(\"done\"); //dd\n" + 
41783
				"	}\n" + 
41784
				"	static Foo createFoo() {\n" + 
41785
				"		return new Foo() {\n" + 
41786
				"			public <T> T foo(T t) {\n" + 
41787
				"				return t;\n" + 
41788
				"			}\n" + 
41789
				"		};\n" + 
41790
				"	}\n" + 
41791
				"}\n", // =================
41792
			},
41793
			"done");
41794
}
41795
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=238484 - variation
41796
public void test1353() {
41797
	this.runNegativeTest(
41798
			new String[] {
41799
				"X.java", // =================
41800
				"import java.io.IOException;\n" + 
41801
				"\n" + 
41802
				"interface IFoo<U> {\n" + 
41803
				"	<T> T foo(T t) throws IOException;\n" + 
41804
				"}\n" + 
41805
				"interface JFoo<U> {\n" + 
41806
				"	<T> T foo(T t);\n" + 
41807
				"}\n" + 
41808
				"abstract class Foo implements IFoo<String>, JFoo<String> {}\n" + 
41809
				"\n" + 
41810
				"public class X {\n" + 
41811
				"	public static void main(String[] args) {\n" + 
41812
				"		Foo f = createFoo();\n" + 
41813
				"		try {\n" + 
41814
				"			f.foo(null);\n" + 
41815
				"		} catch(IOException e) {\n" + 
41816
				"		}\n" + 
41817
				"		System.out.println(\"done\"); //dd\n" + 
41818
				"	}\n" + 
41819
				"	static Foo createFoo() {\n" + 
41820
				"		return new Foo() {\n" + 
41821
				"			public <T> T foo(T t) {\n" + 
41822
				"				return t;\n" + 
41823
				"			}\n" + 
41824
				"		};\n" + 
41825
				"	}\n" + 
41826
				"}\n", // =================
41827
			},
41828
			"----------\n" + 
41829
			"1. ERROR in X.java (at line 16)\n" + 
41830
			"	} catch(IOException e) {\n" + 
41831
			"	        ^^^^^^^^^^^\n" + 
41832
			"Unreachable catch block for IOException. This exception is never thrown from the try statement body\n" + 
41833
			"----------\n");
41834
}
41681
41835
41682
}
41836
}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java (-47 / +33 lines)
Lines 3378-3385 Link Here
3378
			MethodBinding current = moreSpecific[i];
3378
			MethodBinding current = moreSpecific[i];
3379
			if (current != null) {
3379
			if (current != null) {
3380
				ReferenceBinding[] mostSpecificExceptions = null;
3380
				ReferenceBinding[] mostSpecificExceptions = null;
3381
				SimpleSet possibleMethods = null;
3382
				MethodBinding original = current.original();
3381
				MethodBinding original = current.original();
3382
				boolean shouldIntersectExceptions = original.declaringClass.isInterface() && original.thrownExceptions != Binding.NO_EXCEPTIONS; // only needed when selecting from interface methods
3383
				for (int j = 0; j < visibleSize; j++) {
3383
				for (int j = 0; j < visibleSize; j++) {
3384
					MethodBinding next = moreSpecific[j];
3384
					MethodBinding next = moreSpecific[j];
3385
					if (next == null || i == j) continue;
3385
					if (next == null || i == j) continue;
Lines 3443-3500 Link Here
3443
							// 15.12.2
3443
							// 15.12.2
3444
							continue nextSpecific; // choose original2 instead
3444
							continue nextSpecific; // choose original2 instead
3445
						}
3445
						}
3446
						if (original.thrownExceptions != original2.thrownExceptions) {
3446
						if (shouldIntersectExceptions && original2.declaringClass.isInterface()) {
3447
							if (mostSpecificExceptions == null)
3447
							if (original.thrownExceptions != original2.thrownExceptions) {
3448
								mostSpecificExceptions = original.thrownExceptions;
3448
								if (original2.thrownExceptions == Binding.NO_EXCEPTIONS) {
3449
							if (possibleMethods == null)
3449
									mostSpecificExceptions = Binding.NO_EXCEPTIONS;
3450
								possibleMethods = new SimpleSet(3);
3450
								} else {
3451
							int mostSpecificLength = mostSpecificExceptions.length;
3451
									if (mostSpecificExceptions == null) {
3452
							int original2Length = original2.thrownExceptions.length;
3452
										mostSpecificExceptions = original.thrownExceptions;
3453
							SimpleSet temp = new SimpleSet(mostSpecificLength);
3453
									}
3454
							nextException : for (int t = 0; t < mostSpecificLength; t++) {
3454
									int mostSpecificLength = mostSpecificExceptions.length;
3455
								ReferenceBinding exception = mostSpecificExceptions[t];
3455
									int original2Length = original2.thrownExceptions.length;
3456
								for (int s = 0; s < original2Length; s++) {
3456
									SimpleSet temp = new SimpleSet(mostSpecificLength);
3457
									if (exception.isCompatibleWith(original2.thrownExceptions[s])) {
3457
									boolean changed = false;
3458
										possibleMethods.add(current);
3458
									nextException : for (int t = 0; t < mostSpecificLength; t++) {
3459
										temp.add(exception);
3459
										ReferenceBinding exception = mostSpecificExceptions[t];
3460
										continue nextException;
3460
										for (int s = 0; s < original2Length; s++) {
3461
									} else if (original2.thrownExceptions[s].isCompatibleWith(exception)) {
3461
											if (exception.isCompatibleWith(original2.thrownExceptions[s])) {
3462
										possibleMethods.add(next);
3462
												temp.add(exception);
3463
										temp.add(original2.thrownExceptions[s]);
3463
												continue nextException;
3464
										continue nextException;
3464
											} else if (original2.thrownExceptions[s].isCompatibleWith(exception)) {
3465
												temp.add(original2.thrownExceptions[s]);
3466
												changed = true;
3467
												continue nextException;
3468
											} else {
3469
												changed = true;
3470
											}
3471
										}
3472
									}
3473
									if (changed) {
3474
										mostSpecificExceptions = temp.elementSize == 0 ? Binding.NO_EXCEPTIONS : new ReferenceBinding[temp.elementSize];
3475
										temp.asArray(mostSpecificExceptions);
3465
									}
3476
									}
3466
								}
3477
								}
3467
							}
3478
							}
3468
							mostSpecificExceptions = temp.elementSize == 0 ? Binding.NO_EXCEPTIONS : new ReferenceBinding[temp.elementSize];
3469
							temp.asArray(mostSpecificExceptions);
3470
						}
3479
						}
3471
					}
3480
					}
3472
				}
3481
				}
3473
				if (mostSpecificExceptions != null) {
3482
				if (mostSpecificExceptions != null && mostSpecificExceptions != current.thrownExceptions) {
3474
					Object[] values = possibleMethods.values;
3483
					return new MostSpecificExceptionMethodBinding(current, mostSpecificExceptions);
3475
					int exceptionLength = mostSpecificExceptions.length;
3476
					nextMethod : for (int p = 0, vLength = values.length; p < vLength; p++) {
3477
						MethodBinding possible = (MethodBinding) values[p];
3478
						if (possible == null) continue nextMethod;
3479
						ReferenceBinding[] itsExceptions = possible.thrownExceptions;
3480
						if (itsExceptions.length == exceptionLength) {
3481
							nextException : for (int e = 0; e < exceptionLength; e++) {
3482
								ReferenceBinding exception = itsExceptions[e];
3483
								for (int f = 0; f < exceptionLength; f++)
3484
									if (exception == mostSpecificExceptions[f]) continue nextException;
3485
								continue nextMethod;
3486
							}
3487
							return possible;
3488
						}
3489
					}
3490
					return new MethodBinding(
3491
						current.modifiers | ClassFileConstants.AccSynthetic,
3492
						current.selector,
3493
						current.returnType,
3494
						current.parameters,
3495
						mostSpecificExceptions,
3496
						current.declaringClass
3497
					);
3498
				}
3484
				}
3499
				return current;
3485
				return current;
3500
			}
3486
			}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/MostSpecificExceptionMethodBinding.java (+35 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
13
/**
14
 * Pseudo method binding used to wrapper a real method, and expose less exceptions than original.
15
 * For other protocols, it should delegate to original method
16
 */
17
public class MostSpecificExceptionMethodBinding  extends MethodBinding {
18
19
	private MethodBinding originalMethod;
20
	
21
	public MostSpecificExceptionMethodBinding (MethodBinding originalMethod, ReferenceBinding[] mostSpecificExceptions) {
22
		super(
23
				originalMethod.modifiers, 
24
				originalMethod.selector, 
25
				originalMethod.returnType, 
26
				originalMethod.parameters, 
27
				mostSpecificExceptions, 
28
				originalMethod.declaringClass);
29
		this.originalMethod = originalMethod;
30
	}
31
	
32
	public MethodBinding original() {
33
		return this.originalMethod.original();
34
	}
35
}

Return to bug 238484