Do all open projects in Intelij 8 have the same class loader?

I am creating a plugin where I have used some singleton classes containing static member objects storing meta-information about current project.

With a single class loader, if more than one project gets opened simultaneously then same instance of the singleton class will be shared by all the projects and hence changes in one project will reflect in the other through the statics.

Is there a way by which we can specify that separate class loaders should be used for every project which is opened?

Edited by: Kunjan on Nov 11, 2008 11:36 AM

6 comments
Comment actions Permalink

No, AFAIK that's not possible. You have to use a project component (or service) to store project-specific data.

0
Comment actions Permalink

Yes i am using project component.

But the problem is in my VCS i am using singleton classes.

Now suppose I take the instance of a singleton class say, SING, in the project component.

Then i assign value to it's static member variable say, PROJPATH, i.e. the path of the current open project.

Now when another project is opened in a new frame then again project component is invoked which looks for the instance of SING.

Now since there is a single class loader for all projects so, this project component also gets the same instance of SING as shared by the previous project's project component.
So, this time when PROJPATH gets the value , it overrides the value stored previously.

This is the main problem I am having.

So now i'm ending up writing my own custom class loader to load my VCS classes for me.

0
Comment actions Permalink

Yes, I did understand your problem from your first post. My suggestion is to translate your singletons (e.g. SING) into a project component too.

0
Comment actions Permalink

But my singleton's are in my VCS code which is a java application and not an idea plugin.
My plugin does not have singletons but I need to instantiaite the singletons present inside my VCS package to get an access to the member objects.

So i can't actually make them project component because then my VCS would become dependent to intellij API.

Or is there a way by which I can make my VCS singleton classes project component and still have my VCS run for other IDEs also.

0
Comment actions Permalink

I see, that's another situation. Then you probably really have to use your own classloader.

0
Comment actions Permalink

Hello Kunjan,

Yes, all projects use the same classloader. No, there is no way to configure
it differently. You must never store Project or Module instances in static
variables.

I made a plugin with a following project component:
*public class ProjComp implements ProjectComponent {*
*private static Project proj;*
*public ProjComp(Project project) {*
*proj=project;*
*}*
*public void initComponent() {*
*System.out.println("Initiated"+proj.getName());*
*}*
*public void disposeComponent() {*
*System.out.println("Disposed"+proj.getName());*
*}*
@NotNull
public String getComponentName() {
return "ProjComp";
}
public void projectOpened() {
// called when project is opened
}
public void projectClosed() {
// called when project is being closed
}
}
Basically it's supposed to print the names of project as they are
initiated and disposed.

Then to test it I ran it and opened projects in following order:
Project PlugTry
Project PlugBug
And then I closed them.

I was amazed to see the output as:
InitiatedDefault (Template) Project
InitiatedPlugTry
InitiatedPlugBug
DisposedPlugBug
DisposedPlugBug
DisposedPlugBug
What the output suggests me is that a same class loader is working for
all projects and as a result the static member variable proj is being
shared?

Now my problem is that I am creating a plugin where I have used some
singleton classes containing static member objects storing
meta-information about current project.

With a single class loader, if more than one project gets opened
simultaneously then same instance of the singleton class will be
shared by all the projects and hence changes in one project will
reflect in the other through the statics.

Is there a way by which we can specify that separate class loaders
should be used for every project which is opened?

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.