This Question is Answered

2 "helpful" answers available (2 pts)
5 Replies Last post: Jun 19, 2013 11:38 AM by Denys Dushyn  
HugoPalma Novice 177 posts since
Dec 10, 2002
Currently Being Moderated

Dec 19, 2006 7:54 PM

Creating a new class

At some point in my plugin i want to create a new class in a given package in the current module from a class template.


Although this sounds simple is has lots of things that have to be taken care of before you actually create the class. Some of these are:


- What if the package doesn't exist ? I have to check if the package is created and if not create it myself.

- In which source root should the class/package be created ? I have to implement a source root selector dialog for this.


There may be more but these are the ones i thought of instantly.


Anyway, the question is:


Is there anything in the open api that already takes care of all this for me ?



Sascha Weinreuter IDEA Plugin Contest Winner 1,212 posts since
Aug 19, 2002
Currently Being Moderated
Dec 21, 2006 9:14 PM in response to: HugoPalma
Re: Creating a new class

You might find some useful stuff for that in com.intellij.ide.util.PackageUtil




Russ Jackson Newbie 5 posts since
Jul 18, 2005
Currently Being Moderated
Dec 31, 2006 4:25 PM in response to: HugoPalma
Re: Creating a new class

Once you locate the source root directory (there are several ways of doing so) you can use the following methods to create the directory structure for a package.  Note that they will only create a new directory if it does not already exist.  Also, be sure to use these inside a WriteCommandAction.


    public static PsiDirectory createDirectory(PsiDirectory parent, String name)

        throws IncorrectOperationException {

        PsiDirectory result = null;


        for (PsiDirectory dir : parent.getSubdirectories()) {

            if (dir.getName().equalsIgnoreCase(name)) {

                result = dir;





        if (null == result) {

            result = parent.createSubdirectory(name);



        return result;

    } // createDirectory()


    public static PsiDirectory createPackage(PsiDirectory sourceDir, String qualifiedPackage)

        throws IncorrectOperationException {

        PsiDirectory parent = sourceDir;

        StringTokenizer token = new StringTokenizer(qualifiedPackage, ".");

        while (token.hasMoreTokens()) {

            String dirName = token.nextToken();

            parent = createDirectory(parent, dirName);


        return parent;

    } // createPackage()

akram ajouli Newbie 57 posts since
Jan 25, 2012
Currently Being Moderated
Jan 27, 2012 12:38 PM in response to: Russ Jackson
Re: Creating a new class



I am asking how to add or create a new java class in a PsiDirectory ?

Denys Dushyn Newbie 11 posts since
Jun 14, 2011
Currently Being Moderated
Jun 19, 2013 11:38 AM in response to: akram ajouli
Re: Creating a new class


You can use JavaDirectoryService class. The class has several methods that allows you to create a java class in a given directory


JavaDirectoryService.createClass(PsiDirectory dir, String name) -- allows you to create a java class in a a given directory.

Or you can use

JavaDirectoryService.createClass(PsiDirectory dir, String name, String templateName)  -- allows to create a java class using file template

and specify a file template for this. If you going to use templates you should create freemarketer template and register it in plugin.xml via <internalFileTemplate> extention point.

More Like This

  • Retrieving data ...