This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 250809
Collapse All | Expand All

(-)java.hints/src/org/netbeans/modules/java/hints/errors/Bundle.properties (-2 / +2 lines)
Lines 105-112 Link Here
105
#     1: interface
105
#     1: interface
106
#     2: enum
106
#     2: enum
107
#     3: annotation type
107
#     3: annotation type
108
FIX_CreateClassInPackage=Create {2,choice,0#class|1#interface|2#enum|3#annotation type} "{0}" in package {1}
108
FIX_CreateClassInPackage=Create {2,choice,0#class|1#interface|2#enum|3#annotation type} "{0}" in package {1} ({3})
109
FIX_CreateClassAndCtorInPackage=Create {2,choice,0#class|1#interface|2#enum|3#annotation type} "{0}" with constructor "{0}({3})" in package {1}
109
FIX_CreateClassAndCtorInPackage=Create {2,choice,0#class|1#interface|2#enum|3#annotation type} "{0}" with constructor "{0}({3})" in package {1} ({4})
110
110
111
#{0}: new class simple name
111
#{0}: new class simple name
112
#{1}: target class name
112
#{1}: target class name
(-)java.hints/src/org/netbeans/modules/java/hints/errors/CreateClassFix.java (-3 / +5 lines)
Lines 246-264 Link Here
246
    static final class CreateOuterClassFix extends CreateClassFix {
246
    static final class CreateOuterClassFix extends CreateClassFix {
247
        private FileObject targetSourceRoot;
247
        private FileObject targetSourceRoot;
248
        private String packageName;
248
        private String packageName;
249
        private String relativePath;
249
        private String simpleName;
250
        private String simpleName;
250
        
251
        
251
        public CreateOuterClassFix(CompilationInfo info, FileObject targetSourceRoot, String packageName, String simpleName, Set<Modifier> modifiers, List<? extends TypeMirror> argumentTypes, List<String> argumentNames, TypeMirror superType, ElementKind kind, int numTypeParameters) {
252
        public CreateOuterClassFix(CompilationInfo info, FileObject targetSourceRoot, String packageName, String simpleName, Set<Modifier> modifiers, List<? extends TypeMirror> argumentTypes, List<String> argumentNames, TypeMirror superType, ElementKind kind, int numTypeParameters, String relativePath) {
252
            super(info, modifiers, argumentTypes, argumentNames, superType, kind, numTypeParameters);
253
            super(info, modifiers, argumentTypes, argumentNames, superType, kind, numTypeParameters);
253
            
254
            
254
            this.targetSourceRoot = targetSourceRoot;
255
            this.targetSourceRoot = targetSourceRoot;
255
            this.packageName = packageName;
256
            this.packageName = packageName;
256
            this.simpleName = simpleName;
257
            this.simpleName = simpleName;
258
            this.relativePath = relativePath;
257
        }
259
        }
258
260
259
        public String getText() {
261
        public String getText() {
260
            if (argumentNames == null || argumentNames.isEmpty())
262
            if (argumentNames == null || argumentNames.isEmpty())
261
                return NbBundle.getMessage(CreateClassFix.class, "FIX_CreateClassInPackage", simpleName, packageName, valueForBundle(kind));
263
                return NbBundle.getMessage(CreateClassFix.class, "FIX_CreateClassInPackage", simpleName, packageName, valueForBundle(kind), relativePath);
262
            else {
264
            else {
263
                StringBuffer buf = new StringBuffer();
265
                StringBuffer buf = new StringBuffer();
264
                for (TypeMirror tm : argumentTypeMirrors) {
266
                for (TypeMirror tm : argumentTypeMirrors) {
Lines 266-272 Link Here
266
                    buf.append(",");
268
                    buf.append(",");
267
                }
269
                }
268
                String ctorParams = buf.toString();
270
                String ctorParams = buf.toString();
269
                Object[] params = new Object[] {simpleName, packageName, valueForBundle(kind), ctorParams.substring(0, ctorParams.length() - 1)};
271
                Object[] params = new Object[] {simpleName, packageName, valueForBundle(kind), ctorParams.substring(0, ctorParams.length() - 1), relativePath};
270
                return NbBundle.getMessage(CreateClassFix.class, "FIX_CreateClassAndCtorInPackage", params);
272
                return NbBundle.getMessage(CreateClassFix.class, "FIX_CreateClassAndCtorInPackage", params);
271
            }
273
            }
272
        }
274
        }
(-)java.hints/src/org/netbeans/modules/java/hints/errors/CreateElement.java (-8 / +66 lines)
Lines 77-88 Link Here
77
import javax.lang.model.type.TypeKind;
77
import javax.lang.model.type.TypeKind;
78
import javax.lang.model.type.TypeMirror;
78
import javax.lang.model.type.TypeMirror;
79
import javax.lang.model.type.TypeVariable;
79
import javax.lang.model.type.TypeVariable;
80
import org.netbeans.api.java.classpath.ClassPath;
80
import org.netbeans.api.java.project.JavaProjectConstants;
81
import org.netbeans.api.java.source.ClasspathInfo.PathKind;
82
import org.netbeans.api.java.source.CompilationInfo;
81
import org.netbeans.api.java.source.CompilationInfo;
83
import org.netbeans.api.java.source.ElementHandle;
82
import org.netbeans.api.java.source.ElementHandle;
84
import org.netbeans.api.java.source.SourceUtils;
83
import org.netbeans.api.java.source.SourceUtils;
85
import org.netbeans.api.java.source.TreeUtilities;
84
import org.netbeans.api.java.source.TreeUtilities;
85
import org.netbeans.api.project.FileOwnerQuery;
86
import org.netbeans.api.project.Project;
87
import org.netbeans.api.project.SourceGroup;
88
import org.netbeans.api.project.SourceGroupModifier;
86
import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateInnerClassFix;
89
import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateInnerClassFix;
87
import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateOuterClassFix;
90
import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateOuterClassFix;
88
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
91
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
Lines 96-101 Link Here
96
import static org.netbeans.modules.java.hints.errors.CreateElementUtilities.*;
99
import static org.netbeans.modules.java.hints.errors.CreateElementUtilities.*;
97
import org.netbeans.modules.java.hints.errors.ErrorFixesFakeHint.FixKind;
100
import org.netbeans.modules.java.hints.errors.ErrorFixesFakeHint.FixKind;
98
import org.netbeans.modules.java.hints.errors.Utilities.MethodArguments;
101
import org.netbeans.modules.java.hints.errors.Utilities.MethodArguments;
102
import org.openide.filesystems.FileUtil;
99
import org.openide.util.Pair;
103
import org.openide.util.Pair;
100
104
101
/**
105
/**
Lines 518-523 Link Here
518
        return Collections.<Fix>singletonList(new CreateMethodFix(info, simpleName, modifiers, target, returnType, formalArguments.parameterTypes, formalArguments.parameterNames, formalArguments.typeParameterTypes, formalArguments.typeParameterNames, targetFile));
522
        return Collections.<Fix>singletonList(new CreateMethodFix(info, simpleName, modifiers, target, returnType, formalArguments.parameterTypes, formalArguments.parameterNames, formalArguments.typeParameterTypes, formalArguments.typeParameterNames, targetFile));
519
    }
523
    }
520
524
525
    /**
526
     * Gets the possible sourceGroups based on the current file.
527
     * <ul>
528
     * <li>If it is a file from src/main/java it will return only
529
     * src/main/java.</li>
530
     * <li>If it is a file from src/test/java it will return src/main/java AND
531
     * src/test/java.</li>
532
     * </ul>
533
     *
534
     * @param fileObject
535
     * @return
536
     */
537
    private static List<SourceGroup> getPossibleSourceGroups(FileObject fileObject) {
538
        Project p = FileOwnerQuery.getOwner(fileObject);
539
        if (null == p) {
540
            return Collections.emptyList();
541
        }
542
543
        SourceGroup sourceGroup = SourceGroupModifier.createSourceGroup(p, JavaProjectConstants.SOURCES_TYPE_JAVA, JavaProjectConstants.SOURCES_HINT_MAIN);
544
        SourceGroup testSourceGroup = SourceGroupModifier.createSourceGroup(p, JavaProjectConstants.SOURCES_TYPE_JAVA, JavaProjectConstants.SOURCES_HINT_TEST);
545
546
        boolean isInTestSources = false;
547
        if (null != testSourceGroup) {
548
            isInTestSources = FileUtil.isParentOf(testSourceGroup.getRootFolder(), fileObject);
549
        }
550
551
        List<SourceGroup> list = new ArrayList<>();
552
        if (isInTestSources) {
553
            //in test sources (f.e. src/test/java) -> return main sources and test sources
554
            if (null != sourceGroup) {
555
                list.add(sourceGroup);
556
            }
557
558
            if (null != testSourceGroup) {
559
                list.add(testSourceGroup);
560
            }
561
562
        } else {
563
            //in sources (f.e. src/main/java) -> return only main sources
564
            if (null != sourceGroup) {
565
                list.add(sourceGroup);
566
            }
567
        }
568
        return list;
569
    }
521
    private static List<Fix> prepareCreateOuterClassFix(CompilationInfo info, TreePath invocation, Element source, Set<Modifier> modifiers, String simpleName, List<? extends ExpressionTree> realArguments, TypeMirror superType, ElementKind kind, int numTypeParameters) {
570
    private static List<Fix> prepareCreateOuterClassFix(CompilationInfo info, TreePath invocation, Element source, Set<Modifier> modifiers, String simpleName, List<? extends ExpressionTree> realArguments, TypeMirror superType, ElementKind kind, int numTypeParameters) {
522
        Pair<List<? extends TypeMirror>, List<String>> formalArguments = invocation != null ? Utilities.resolveArguments(info, invocation, realArguments, null) : Pair.<List<? extends TypeMirror>, List<String>>of(null, null);
571
        Pair<List<? extends TypeMirror>, List<String>> formalArguments = invocation != null ? Utilities.resolveArguments(info, invocation, realArguments, null) : Pair.<List<? extends TypeMirror>, List<String>>of(null, null);
523
572
Lines 528-544 Link Here
528
        if (superType != null && (superType.getKind() == TypeKind.OTHER)) {
577
        if (superType != null && (superType.getKind() == TypeKind.OTHER)) {
529
            return Collections.<Fix>emptyList();
578
            return Collections.<Fix>emptyList();
530
        }
579
        }
580
        final FileObject fileObject = info.getFileObject();
581
        Project p = FileOwnerQuery.getOwner(fileObject);
582
        if (null == p) {
583
            return Collections.emptyList();
584
        }
585
        List<Fix> fixes = new ArrayList<>();
531
586
532
        ClassPath cp = info.getClasspathInfo().getClassPath(PathKind.SOURCE);
587
        FileObject projectDirectory = p.getProjectDirectory();
533
        FileObject root = cp.findOwnerRoot(info.getFileObject());
534
588
535
        if (root == null) { //File not part of any project
589
        for (SourceGroup sourceGroup : getPossibleSourceGroups(fileObject)) {
536
            return Collections.<Fix>emptyList();
590
            final FileObject sourceGroupRoot = sourceGroup.getRootFolder();
537
        }
591
            String relativePath = FileUtil.getRelativePath(projectDirectory, sourceGroupRoot);
538
592
593
            if (null != relativePath) {
539
        PackageElement packageElement = (PackageElement) (source instanceof PackageElement ? source : info.getElementUtilities().outermostTypeElement(source).getEnclosingElement());
594
        PackageElement packageElement = (PackageElement) (source instanceof PackageElement ? source : info.getElementUtilities().outermostTypeElement(source).getEnclosingElement());
595
                fixes.add(new CreateOuterClassFix(info, sourceGroupRoot, packageElement.getQualifiedName().toString(), simpleName, modifiers, formalArguments.first(), formalArguments.second(), superType, kind, numTypeParameters, relativePath));
596
            }
597
        }
540
598
541
        return Collections.<Fix>singletonList(new CreateOuterClassFix(info, root, packageElement.getQualifiedName().toString(), simpleName, modifiers, formalArguments.first(), formalArguments.second(), superType, kind, numTypeParameters));
599
        return fixes;
542
    }
600
    }
543
601
544
    private static List<Fix> prepareCreateInnerClassFix(CompilationInfo info, TreePath invocation, TypeElement target, Set<Modifier> modifiers, String simpleName, List<? extends ExpressionTree> realArguments, TypeMirror superType, ElementKind kind, int numTypeParameters) {
602
    private static List<Fix> prepareCreateInnerClassFix(CompilationInfo info, TreePath invocation, TypeElement target, Set<Modifier> modifiers, String simpleName, List<? extends ExpressionTree> realArguments, TypeMirror superType, ElementKind kind, int numTypeParameters) {

Return to bug 250809