Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce83b15bde8c24ed0a0b646a05f82eaae83c22eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*******************************************************************************
 * Copyright (c) 2016, 2017 Sven Strohschein and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * Contributors:
 *     Sven Strohschein - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.tests.util.AbstractCompilerTest;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
import org.eclipse.jdt.internal.core.INameEnvironmentWithProgress;

public class PackageBindingTest extends AbstractCompilerTest
{
	public PackageBindingTest(String name) {
		super(name);
	}

	/**
	 * This test checks if it is searched for packages before searching for types.
	 * The search for packages is much faster than searching for types, therefore it should get executed before searching for types.
	 * Commented since reverted to original behaviour as per bug 495598
	 */
	public void _test01() {
		NameEnvironmentDummy nameEnv = new NameEnvironmentDummy(true);

		PackageBinding packageBinding = new PackageBinding(new LookupEnvironment(null, new CompilerOptions(), null, nameEnv));
		Binding resultBinding = packageBinding.getTypeOrPackage("java.lang".toCharArray(), null);
		assertNotNull(resultBinding);

		assertTrue(nameEnv.isPackageSearchExecuted);
		assertFalse(nameEnv.isTypeSearchExecuted);
	}

	/**
	 * This test checks if it is searched for types when no package was found.
	 * The test {@link #_test01()} checks if the package search is executed before the type search.
	 * The search for packages is much faster than searching for types, therefore it should get executed before searching for types.
	 */
	public void test02() {
		NameEnvironmentDummy nameEnv = new NameEnvironmentDummy(false);

		LookupEnvironment environment = new LookupEnvironment(null, new CompilerOptions(), null, nameEnv);
		PackageBinding packageBinding = new PackageBinding(environment);
		Binding resultBinding = packageBinding.getTypeOrPackage("java.lang.String".toCharArray(), environment.module);
		assertNull(resultBinding); // (not implemented)

		assertTrue(nameEnv.isPackageSearchExecuted);
		assertTrue(nameEnv.isTypeSearchExecuted);
	}

	/**
	 * This test checks if {@link INameEnvironment#findType(char[], char[][])} is executed.
	 * INameEnvironment has no option to avoid the search for secondary types, therefore the search for secondary types is executed (when available).
	 */
	public void test03() {
		NameEnvironmentDummy nameEnv = new NameEnvironmentDummy(false);

		LookupEnvironment lookupEnv = new LookupEnvironment(null, new CompilerOptions(), null, nameEnv);
		PackageBinding packageBinding = lookupEnv.createPackage(new char[][]{"org/eclipse/jdt".toCharArray(), "org/eclipse/jdt/internal".toCharArray()});
		assertNotNull(packageBinding);

		assertTrue(nameEnv.isTypeSearchExecuted); //the method findType(char[], char[][]) should got executed (without an option to avoid the search for secondary types)
	}

	/**
	 * This test checks if types are searched on creating a package, but without the search for secondary types.
	 * It isn't necessary to search for secondary types when creating a package, because a package name can not collide with a secondary type.
	 * The search for secondary types should not get executed, because the search for secondary types is very expensive regarding performance
	 * (all classes of a package have to get loaded, parsed and analyzed).
	 */
	public void test04() {
		NameEnvironmentWithProgressDummy nameEnvWithProgress = new NameEnvironmentWithProgressDummy();

		LookupEnvironment lookupEnv = new LookupEnvironment(null, new CompilerOptions(), null, nameEnvWithProgress);
		PackageBinding packageBinding = lookupEnv.createPackage(new char[][]{"org/eclipse/jdt".toCharArray(), "org/eclipse/jdt/internal".toCharArray()});
		assertNotNull(packageBinding);

		assertTrue(nameEnvWithProgress.isTypeSearchExecutedWithSearchWithSecondaryTypes); //the method findType(char[], char[][], boolean) should got executed ...
		assertFalse(nameEnvWithProgress.isTypeSearchWithSearchWithSecondaryTypes); //... but without the search for secondary types
	}

	private class NameEnvironmentDummy implements INameEnvironment
	{
		private final boolean isPackage;
		boolean isPackageSearchExecuted;
		boolean isTypeSearchExecuted;

		NameEnvironmentDummy(boolean isPackage) {
			this.isPackage = isPackage;
		}

		@Override
		public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
			this.isTypeSearchExecuted = true;
			return null;
		}

		@Override
		public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
			this.isTypeSearchExecuted = true;
			return null;
		}

		@Override
		public boolean isPackage(char[][] parentPackageName, char[] packageName) {
			this.isPackageSearchExecuted = true;
			return this.isPackage;
		}

		@Override
		public void cleanup() {
		}
	}

	private class NameEnvironmentWithProgressDummy implements INameEnvironmentWithProgress
	{
		boolean isTypeSearchWithSearchWithSecondaryTypes;
		boolean isTypeSearchExecutedWithSearchWithSecondaryTypes;

		NameEnvironmentWithProgressDummy() {}

		@Override
		public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
			return null;
		}

		@Override
		public boolean isPackage(char[][] parentPackageName, char[] packageName) {
			return false;
		}

		@Override
		public void cleanup() {
		}

		@Override
		public void setMonitor(IProgressMonitor monitor) {
		}

		@Override
		public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName, boolean searchWithSecondaryTypes, char[] moduleName) {
			this.isTypeSearchExecutedWithSearchWithSecondaryTypes = true;
			this.isTypeSearchWithSearchWithSecondaryTypes = searchWithSecondaryTypes;
			return null;
		}

		@Override
		public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
			// TODO Auto-generated method stub
			return null;
		}
	}
}

Back to the top