Is there a way to add a custom class template to Eclipse? - java

Here's the scenario. I often create classes that follow a certain pattern. All classes extend a base abstract class (out of my control) with certain methods I wish to always override, including the constructor. I'd like to rightclick on my package name, click New, and then click "Foo Class". I'd then have a class added to my project, but using the "Foo" template instead of the standard class template. I understand I can change the class template, but I don't want to change it for all classes. Is this possible without writing a full blown extension?

you can use the editor template to create a new template and use that.
Go to Preferences->Java->Editor->Templates and create your template there.
Then create a new class file and apply your template.

Unfortunately no. What you'd like to achieve needs writing an eclipse plugin.
I'd suggest setting up a Template (at Preferences | Java | Editor | Templates) and giving it a short useful name describing your scenario. Whenever you'd want to write an additional template class, you could create a new class, press Ctrl+A, Del (select all and remove), then type "name Ctrl+Space" and have your template there as configured.
ps. I'd really review your requirement, can't think of a valid approach at the moment which would require writing many similar classes without a valid way to remove the code duplication you are just about to introduce.

Related

how to create an eclipse plugin to auto create read/ writeExternal methods of existing class

How to create eclipse plugin to auto create the serialization code read/writeExternal on existing code java classes?
Steps needed get the class from active tab (and or info on class field info like one in outline window) and generate code for each field, maybe using reflection will also help.
The easiest way is to build upon org.eclipse.jdt.ui.actions.GenerateMethodAbstractAction
that is used by eclipse to implement GenerateToStringAction and GeneateHashCodeEqualsAction.
Basically:
Build a basic sub-class of GenerateMethodAbstractAction
Implement logic that enumerates all the fields / properties etc. you want to process in generateCandidates(). You also need to decide if you recurse into superclass or not.
Implement logic that generates MethodDeclarations for readExternal/writeExternal methods using data collected in step 2.
Wrap generated MethodDeclarations into an IWorkspaceRunnable that applies them as edits (see GenerateToStringOperation) and return it from createOperation(...).
Register your new action to "Source" menu so it can be used
The code required is rather long and involved so it's better to follow the two existing action classes for guidance.
If you choose to put it somewhere else than "Source" menu, you can discover the active editor with
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()
See also:
How to extend the source menu in Eclipse? (or: What is its locationURI?)
How to get "active editor" in Eclipse plugin?

Multiple classes in the same eclipse window

I'm reading Thinking in Java and it's frustrating to declare each class in a separate window in Eclipse, as the examples often contain 6-7 very simple classes.
I can just make a new class file, make one class public in this class file and the others with default access, but I don't know what should be the class' name I created. For example, I do the following:
New -> Class -> and then I must choose a class name, let's say it's Dog.
Now, in this file, I have this:
public class Dog {
}
class Cat {
}
But since I have two classes, it's a little weird to have this class file (I don't know if it's the right word here?) to be named Dog in Eclipse (The name in the src folder).
Is there a better way to declare multiple classes in the same window(?) in Eclipse?
A java file can have at most only one public class into it. And the name of that file should be same as of that public class.
I would say the frustration are not genuine because:
This is the how Java is designed and makes all sense to define each
class in a separate file. (Unless you want to write your own compiler)
You may want to use some shortcuts e.g.
Cntrl + Shift + R` to search a class
Alt + Shift + R to rename
You can update Eclipse to use shortcut for switching within classes.
What you're doing isn't going to compile. Each top level java class must be declared in a file with the same name. It will give you an error "Cat must be declared in its own file" or something like that. If you really want to, you can put the Cat class inside of the Dog class, which is called an inner class. However since they aren't related classes you shouldn't do that. Just declare each one in its own file.
Keep each class in it's own position. If your class is small and data can be exposed you can consider using nested (inner) class.
By the way, in Eclipse you can show multiple class at same time. Just drag you file title to some place.
To actually answer your question, rather than leave a bunch of comments stating why you shouldn't (which you seem to understand already), no. There isn't really a better way to do what you want. I don't know if it will compile or not (I seem to recall seeing that in the past in Java 5), but KyleM seems to think not so we'll go with that.
Short answer: no, there is not a better way to declare multiple classes in the same file.
(I don't want to suggest inner classes because that is kind of complicated for someone just starting java, as your post suggests).
Don't mix Eclipse window with files, you can understand a .java file as a container for a java class. It's the standard way and it would help you to have a more clear project when it becomes bigger.
You can have more information about this here
If you want 2 classes in the screen you can split the eclipse editor window by dragging the opened tab file and drop it on the tabs zone.
Unfortunately you do have to do this the long way, as everyone else has suggested / insisted. If the problem is a matter of clicking around through tabs, though, eclipse does allow you to drag tabs into new windows on the screen, which lets you view potentially all of them at once.
You also end up with an "overview" of the classes in the file explorer on the left of the screen, if that's more along the lines of what you're looking for.
Good luck (:

Dynamic templates in Play Framework 2.0

There is TemplateLoader in Play 1.0 for generating templates in runtime.
Is there any solution to dynamically load template in Play 2.0? Or can I somehow convert it to scala code for using Eval?
For example: I want to store some templates in the database, so that certain users could edit them.
Play 2.0 already compiles your templates to object methods, so you don't have to 'dynamically load' them!
Consider this simple template called app/views/test.scala.html.
#(num:Long)
Your number is #num
It becomes a Scala method of views.html called test. Evaluate it with this code:
val msg : String = views.html.test(23).toString()
You don't have to use html views only. To use templates with strings, use the play.api.templates.Txt derived classes. This is a template called app/views/quick.scala.txt:
#(id:Long)Your id is #id
It becomes a method views.txt.quick and is used:
val msg2 : String = views.txt.quick(32).body
You can find out more in the documentation for the the play.api.templates package.
It seems the relevant code is in framework/src/play/src/main/scala/system/ApplicationProvider.scala in the Play-2.0 directory, particularly the ReloadableApplication class. I'm unsure how this compiling on the fly would suit you, since you don't want to do it when the template is requested (it is slow). Which means that storing in a database doesn't quite make sense: you don't want to store the template source code, but rather the compiled template object.
For arguments sake, if you just wrote the templates to the app/views directory, you could leave Play to compile them at its leisure. But then, beware, because they probably won't compile on a production system.

Invoking acceleo template from java app

Hi all i'm very new to acceleo
In my project, i have a java driver class which calls different acceleo templates. Some of the templates have parameters without any EObject type. I am calling calling initialize() and doGenerate() functions of generated java module for a template. the problems are facing are :
initialize() expects the first argument to be Ecore object and rest of parameters are as List. However as I mentioned some of the templates do not have any EObject parameters. How do I call such templates from a java application?
To work around the above problem, i adjusted my driver and templates to have dummy EObject as first parameter. Then it calls templates successfully but it won't generate any output. The templates generate output if I call them from another driver template though. However I do not want to write my driver program in MTL as it requires complex analysis of data model.
Please advice me on how can I progress in my case.
Thanks&Regards
Dhanunjaya M.
The API we expose by default through the Java class we generate alongside the "main" templates' modules and the Acceleo "facade" classes always assume that there is an EObject as first parameter of the templates that are to be called. This has been made in order to facilitate the use for most use cases (we expect this use case to be 90% of the total).
For other use cases, you will have to make use of the APIs that are behind those facades. Namely, you can create another "initialize" method that does not take an EObject as parameter for these cases when you simply don't have one. You will then need to also override the "generate(Monitor)" method so that it does not use AcceleoService.doGenerate... or any other method of AcceleoService for that matter : this is the "facade" class that I was talking about.
What you will need is to call a method that mimics what AcceleoService.doGenerate does without relying on an EObject to find the template that needs to be called. If you do not have Acceleo's SDK or sources at hand, you can take a peek at the code through github : AcceleoService#doGenerate.

Can eclipse extract a second class in class file to its own file

I often refactor code first by creating an inner class inside the class I'm working on--When I'm done, I move the entire thing into a new class file. This makes refactoring code into the new class extremely easy because A) I'm only dealing with a single file, and B) I don't create new files until I have a pretty good idea of the name/names (Sometimes it ends up as more than one class).
Is there any way Eclipse can help me with the final move? I should just be able to tell it what package I want the class in, it can figure out the filename from the class name and the directory from the package.
This seems like a trivial refactor and really obvious, but I can't figure out the keystrokes/gestures/whatever to make it happen. I've tried dragging, menus, context menus, and browsing through the keyboard shortcuts.
Anyone know this one?
[edit] These are already "Top Level" classes in this file, not inner classes, and "Move" doesn't seem to want to create a new class for me. This is the hard way that I usually do it--involves going out, creating an empty class, coming back and moving. I would like to do the whole thing in a single step.
I'm sorry I gave the wrong answer before. I rechecked, and it didn't do quite want you want. I did find a solution for you though, again, in 3.4.
Highlight the class, do a copy CTRL-C or cut CTRL-X, click on the package you want the class do go into, and do a paste, CTRL-V. Eclipse will auto generate the class for you.
Convert Member Type to Top Level doesn't quite work. Doing that will create a field of the outer class and generate a constructor that takes the outer class as a parameter.
In Eclipse 3.6, you can do: Refactor -> Move type to new file
Right-click the class name (in the source code) and choose Refactor -> Convert Member Type to Top Level. It doesn't let you choose the package, though.
For IntelliJ IDEA / Android Studio:
Refactor -> Move -> Move inner class MyInnerClass to upper level
Can be done in 2 refactorings :
Convert Member type to top level
Move

Categories

Resources