Is there any easy way to generate Interfaces for Existing classes? - java

I have many classes.
Now i want all those classes to implement their own Interface. I can manually create interfaces by copying all method signatures to Interface.
Is there a automatic way to create them in Eclipse?

In your source code or in package explorer or outline view, select one or more methods that are not yet implementing an interface, right-click and select "Refactor" --> "Extract interface"

Try with right click on ImplementationClass in the package explorer or members view. Then go to Refactor(shortcut Alt + Shift + T) -> Extract Interface . Then select methods for Interface

Related

When creating a Java class in IntelliJ: is there a wizard/menu to select the package and class to extend from?

One of the features I like of Eclipse is that when creating Java classes, a wizard is available to specify different properties for the class. Like its package, class to extend from...etc (see below in the screen cap).
Does IntelliJ provide something similar? I created a class but the process wasn't smooth. I had to...mark /java directory as Source Root...create new package...create a new class...and now, I want to extend from a different class other than Object... I would like to avoid doing this step by step, if possible.
I am using IntelliJ Community Edition, version 15.
No, there is no such wizard. Marking the directory as a source root is a one-time action, and is normally taken care of when you create the project. To specify the package, you can enter the package and class name directly in the "New Java class" dialog. If you want to extend a different class, the code completion will help you enter the "extends" or "implements" keyword and specify the base class name.
You can make package at the same time as you create java class.
When you do right click -> new -> java class : at the place of the name you can mark
my.new.package.ClassName and IntelliJ will create the package : my.new.package for you.
For more explication you can visit this page.
In IJ you don't need to write all of this stuff manually. Just right click on the method an choose Go to Test or press Ctrl-Shift-T. This functionality is described on the help page IntelliJ IDEA 15.0 Help /Creating Tests.
In the Create Test dialog:
Select the testing library to be used.
If the selected library is missing in your module, click the Fix button. As a result, the corresponding library will be automatically added to the module libraries.
Define the name and location of the test class to be generated.
In the Class name field, specify the name of the stub test class to be generated.
In the Superclass field, IntelliJ IDEA suggests the appropriate super class for JUnit3. For JUnit 4 and TestNG, this field is blank.
In the Destination package field, define where the generated test class should be placed.
Specify whether you want the setUp()/tearDown() methods (for JUnit), or the #Before/#After annotations to be generated.
In the table that shows the list of all the methods of the source class, select the ones you want to generate test methods for.
Click OK.
In addition to other answers if you hit Alt-Enter on any interface or abstract class name one of the intention options will be to create an implementation.

How to generate interface's methods into all implementation classes?

I am looking for some kind of refactoring feature in Eclipse to generate methods in implementation classes from an interface class. Let's say I have JavaClassImpl1 and JavaClassImpl2, which implement JavaClassInterface. What I'd like to do is when I add a method to JavaClassInterface, the refactoring option is to generate empty methods in all implementation classes, in this case, in JavaClassImpl1 and JavaClassImpl2.
If you haven't implemented all of the required methods in eclipse, it will show a bunch of compile-time errors in your class (the class will have a red underline).
If you hover over the class name, a pop-up will appear with available quick fixes, one of which will be Add unimplemented methods. If you select that eclipse will generate a stub for each unimplemented method.
Another way to access the same function is to rightclick in your class and select Source -> Override/Implement Methods... which will pop up a wizard which allows you to select which methods you would like eclipse to stub out for you.
Update
If the change you make to your interface is done via one of the eclipse refactor tools, then eclipse will generally update references to the thing that was changed. For instance, if you do a Refactor -> Introduce Parameter Object... on a method in your interface, then eclipse will automatically update implementing methods.
If you're adding code manually, eclipse doesn't even know about the change until the next time you compile, and you'll have to explicitly ask eclipse to refactor the code.

How do I add methods and attributes using ObjectAid?

My goal is to add methods and attributes to the class diagram to make it generate stubs of code in the classes.
Here's an example:
If I'm just starting a project. I have a class called tuna and I want to add the methods swim(), eat() and poop() with it generating stubs. This way by the time I'm done with all the classes and start to code I won't forget what I was thinking.
Could ObjectAid be the wrong tool for the job?
ObjectAid can only show you the source code, you have to go to an editor to change it. When you double-click on a class, method or field, an editor will open at the proper location.

How to implement unimplemented methods to all child classes in eclipse

As you know, eclipse provides with a nice way to implement unimplemented methods if a child class does not have them implemented. Is there any way to apply this to all child classes?
My problem is that I have to do this for each child class when there are 50 of them. I would appreciate any help.
select the top level package in your package explorer. Go to the 'Problems' view. There should be the list of errors of "The type Foo must implement the inherited abstract method Parent.foo()" (for given class/method names). Right click on the error, select "Quick Fix". You can select the "Add unimplemented methods" option and click the "Select All Button" to select all the child classes.
EDIT: This works even for multiple methods per parent class.
Problem can be solved using "source/cleanup" feature, after applying few changes in eclipse as:
Make your own project specific profile for Java Code Style/Clean Up:
Select the Project/BuildPath/Configure Build Path> Click on Java Code Style/Clean Up > Check Enable Project Specific Settings/Edit> Check Add unimplemented methods and give Profile name.
2. Select Project/Source/Clean Up, it will show project name and the profile name we created. Click on finish.
Now method will be added to all child classes:
Once profile is created, this works even for multiple methods added in the interface or abstract class. Just follow step 2.
For Android Studio guys below is the workaround for it,
Add this method in your interface or abstract class,
#Override
public void methodInInterfaceOrAbstract()
{
// this must me in interface or abstract class which will be implemented by child classes
}
Then right click the java file, select the option "Refactor" then select the option "Push members down". Select the method which you want to push down to all child classes and click refactor.
Your new method added in interface or abstract class will be moved to all child classes.
Now you need to add the declaration of the method in interface or abstract class again as the method was moved to all child classes.

How to create a custom 'new class wizard' for Eclipse?

I would like to create a functionality ( for myself ), wherein on clicking a button ( or say firing any event or anything that can trigger my program ), a popup will be displayed which will ask the name of the Class, objects it have and few more thing. Then on pressing OK, it will create a java file with skeleton of predefined methods, inherit known interface and ...
So, basically how to do that? Do i need to create a plugin for eclipse or there is something else in eclipse for it.
PS Please change the title. I am unable to think of any better one.
As others said, you want to create a wizard, then you want to augment the New Class Wizard, which is doing something similar to what you want (but the default wizard don't allow you to to add fields and custom methods).
To create a wizard, you can use the "New File Wizard" extension template: Create a plug-in, then, go to the extensions tab, select Add..., and select the "Extension Wizards" tab. That will get you started on Eclipse wizards.
Once you've learned the basics of creating Wizards and pages, then, include the org.eclipse.jdt.ui and org.eclipse.jdt.core in your plug-in dependencies. Open the following type (Ctrl-Shift-T): "NewClassWizardPage". This is the page that is displayed when you select New > Class in the Package Explorer.
You can probably either copy this page and the parent pages to help you get started or just extend it (in my experience, internal Eclipse wizards such as this one are difficult to extend because they have lots of fields and methods that are package/private, so I usually end up copying the code as a starting point... don't forget to keep the license though!).
You more or less want to add your own wizzard to the 'new class' dialog .. right?
This was the first site I found when typing "creating your own new wizzard eclipse" in Google: http://www.eclipse.org/articles/article.php?file=Article-JFaceWizards/index.html
I may be mis-understanding the question, but it sounds like you are re-implementing the New Class Wizard that exists already.
It lets you name the class, the containing package. Can assign a superclass and/or interface and can also choose if you want to include the contructors for the superclass.
A new .java file is created with all known methods from declared interfaces and also any abstract methods from the superclass.
Edt: Title was changed whilst I was writing this reply to "How to creat a customer 'new class wizard; for Eclipse". It makes my answer slightly redundant but I'm not seeing any new functionality being added in the question.

Categories