public SNode resolveClass(PsiClassType t) { PsiClass cls = t.resolve(); // TODO q: handle this case? create dynamic reference? if (cls == null) { return null; } System.out.println("Class resolved: " + cls.getQualifiedName()); PsiElement e = cls; do { e = e.getParent(); if (!(e instanceof PsiClass) && !(e instanceof PsiJavaFile)) { return null; } } while (!(e instanceof PsiJavaFile)); PsiJavaFile file = (PsiJavaFile) e; String packageName = file.getPackageName(); SModelReference modelRef = SModelReference.fromString(packageName); SNode clsType = SConceptOperations.createNewNode( "jetbrains.mps.baseLanguage.structure.ClassifierType", null); clsType.setReference( "classifier", new DynamicReference("classifier", clsType, modelRef, t.getClassName())); System.out.println("Class type: " + t.getClassName()); return clsType; }
private static void collectNamesToImport( @NotNull final Set<String> names, @NotNull final PsiJavaFile file, @NotNull final Set<String> namesToImportStaticly, PsiFile context) { String packageName = file.getPackageName(); final PsiElement[] roots = file.getPsiRoots(); for (PsiElement root : roots) { addNamesToImport(names, root, packageName, namesToImportStaticly, context); } }
@NotNull private File fileToFile(PsiJavaFile javaFile, List<String> additionalImports) { PsiImportList importList = javaFile.getImportList(); List<Import> imports = importList == null ? Collections.<Import>emptyList() : importsToImportList(importList.getAllImportStatements()); for (String i : additionalImports) imports.add(new Import(i)); return new File( quoteKeywords(javaFile.getPackageName()), imports, classesToClassList(javaFile.getClasses()), createMainFunction(javaFile)); }
public static boolean hasDefaultImportConflict(String fqName, PsiJavaFile file) { final String shortName = ClassUtil.extractClassName(fqName); final String packageName = ClassUtil.extractPackageName(fqName); final String filePackageName = file.getPackageName(); if (filePackageName.equals(packageName)) { return false; } final Project project = file.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiPackage filePackage = psiFacade.findPackage(filePackageName); if (filePackage == null) { return false; } return filePackage.containsClassNamed(shortName); }
public static void addImportIfNeeded(@NotNull PsiClass aClass, @NotNull PsiElement context) { final PsiFile file = context.getContainingFile(); if (!(file instanceof PsiJavaFile)) { return; } final PsiJavaFile javaFile = (PsiJavaFile) file; final PsiClass outerClass = aClass.getContainingClass(); if (outerClass == null) { if (PsiTreeUtil.isAncestor(javaFile, aClass, true)) { return; } } else if (PsiTreeUtil.isAncestor(outerClass, context, true)) { final PsiElement brace = outerClass.getLBrace(); if (brace != null && brace.getTextOffset() < context.getTextOffset()) { return; } } final String qualifiedName = aClass.getQualifiedName(); if (qualifiedName == null) { return; } final PsiImportList importList = javaFile.getImportList(); if (importList == null) { return; } final String containingPackageName = javaFile.getPackageName(); @NonNls final String packageName = ClassUtil.extractPackageName(qualifiedName); if (containingPackageName.equals(packageName) || importList.findSingleClassImportStatement(qualifiedName) != null) { return; } if (importList.findOnDemandImportStatement(packageName) != null && !hasDefaultImportConflict(qualifiedName, javaFile) && !hasOnDemandImportConflict(qualifiedName, javaFile)) { return; } final Project project = importList.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiElementFactory elementFactory = psiFacade.getElementFactory(); final PsiImportStatement importStatement = elementFactory.createImportStatement(aClass); importList.add(importStatement); }
public static boolean hasDefaultImportConflict(String fqName, PsiJavaFile file) { final String shortName = ClassUtil.extractClassName(fqName); final String packageName = ClassUtil.extractPackageName(fqName); final String filePackageName = file.getPackageName(); if (filePackageName.equals(packageName)) { return false; } final Project project = file.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiPackage filePackage = psiFacade.findPackage(filePackageName); if (filePackage == null) { return false; } final PsiClass[] classes = filePackage.getClasses(); for (PsiClass aClass : classes) { final String className = aClass.getName(); if (shortName.equals(className)) { return true; } } return false; }
protected void initialize() { myDefaultConstructor = null; final PsiClass psiClass = getElement(); LOG.assertTrue(psiClass != null); PsiElement psiParent = psiClass.getParent(); if (psiParent instanceof PsiFile) { if (isSyntheticJSP()) { final RefFileImpl refFile = (RefFileImpl) getRefManager().getReference(JspPsiUtil.getJspFile(psiClass)); LOG.assertTrue(refFile != null); refFile.add(this); } else if (psiParent instanceof PsiJavaFile) { PsiJavaFile psiFile = (PsiJavaFile) psiParent; String packageName = psiFile.getPackageName(); if (!"".equals(packageName)) { ((RefPackageImpl) getRefJavaManager().getPackage(packageName)).add(this); } else { ((RefPackageImpl) getRefJavaManager().getDefaultPackage()).add(this); } } final Module module = ModuleUtil.findModuleForPsiElement(psiClass); LOG.assertTrue(module != null); final RefModuleImpl refModule = ((RefModuleImpl) getRefManager().getRefModule(module)); LOG.assertTrue(refModule != null); refModule.add(this); } else { while (!(psiParent instanceof PsiClass || psiParent instanceof PsiMethod || psiParent instanceof PsiField)) { psiParent = psiParent.getParent(); } RefElement refParent = getRefManager().getReference(psiParent); LOG.assertTrue(refParent != null); ((RefElementImpl) refParent).add(this); } setAbstract(psiClass.hasModifierProperty(PsiModifier.ABSTRACT)); setAnonymous(psiClass instanceof PsiAnonymousClass); setIsLocal(!(isAnonymous() || psiParent instanceof PsiClass || psiParent instanceof PsiFile)); setInterface(psiClass.isInterface()); initializeSuperReferences(psiClass); PsiMethod[] psiMethods = psiClass.getMethods(); PsiField[] psiFields = psiClass.getFields(); setUtilityClass(psiMethods.length > 0 || psiFields.length > 0); for (PsiField psiField : psiFields) { getRefManager().getReference(psiField); } if (!isApplet()) { final PsiClass servlet = getRefJavaManager().getServlet(); setServlet(servlet != null && psiClass.isInheritor(servlet, true)); } if (!isApplet() && !isServlet()) { setTestCase(TestUtil.isTestClass(psiClass)); for (RefClass refBase : getBaseClasses()) { ((RefClassImpl) refBase).setTestCase(true); } } for (PsiMethod psiMethod : psiMethods) { RefMethod refMethod = (RefMethod) getRefManager().getReference(psiMethod); if (refMethod != null) { if (psiMethod.isConstructor()) { if (psiMethod.getParameterList().getParametersCount() > 0 || !psiMethod.hasModifierProperty(PsiModifier.PRIVATE)) { setUtilityClass(false); } addConstructor(refMethod); if (psiMethod.getParameterList().getParametersCount() == 0) { setDefaultConstructor((RefMethodImpl) refMethod); } } else { if (!psiMethod.hasModifierProperty(PsiModifier.STATIC)) { setUtilityClass(false); } } } } if (getConstructors().size() == 0 && !isInterface() && !isAnonymous()) { RefImplicitConstructorImpl refImplicitConstructor = new RefImplicitConstructorImpl(this); setDefaultConstructor(refImplicitConstructor); addConstructor(refImplicitConstructor); } if (isInterface()) { for (int i = 0; i < psiFields.length && isUtilityClass(); i++) { PsiField psiField = psiFields[i]; if (!psiField.hasModifierProperty(PsiModifier.STATIC)) { setUtilityClass(false); } } } final PsiClass applet = getRefJavaManager().getApplet(); setApplet(applet != null && psiClass.isInheritor(applet, true)); getRefManager().fireNodeInitialized(this); }
@NotNull private static Set<String> findSingleImports( @NotNull final PsiJavaFile file, @NotNull String[] names, @NotNull final Set<String> onDemandImports, @NotNull Set<String> namesToImportStaticly) { final GlobalSearchScope resolveScope = file.getResolveScope(); Set<String> namesToUseSingle = new THashSet<String>(); final String thisPackageName = file.getPackageName(); final Set<String> implicitlyImportedPackages = new THashSet<String>(Arrays.asList(file.getImplicitlyImportedPackages())); final PsiManager manager = file.getManager(); for (String name : names) { String prefix = getPackageOrClassName(name); if (prefix.length() == 0) continue; final boolean isImplicitlyImported = implicitlyImportedPackages.contains(prefix); if (!onDemandImports.contains(prefix) && !isImplicitlyImported) continue; String shortName = PsiNameHelper.getShortClassName(name); String thisPackageClass = thisPackageName.length() > 0 ? thisPackageName + "." + shortName : shortName; if (JavaPsiFacade.getInstance(manager.getProject()).findClass(thisPackageClass, resolveScope) != null) { namesToUseSingle.add(name); continue; } if (!isImplicitlyImported) { String langPackageClass = JAVA_LANG_PACKAGE + "." + shortName; // TODO : JSP! if (JavaPsiFacade.getInstance(manager.getProject()) .findClass(langPackageClass, resolveScope) != null) { namesToUseSingle.add(name); continue; } } for (String onDemandName : onDemandImports) { if (prefix.equals(onDemandName)) continue; if (namesToImportStaticly.contains(name)) { PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(onDemandName, resolveScope); if (aClass != null) { PsiField field = aClass.findFieldByName(shortName, true); if (field != null && field.hasModifierProperty(PsiModifier.STATIC)) { namesToUseSingle.add(name); } else { PsiClass inner = aClass.findInnerClassByName(shortName, true); if (inner != null && inner.hasModifierProperty(PsiModifier.STATIC)) { namesToUseSingle.add(name); } else { PsiMethod[] methods = aClass.findMethodsByName(shortName, true); for (PsiMethod method : methods) { if (method.hasModifierProperty(PsiModifier.STATIC)) { namesToUseSingle.add(name); } } } } } } else { PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()) .findClass(onDemandName + "." + shortName, resolveScope); if (aClass != null) { namesToUseSingle.add(name); } } } } return namesToUseSingle; }
@Override @Nullable public List<PsiFile> findExternalAnnotationsFiles(@NotNull PsiModifierListOwner listOwner) { final PsiFile containingFile = listOwner.getContainingFile(); if (!(containingFile instanceof PsiJavaFile)) { return null; } final PsiJavaFile javaFile = (PsiJavaFile) containingFile; final String packageName = javaFile.getPackageName(); final VirtualFile virtualFile = containingFile.getVirtualFile(); if (virtualFile == null) return null; final List<PsiFile> files = myExternalAnnotations.get(virtualFile); if (files == NULL_LIST) return null; if (files != null) { boolean allValid = true; for (PsiFile file : files) { allValid &= file.isValid(); } if (allValid) { return files; } } if (virtualFile == null) { return null; } Set<PsiFile> possibleAnnotationsXmls = new THashSet<PsiFile>(); for (VirtualFile root : getExternalAnnotationsRoots(virtualFile)) { final VirtualFile ext = root.findFileByRelativePath(packageName.replace('.', '/') + "/" + ANNOTATIONS_XML); if (ext == null) continue; final PsiFile psiFile = myPsiManager.findFile(ext); if (psiFile == null) continue; possibleAnnotationsXmls.add(psiFile); } List<PsiFile> result; if (possibleAnnotationsXmls.isEmpty()) { myExternalAnnotations.put(virtualFile, NULL_LIST); result = null; } else { result = new SmartList<PsiFile>(possibleAnnotationsXmls); // sorting by writability: writable go first Collections.sort( result, new Comparator<PsiFile>() { @Override public int compare(PsiFile f1, PsiFile f2) { boolean w1 = f1.isWritable(); boolean w2 = f2.isWritable(); if (w1 == w2) { return 0; } return w1 ? -1 : 1; } }); myExternalAnnotations.put(virtualFile, result); } return result; }