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 (-4 / +27 lines)
Lines 68-73 Link Here
68
import org.netbeans.api.java.source.WorkingCopy;
68
import org.netbeans.api.java.source.WorkingCopy;
69
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
69
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
70
import org.netbeans.spi.editor.hints.ChangeInfo;
70
import org.netbeans.spi.editor.hints.ChangeInfo;
71
import org.netbeans.spi.editor.hints.EnhancedFix;
71
import org.netbeans.spi.editor.hints.Fix;
72
import org.netbeans.spi.editor.hints.Fix;
72
import org.openide.filesystems.FileObject;
73
import org.openide.filesystems.FileObject;
73
import org.openide.filesystems.FileUtil;
74
import org.openide.filesystems.FileUtil;
Lines 79-89 Link Here
79
 *
80
 *
80
 * @author Jan lahoda
81
 * @author Jan lahoda
81
 */
82
 */
82
public abstract class CreateClassFix implements Fix {
83
public abstract class CreateClassFix implements EnhancedFix {
83
    
84
    
84
    protected Set<Modifier> modifiers;
85
    protected Set<Modifier> modifiers;
85
    protected List<TypeMirrorHandle> argumentTypes; //if a specific constructor should be created
86
    protected List<TypeMirrorHandle> argumentTypes; //if a specific constructor should be created
86
    protected List<String> argumentNames; //dtto.
87
    protected List<String> argumentNames; //dtto.
88
    private Integer prio;
87
    private List<TypeMirrorHandle> superTypes;
89
    private List<TypeMirrorHandle> superTypes;
88
    protected ElementKind kind;
90
    protected ElementKind kind;
89
    private int numTypeParameters;
91
    private int numTypeParameters;
Lines 243-264 Link Here
243
    
245
    
244
    public abstract String toDebugString(CompilationInfo info);
246
    public abstract String toDebugString(CompilationInfo info);
245
    
247
    
248
    /**
249
     * Will be used in {@link #getSortText()}
250
     *
251
     * @param prio
252
     */
253
    protected void setPriority(Integer prio) {
254
        this.prio = prio;
255
    }
256
257
    @Override
258
    public CharSequence getSortText() {
259
        //see usage at org.netbeans.modules.editor.hints.FixData.getSortText
260
        if (null == prio) {
261
            return getText();
262
        }
263
264
        return String.format("%04d-%s", prio, getText());
265
    }
266
    
246
    static final class CreateOuterClassFix extends CreateClassFix {
267
    static final class CreateOuterClassFix extends CreateClassFix {
247
        private FileObject targetSourceRoot;
268
        private FileObject targetSourceRoot;
248
        private String packageName;
269
        private String packageName;
270
        private String relativePath;
249
        private String simpleName;
271
        private String simpleName;
250
        
272
        
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) {
273
        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);
274
            super(info, modifiers, argumentTypes, argumentNames, superType, kind, numTypeParameters);
253
            
275
            
254
            this.targetSourceRoot = targetSourceRoot;
276
            this.targetSourceRoot = targetSourceRoot;
255
            this.packageName = packageName;
277
            this.packageName = packageName;
256
            this.simpleName = simpleName;
278
            this.simpleName = simpleName;
279
            this.relativePath = relativePath;
257
        }
280
        }
258
281
259
        public String getText() {
282
        public String getText() {
260
            if (argumentNames == null || argumentNames.isEmpty())
283
            if (argumentNames == null || argumentNames.isEmpty())
261
                return NbBundle.getMessage(CreateClassFix.class, "FIX_CreateClassInPackage", simpleName, packageName, valueForBundle(kind));
284
                return NbBundle.getMessage(CreateClassFix.class, "FIX_CreateClassInPackage", simpleName, packageName, valueForBundle(kind), relativePath);
262
            else {
285
            else {
263
                StringBuffer buf = new StringBuffer();
286
                StringBuffer buf = new StringBuffer();
264
                for (TypeMirror tm : argumentTypeMirrors) {
287
                for (TypeMirror tm : argumentTypeMirrors) {
Lines 266-272 Link Here
266
                    buf.append(",");
289
                    buf.append(",");
267
                }
290
                }
268
                String ctorParams = buf.toString();
291
                String ctorParams = buf.toString();
269
                Object[] params = new Object[] {simpleName, packageName, valueForBundle(kind), ctorParams.substring(0, ctorParams.length() - 1)};
292
                Object[] params = new Object[] {simpleName, packageName, valueForBundle(kind), ctorParams.substring(0, ctorParams.length() - 1), relativePath};
270
                return NbBundle.getMessage(CreateClassFix.class, "FIX_CreateClassAndCtorInPackage", params);
293
                return NbBundle.getMessage(CreateClassFix.class, "FIX_CreateClassAndCtorInPackage", params);
271
            }
294
            }
272
        }
295
        }
(-)java.hints/src/org/netbeans/modules/java/hints/errors/CreateElement.java (-8 / +96 lines)
Lines 60-69 Link Here
60
import java.util.Arrays;
60
import java.util.Arrays;
61
import java.util.Collections;
61
import java.util.Collections;
62
import java.util.EnumSet;
62
import java.util.EnumSet;
63
import java.util.HashMap;
63
import java.util.HashSet;
64
import java.util.HashSet;
64
import java.util.Iterator;
65
import java.util.Iterator;
65
import java.util.LinkedList;
66
import java.util.LinkedList;
66
import java.util.List;
67
import java.util.List;
68
import java.util.Map;
67
import java.util.Set;
69
import java.util.Set;
68
import java.util.logging.Level;
70
import java.util.logging.Level;
69
import java.util.logging.Logger;
71
import java.util.logging.Logger;
Lines 77-88 Link Here
77
import javax.lang.model.type.TypeKind;
79
import javax.lang.model.type.TypeKind;
78
import javax.lang.model.type.TypeMirror;
80
import javax.lang.model.type.TypeMirror;
79
import javax.lang.model.type.TypeVariable;
81
import javax.lang.model.type.TypeVariable;
80
import org.netbeans.api.java.classpath.ClassPath;
82
import org.netbeans.api.java.project.JavaProjectConstants;
81
import org.netbeans.api.java.source.ClasspathInfo.PathKind;
82
import org.netbeans.api.java.source.CompilationInfo;
83
import org.netbeans.api.java.source.CompilationInfo;
83
import org.netbeans.api.java.source.ElementHandle;
84
import org.netbeans.api.java.source.ElementHandle;
84
import org.netbeans.api.java.source.SourceUtils;
85
import org.netbeans.api.java.source.SourceUtils;
85
import org.netbeans.api.java.source.TreeUtilities;
86
import org.netbeans.api.java.source.TreeUtilities;
87
import org.netbeans.api.project.FileOwnerQuery;
88
import org.netbeans.api.project.Project;
89
import org.netbeans.api.project.SourceGroup;
90
import org.netbeans.api.project.SourceGroupModifier;
86
import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateInnerClassFix;
91
import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateInnerClassFix;
87
import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateOuterClassFix;
92
import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateOuterClassFix;
88
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
93
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
Lines 96-109 Link Here
96
import static org.netbeans.modules.java.hints.errors.CreateElementUtilities.*;
101
import static org.netbeans.modules.java.hints.errors.CreateElementUtilities.*;
97
import org.netbeans.modules.java.hints.errors.ErrorFixesFakeHint.FixKind;
102
import org.netbeans.modules.java.hints.errors.ErrorFixesFakeHint.FixKind;
98
import org.netbeans.modules.java.hints.errors.Utilities.MethodArguments;
103
import org.netbeans.modules.java.hints.errors.Utilities.MethodArguments;
104
import org.openide.filesystems.FileUtil;
99
import org.openide.util.Pair;
105
import org.openide.util.Pair;
100
106
101
/**
107
/**
102
 *
108
 *
103
 * @author Jan Lahoda
109
 * @author Jan Lahoda
110
 * @author markiewb (contributions)
104
 */
111
 */
105
public final class CreateElement implements ErrorRule<Void> {
112
public final class CreateElement implements ErrorRule<Void> {
106
    private static final Logger LOG = Logger.getLogger(CreateElement.class.getName());
113
    private static final Logger LOG = Logger.getLogger(CreateElement.class.getName());
114
    private static final int PRIO_TESTSOURCEGROUP = 500;
115
    private static final int PRIO_MAINSOURCEGROUP = 1000;
107
    
116
    
108
    /** Creates a new instance of CreateElement */
117
    /** Creates a new instance of CreateElement */
109
    public CreateElement() {
118
    public CreateElement() {
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));
527
        return Collections.<Fix>singletonList(new CreateMethodFix(info, simpleName, modifiers, target, returnType, formalArguments.parameterTypes, formalArguments.parameterNames, formalArguments.typeParameterTypes, formalArguments.typeParameterNames, targetFile));
519
    }
528
    }
520
529
530
    /**
531
     * Gets the possible sourceGroups based on the current file.
532
     * <ul>
533
     * <li>If it is a file from src/main/java it will return only
534
     * src/main/java.</li>
535
     * <li>If it is a file from src/test/java it will return src/main/java AND
536
     * src/test/java. (src/test/java will have a higher prio than src/main/java)</li>
537
     * </ul>
538
     *
539
     * @param fileObject
540
     * @return map of sourceGroup and its hint-priority
541
     */
542
    private static Map<SourceGroup, Integer> getPossibleSourceGroups(FileObject fileObject) {
543
        Boolean isInTestSources = isInTestSources(fileObject);
544
        if (null == isInTestSources) {
545
            return Collections.emptyMap();
546
        }
547
548
        Project p = FileOwnerQuery.getOwner(fileObject);
549
        if (null == p) {
550
            return Collections.emptyMap();
551
        }
552
553
        SourceGroup sourceGroup = SourceGroupModifier.createSourceGroup(p, JavaProjectConstants.SOURCES_TYPE_JAVA, JavaProjectConstants.SOURCES_HINT_MAIN);
554
        SourceGroup testSourceGroup = SourceGroupModifier.createSourceGroup(p, JavaProjectConstants.SOURCES_TYPE_JAVA, JavaProjectConstants.SOURCES_HINT_TEST);
555
556
        Map<SourceGroup, Integer> list = new HashMap<>();
557
        if (isInTestSources) {
558
            //in test sources (f.e. src/test/java) -> return main sources and test sources
559
            if (null != sourceGroup) {
560
                list.put(sourceGroup, PRIO_MAINSOURCEGROUP);
561
            }
562
563
            if (null != testSourceGroup) {
564
                //test source group has a higher prio -> before main source group
565
                list.put(testSourceGroup, PRIO_TESTSOURCEGROUP);
566
            }
567
568
        } else {
569
            //in sources (f.e. src/main/java) -> return only main sources
570
            if (null != sourceGroup) {
571
                list.put(sourceGroup, PRIO_MAINSOURCEGROUP);
572
            }
573
        }
574
        return list;
575
    }
576
577
    private static Boolean isInTestSources(FileObject fileObject) {
578
        Project p = FileOwnerQuery.getOwner(fileObject);
579
        if (null == p) {
580
            return null;
581
        }
582
583
        SourceGroup testSourceGroup = SourceGroupModifier.createSourceGroup(p, JavaProjectConstants.SOURCES_TYPE_JAVA, JavaProjectConstants.SOURCES_HINT_TEST);
584
        boolean isInTestSources = false;
585
        if (null != testSourceGroup) {
586
            isInTestSources = FileUtil.isParentOf(testSourceGroup.getRootFolder(), fileObject);
587
        }
588
        return isInTestSources;
589
    }
590
    
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) {
591
    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);
592
        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
593
Lines 528-546 Link Here
528
        if (superType != null && (superType.getKind() == TypeKind.OTHER)) {
598
        if (superType != null && (superType.getKind() == TypeKind.OTHER)) {
529
            return Collections.<Fix>emptyList();
599
            return Collections.<Fix>emptyList();
530
        }
600
        }
601
        final FileObject fileObject = info.getFileObject();
602
        Project p = FileOwnerQuery.getOwner(fileObject);
603
        if (null == p) {
604
            return Collections.emptyList();
605
        }
606
        List<Fix> fixes = new ArrayList<>();
531
607
532
        ClassPath cp = info.getClasspathInfo().getClassPath(PathKind.SOURCE);
608
        FileObject projectDirectory = p.getProjectDirectory();
533
        FileObject root = cp.findOwnerRoot(info.getFileObject());
534
609
535
        if (root == null) { //File not part of any project
610
        for (Map.Entry<SourceGroup, Integer> entrySet : getPossibleSourceGroups(fileObject).entrySet()) {
536
            return Collections.<Fix>emptyList();
611
            SourceGroup sourceGroup = entrySet.getKey();
537
        }
612
            Integer value = entrySet.getValue();
538
613
614
            final FileObject sourceGroupRoot = sourceGroup.getRootFolder();
615
            String relativePath = FileUtil.getRelativePath(projectDirectory, sourceGroupRoot);
616
617
            if (null != relativePath) {
539
        PackageElement packageElement = (PackageElement) (source instanceof PackageElement ? source : info.getElementUtilities().outermostTypeElement(source).getEnclosingElement());
618
        PackageElement packageElement = (PackageElement) (source instanceof PackageElement ? source : info.getElementUtilities().outermostTypeElement(source).getEnclosingElement());
540
619
541
        return Collections.<Fix>singletonList(new CreateOuterClassFix(info, root, packageElement.getQualifiedName().toString(), simpleName, modifiers, formalArguments.first(), formalArguments.second(), superType, kind, numTypeParameters));
620
                final CreateOuterClassFix fix = new CreateOuterClassFix(info, sourceGroupRoot, packageElement.getQualifiedName().toString(), simpleName, modifiers, formalArguments.first(), formalArguments.second(), superType, kind, numTypeParameters, relativePath);
621
                fix.setPriority(value);
622
                
623
                fixes.add(fix);
542
    }
624
    }
543
625
626
        }
627
628
629
        return fixes;
630
    }
631
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) {
632
    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) {
545
        Pair<List<? extends TypeMirror>, List<String>> formalArguments = invocation != null ? Utilities.resolveArguments(info, invocation, realArguments, target) : Pair.<List<? extends TypeMirror>, List<String>>of(null, null);
633
        Pair<List<? extends TypeMirror>, List<String>> formalArguments = invocation != null ? Utilities.resolveArguments(info, invocation, realArguments, target) : Pair.<List<? extends TypeMirror>, List<String>>of(null, null);
546
634

Return to bug 250809