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) { |