149 return Class.forName(name);
150 }
catch (ClassNotFoundException cnfe) {
154 Class<?> candidate =
null;
155 String candidateImportString =
"";
156 boolean candidateImportOnDemand =
false;
161 int idxOut = name.indexOf(
'.');
166 outerName = name.substring(0, idxOut);
167 for (String importString : imports) {
173 boolean onDemand =
false;
174 if (!importString.endsWith(
".*")) {
181 if (importString.endsWith(
"." + outerName)) {
193 String cn = importString.substring(0, importString.length() - outerName.length()) + name;
195 cl = Class.forName(cn);
196 }
catch (ClassNotFoundException cnfe) {
206 cl = Class.forName(importString.substring(0, importString.length() - 1) + name);
208 }
catch (ClassNotFoundException cnfe) {
213 if (candidate ==
null || (candidateImportOnDemand && !onDemand)) {
217 candidateImportString = importString;
218 candidateImportOnDemand = onDemand;
219 }
else if (candidate != cl)
221 "simple class name " + name +
" matches " + candidate.getName() +
" (import string "
222 + candidateImportString +
") or " + cl.getName() +
" (import string " + importString +
")");
225 if (candidate ==
null)
226 throw new ClassNotFoundException(
"Cannot find the class with name " + name);
248 if (cls.isPrimitive())
249 return cls.getName();
250 Class<?> outer = cls;
251 while (outer.getDeclaringClass() !=
null)
252 outer = outer.getDeclaringClass();
253 boolean needsFullyQualified =
true;
255 if (importString.equals(outer.getName()))
257 needsFullyQualified =
false;
258 else if (importString.endsWith(
".*")) {
260 String pack = importString.substring(0, importString.length() - 2);
262 if (pack.equals(cls.getPackage().getName()))
263 needsFullyQualified =
false;
266 if (needsFullyQualified)
267 return cls.getName();
269 String name = cls.getName();
270 String pack = cls.getPackage().getName();
271 if (!name.startsWith(pack))
272 throw new IllegalStateException(
"The class name " + name +
" does not contain the package name " + pack);
275 return name.substring(pack.length() + 1);