diff -r f51867aa225d java.source.base/src/org/netbeans/api/java/source/ElementHandle.java --- a/java.source.base/src/org/netbeans/api/java/source/ElementHandle.java Tue May 10 13:35:02 2016 +0300 +++ b/java.source.base/src/org/netbeans/api/java/source/ElementHandle.java Wed May 18 16:36:24 2016 +0300 @@ -57,6 +57,7 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.NestingKind; import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeParameterElement; @@ -113,13 +114,15 @@ } private final ElementKind kind; + private final NestingKind nestingKind; private final String[] signatures; - private ElementHandle(final ElementKind kind, String... signatures) { + private ElementHandle(final ElementKind kind, NestingKind netsingKind, String... signatures) { assert kind != null; assert signatures != null; this.kind = kind; + this.nestingKind = netsingKind; this.signatures = signatures; } @@ -380,6 +383,17 @@ public @NonNull ElementKind getKind () { return this.kind; } + + /** + * Returns the {@link NestingKind} of this element handle, + * it is the nesting kind of the {@link TypeElement} from which the handle + * was created. + * @return {@link NestingKind} or null + * + */ + public NestingKind getNestingKind() { + return nestingKind; + } private static final WeakSet> NORMALIZATION_CACHE = new WeakSet>(); @@ -410,7 +424,7 @@ public static ElementHandle createPackageElementHandle ( @NonNull final String packageName) { Parameters.notNull("packageName", packageName); //NOI18N - return new ElementHandle(ElementKind.PACKAGE, packageName); + return new ElementHandle(ElementKind.PACKAGE, null, packageName); } /** @@ -432,12 +446,13 @@ if (!kind.isClass() && !kind.isInterface()) { throw new IllegalArgumentException(kind.toString()); } - return new ElementHandle(kind, binaryName); + return new ElementHandle(kind, null, binaryName); } private static @NonNull ElementHandle createImpl (@NonNull final T element) throws IllegalArgumentException { Parameters.notNull("element", element); ElementKind kind = element.getKind(); + NestingKind nestingKind = null; String[] signatures; switch (kind) { case PACKAGE: @@ -449,6 +464,7 @@ case ENUM: case ANNOTATION_TYPE: assert element instanceof TypeElement; + nestingKind = ((TypeElement) element).getNestingKind(); signatures = new String[] {ClassFileUtil.encodeClassNameOrArray((TypeElement)element)}; break; case METHOD: @@ -456,11 +472,13 @@ case INSTANCE_INIT: case STATIC_INIT: assert element instanceof ExecutableElement; + nestingKind = getEnclosingElementNestingKind(element); signatures = ClassFileUtil.createExecutableDescriptor((ExecutableElement)element); break; case FIELD: case ENUM_CONSTANT: assert element instanceof VariableElement; + nestingKind = getEnclosingElementNestingKind(element); signatures = ClassFileUtil.createFieldDescriptor((VariableElement)element); break; case TYPE_PARAMETER: @@ -470,12 +488,14 @@ ElementKind gek = ge.getKind(); if (gek.isClass() || gek.isInterface()) { assert ge instanceof TypeElement; + nestingKind = ((TypeElement) ge).getNestingKind(); signatures = new String[2]; signatures[0] = ClassFileUtil.encodeClassNameOrArray((TypeElement)ge); signatures[1] = tpe.getSimpleName().toString(); } else if (gek == ElementKind.METHOD || gek == ElementKind.CONSTRUCTOR) { assert ge instanceof ExecutableElement; + nestingKind = getEnclosingElementNestingKind(ge); String[] _sigs = ClassFileUtil.createExecutableDescriptor((ExecutableElement)ge); signatures = new String[_sigs.length + 1]; System.arraycopy(_sigs, 0, signatures, 0, _sigs.length); @@ -488,7 +508,14 @@ default: throw new IllegalArgumentException(kind.toString()); } - return new ElementHandle (kind, signatures); + return new ElementHandle (kind, nestingKind, signatures); + } + + private static NestingKind getEnclosingElementNestingKind(Element element) { + if (element.getEnclosingElement() instanceof TypeElement) { + return ((TypeElement) element.getEnclosingElement()).getNestingKind(); + } + return null; } /** @@ -563,7 +590,7 @@ if (descriptors.length != 1) { throw new IllegalArgumentException (); } - return new ElementHandle (kind, descriptors); + return new ElementHandle (kind, null, descriptors); case CLASS: case INTERFACE: case ENUM: @@ -572,25 +599,25 @@ if (descriptors.length != 1) { throw new IllegalArgumentException (); } - return new ElementHandle (kind, descriptors); + return new ElementHandle (kind, null, descriptors); case METHOD: case CONSTRUCTOR: if (descriptors.length != 3) { throw new IllegalArgumentException (); } - return new ElementHandle (kind, descriptors); + return new ElementHandle (kind, null, descriptors); case INSTANCE_INIT: case STATIC_INIT: if (descriptors.length != 2) { throw new IllegalArgumentException (); } - return new ElementHandle (kind, descriptors); + return new ElementHandle (kind, null, descriptors); case FIELD: case ENUM_CONSTANT: if (descriptors.length != 3) { throw new IllegalArgumentException (); } - return new ElementHandle (kind, descriptors); + return new ElementHandle (kind, null, descriptors); default: throw new IllegalArgumentException (); } diff -r f51867aa225d java.source.base/src/org/netbeans/api/java/source/SourceUtils.java --- a/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java Tue May 10 13:35:02 2016 +0300 +++ b/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java Wed May 18 16:36:24 2016 +0300 @@ -529,7 +529,7 @@ return folders.isEmpty() ? pair.first() : folders.get(0); } else { final boolean caseSensitive = isCaseSensitive (); - final Object fnames = getSourceFileNames (className); + final Object fnames = getSourceFileNames (className, handle.getNestingKind()); folders.addFirst(pair.first()); if (fnames instanceof String) { FileObject match = findMatchingChild((String)fnames, folders, caseSensitive); @@ -1038,12 +1038,15 @@ * a String (top-level class, no $) or List<String> as the JLS permits $ in * class names. */ - private static Object getSourceFileNames (String classFileName) { + private static Object getSourceFileNames (String classFileName, NestingKind nestingKind) { int max = classFileName.length() - 1; int index = classFileName.indexOf('$'); if (index == -1) { return classFileName; } + if (nestingKind == NestingKind.TOP_LEVEL) { + return classFileName; + } List ll = new ArrayList<>(3); do { ll.add(classFileName.substring(0, index));