@Override public QuickFix_Runtime getQuickFix() { if (myQuickFixTaken) { return myQuickFix; } try { String languageNamespace = NameUtil.namespaceFromLongName(NameUtil.namespaceFromLongName(myClassFQName)); Language language = ModuleRepositoryFacade.getInstance().getModule(languageNamespace, Language.class); Class aClass = ClassLoaderManager.getInstance().getClass(language, myClassFQName); if (aClass == null) { LOG.error("class " + myClassFQName + " not found in a module " + languageNamespace); myQuickFixTaken = true; return null; } QuickFix_Runtime quickFix = (QuickFix_Runtime) aClass.getConstructor().newInstance(); for (String s : myMap.keySet()) { quickFix.putArgument(s, myMap.get(s)); } myQuickFix = quickFix; myQuickFixTaken = true; return quickFix; } catch (Throwable t) { LOG.error(null, t); myQuickFixTaken = true; return null; } }
@Override public void read(Element element, Project project) throws CantLoadSomethingException { super.read(element, project); Element filterXML = element.getChild(FILTER); String filterName = filterXML.getAttribute(CLASS_NAME).getValue(); try { Class filterClass = null; for (Language l : project.getProjectModules(Language.class)) { filterClass = ClassLoaderManager.getInstance().getClass(l, filterName); if (filterClass != null) break; } if (filterClass == null) { try { filterClass = Class.forName(filterName); } catch (ClassNotFoundException e) { filterClass = null; } } if (filterClass != null) { myFilter = (BaseFilter) filterClass.newInstance(); } else { throw new CantLoadSomethingException("Can't find filter class " + filterName); } } catch (Throwable t) { throw new CantLoadSomethingException("Can't instantiate or read filter " + filterName, t); } }
public void init() { if (INSTANCE != null) { throw new IllegalStateException("double initialization"); } INSTANCE = this; myClassLoaderManager.addReloadHandler(myReloadHandler); SModelRepository.getInstance().addModelRepositoryListener(mySModelRepositoryListener); }
private TemplateModel getTemplateModel(String modelName) { Class<TemplateModel> clazz = ClassLoaderManager.getInstance() .getClass(ModuleRepositoryFacade.getInstance().getModule(getReference()), modelName); try { return clazz.getConstructor(TemplateModule.class).newInstance(this); } catch (Exception e) { throw new RuntimeException(e); } }
public void dispose() { for (SModel model : myListeningForModels) { ((SModelInternal) model).removeModelListener(myModelListener); } myListeningForModels.clear(); for (SNodeReference nodePointer : new ArrayList<SNodeReference>(myTypeCheckingContexts.keySet())) { removeContextForNode(nodePointer); } SModelRepository.getInstance().removeModelRepositoryListener(mySModelRepositoryListener); myClassLoaderManager.removeReloadHandler(myReloadHandler); INSTANCE = null; }
private TemplateModel getTemplateModel(String modelName) { Class<TemplateModel> clazz = ClassLoaderManager.getInstance() .getClass(ModuleRepositoryFacade.getInstance().getModule(getReference()), modelName); if (clazz == null) { throw new IllegalStateException( String.format("Failed to obtain generator runtime class for model %s", modelName)); } try { return clazz.getConstructor(TemplateModule.class).newInstance(this); } catch (RuntimeException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } }
public ClassLoaderManager getClassLoaderManager() { return ClassLoaderManager.getInstance(); }
@Override public void disposeComponent() { GlobalSModelEventsManager.getInstance().removeGlobalModelListener(myListener); FileStatusManager.getInstance(myProject).removeFileStatusListener(myFileStatusListener); myClassLoaderManager.removeReloadHandler(myReloadListener); }
@Override public void initComponent() { myClassLoaderManager.addReloadHandler(myReloadListener); FileStatusManager.getInstance(myProject).addFileStatusListener(myFileStatusListener); GlobalSModelEventsManager.getInstance().addGlobalModelListener(myListener); }
@Nullable @Override public ClassLoader getClassLoader() { return myManager.getClassLoader(this); }
public class ReloadableModuleBase extends AbstractModule implements ReloadableModule { private static final Logger LOG = LogManager.getLogger(ReloadableModuleBase.class); private final ClassLoaderManager myManager = ClassLoaderManager.getInstance(); private final List<SModuleDependenciesListener> myListeners = new CopyOnWriteArrayList<SModuleDependenciesListener>(); protected ReloadableModuleBase(IFile file) { super(file); } protected ReloadableModuleBase() {} @NotNull @Override public Class<?> getClass(String classFqName) throws ClassNotFoundException, ModuleIsNotLoadableException { return getClass(classFqName, false); } @NotNull @Override public Class<?> getOwnClass(String classFqName) throws ClassNotFoundException, ModuleIsNotLoadableException { return getClass(classFqName, true); } @NotNull protected Class<?> getClass(String classFqName, boolean ownClassOnly) throws ClassNotFoundException, ModuleClassNotFoundException, ModuleIsNotLoadableException { ClassLoader classLoader = getClassLoader(); if (classLoader == null) { throw new ModuleClassLoaderIsNullException(this); } String internClassName = InternUtil.intern(classFqName); if (ownClassOnly && classLoader instanceof ModuleClassLoader) { return ((ModuleClassLoader) classLoader).loadOwnClass(internClassName); } Class<?> aClass = classLoader.loadClass(internClassName); if (aClass == null) { throw new LoadedClassIsNullException(classLoader, internClassName); } return aClass; } @Nullable @Override public ClassLoader getClassLoader() { return myManager.getClassLoader(this); } @Override public ClassLoader getRootClassLoader() { getRepository().getModelAccess().checkReadAccess(); Set<MPSModuleOwner> moduleOwners = ModuleRepositoryFacade.getInstance().getModuleOwners(this); for (MPSModuleOwner owner : moduleOwners) { if (owner instanceof SLibrary) { ClassLoader classLoader = ((SLibrary) owner).getPluginClassLoader(); if (classLoader != null) { return classLoader; } } } return ReloadableModule.class.getClassLoader(); } @Override public void reload() { if (!willLoad()) return; LOG.info("Reloading module " + this); myManager.reloadModule(this); } @Override public boolean willLoad() { return true; } @Override protected void dependenciesChanged() { super.dependenciesChanged(); if (!willLoad()) return; fireDependenciesChanged(); } protected final void fireDependenciesChanged() { assertCanChange(); for (SModuleDependenciesListener listener : myListeners) { listener.dependenciesChanged(this); } } // NOTE: for internal use public final void addDependenciesListener(SModuleDependenciesListener listener) { myListeners.add(listener); } // NOTE: for internal use public final void removeDependenciesListener(SModuleDependenciesListener listener) { myListeners.remove(listener); } // NOTE: for internal use // notifies about ANY changes in deps, used languages, etc. // designed specifically for the class loading client public static interface SModuleDependenciesListener { public void dependenciesChanged(@NotNull ReloadableModuleBase module); } }
@Override public void reload() { if (!willLoad()) return; LOG.info("Reloading module " + this); myManager.reloadModule(this); }
public TypeContextManager(TypeChecker typeChecker, ClassLoaderManager classLoaderManager) { myTypeChecker = typeChecker; myClassLoaderManager = ClassLoaderManager.getInstance(); }