Create a live template for Java in Intellij - java

I would like to create a template to automatically make my Javadoc documentation when I create a method. For example if I create a method:
protected User createUser_1P(String uname, String[] stocks) throws ElementAlreadyExists {
...
return u;
}
I would like the template to generate something like this:
/*
* #param uname user name
* #param stocks list of stocks
* #return User
* #throws ElementAlreadyExists
*/
Any help will be apreciated. Thanks

To generate JavaDoc for your method in IntelliJ all you need to do is to type /** on the line above the method definition and hit Enter.
For your method the following JavaDoc will be generated:
/**
*
* #param uname
* #param stocks
* #return
* #throws ElementAlreadyExists
*/
protected User createUser_1P(String uname, String[] stocks) throws ElementAlreadyExists {
...
return u;
}
Mind you that IntelliJ does not provide with any descriptions, you will have to fill them out.

Related

How can I get the total number of pages on Spring Data?

I have an Angular app that shows a list of entities and I have a 'show more' button that increments page number and uses this method:
Page<myEntity> result = myRepo.findByAttr(attr, page);
I format this result and send it via REST's JSON. I want to disable 'show more' button if there's no further pages to get. There's a 'frameworkie' specific way to retrieve this number or I should use findAll() and count through this list?
This is the source code of Page interface
public interface Page<T> extends Slice<T> {
/**
* Returns the number of total pages.
*
* #return the number of total pages
*/
int getTotalPages();
/**
* Returns the total amount of elements.
*
* #return the total amount of elements
*/
long getTotalElements();
/**
* Returns a new {#link Page} with the content of the current one mapped by the given {#link Converter}.
*
* #param converter must not be {#literal null}.
* #return a new {#link Page} with the content of the current one mapped by the given {#link Converter}.
* #since 1.10
*/
<S> Page<S> map(Converter<? super T, ? extends S> converter);
}
You have getTotalElements() to get the total number of matching element.
getTotalPages() will give total number of pages.
Use result.getTotalElements() to get the total number of matching element.
Use result.getTotalPages() to get the total number of page.
p.s. Use result.getContent() to get the content as List<>

Creating an enum in DELPHI

I have an enum codes in JAVA. I convert all JAVA code to DELPHI.
I almost done, but i stucked in here. I have no idea, how to convert enum to Delphi.
I am wondering, this code can be convert to Delphi ?
/**
* Enum describing the databin class ID's. Methods exist for getting the
* KakaduClassID and the StandardClassID. I have also included the string
* representations of the databins as defined for cache model updates.
*
*
*/
public enum JPIPDatabinClass {
/** Precinct data bin class. */
PRECINCT_DATABIN(KakaduConstants.KDU_PRECINCT_DATABIN, JPIPConstants.PRECINCT_DATA_BIN_CLASS, "P"),
/** Tile Header data bin class. */
TILE_HEADER_DATABIN(KakaduConstants.KDU_TILE_HEADER_DATABIN, JPIPConstants.TILE_HEADER_DATA_BIN_CLASS, "H"),
/** Tile data bin class. */
TILE_DATABIN(KakaduConstants.KDU_TILE_DATABIN, JPIPConstants.TILE_DATA_BIN_CLASS, "T"),
/** Main Header data bin class. */
MAIN_HEADER_DATABIN(KakaduConstants.KDU_MAIN_HEADER_DATABIN, JPIPConstants.MAIN_HEADER_DATA_BIN_CLASS, "Hm"),
/** Meta data bin class. */
META_DATABIN(KakaduConstants.KDU_META_DATABIN, JPIPConstants.META_DATA_BIN_CLASS, "M");
/** The classID as an integer as per the Kakadu library. */
private int kakaduClassID;
/** The classID as an integer as per the JPEG2000 Part-9 standard. */
private int standardClassID;
/**
* The classID as a string as per the JPEG2000 Part-9 standard. Used for
* cache model updates.
*/
private String jpipString;
/**
* Constructor.
*
* #param _kakaduClassID
* #param _standardClassID
* #param _jpipString
*/
JPIPDatabinClass(int _kakaduClassID, int _standardClassID, String _jpipString) {
kakaduClassID = _kakaduClassID;
standardClassID = _standardClassID;
jpipString = _jpipString;
}
/** Returns the classID as an integer as per the Kakadu library. */
public int getKakaduClassID() {
return kakaduClassID;
}
/** Returns the classID as an integer as per the JPEG2000 Part-9 standard. */
public int getStandardClassID() {
return standardClassID;
}
/**
* Returns the classID as a string as per the JPEG2000 Part-9 standard. Used
* for cache model updates.
*/
public String getJpipString() {
return jpipString;
}
};
This Enum can easily be translated to a plain old Delphi class which has a three-argument constructur like the Java Enum, and three read-only public properties.
JPIPDatabinClass = class(TObject)
private
...
public
constructor Create(AKakaduClassID: Integer; AStandardClassID: Integer; AJPIP: string);
property KakaduClassID: Integer; read FKakaduClassID;
property StandardClassID: Integer; read FStandardClassID;
property JPIP: string; read FJPIP;
end;
and 'singleton style' instances:
function PRECINCT_DATABIN: JPIPDatabinClass;
function TILE_HEADER_DATABIN: JPIPDatabinClass;
...
implementation
var
FPRECINCT_DATABIN: JPIPDatabinClass;
FTILE_HEADER_DATABIN: JPIPDatabinClass;
...
FPRECINCT_DATABIN := JPIPDatabinClass.Create( ... );
FTILE_HEADER_DATABIN := JPIPDatabinClass.Create( ... );
...
function PRECINCT_DATABIN: JPIPDatabinClass;
begin
Result := FPRECINCT_DATABIN;
end;
function TILE_HEADER_DATABIN: JPIPDatabinClass;
begin
Result := FTILE_HEADER_DATABIN;
end;
...
Note: the disadvantage of this approach is that does not create real Delphi enum types, it only emulates the Java enum type as immutable Delphi objects.

The correct way to write javadocs

I have been given the exiciting job to add some javadoc to some code.
so here is my Q:
what is the right way to write javadoc for this constructor.
public Match(int MatchID, int MatchRound, int HomeTeamID, int GuestTeamID, boolean IsPlayed) {
this.isPlayed = IsPlayed;
this.matchID = MatchID;
this.matchRound = MatchRound;
this.homeTeamID = HomeTeamID;
this.guestTeamID = GuestTeamID;
}
I wouldn't write any, because it's obvious.
I would rename all the parameters to follow Java conventions, though.
Here is the formatted Javadoc:
/**
* Constructor for creating a new match.
* #param MatchID the id of the match
* #param MatchRound the round for the match
* #param HomeTeamID the id of the home team
* #param GuestTeamID the id of the guest team
* #param IsPlayed whether or not the match is played
*/
public Match(int MatchID, int MatchRound, int HomeTeamID, int GuestTeamID, boolean IsPlayed) {
In Eclipse you can just type /** above the constructor and it will generate the Javadoc template automatically.

Mybatis No Getter Property

I'm working with Mybatis 3.2.6 and implementing a custom resulthandler. I've done this before using a simple datatype parameter and have had no problems. This time around I need to pass in several arguments... The signature I'm using is
session.select(statement, parameter, handler);
For parameter I've created a simple POJO to easily send in what I need. It is as follows:
public class DifferenceParam {
private int current;
private int compare;
private String table;
private String comparator;
/**
* Constructor excluding comparator. Will default a value of
* "code" to compare content on, e.g., <br/>
* {#code select * from a minus select * from b where a.code = b.code } <br/>
* #param table
* #param current
* #param compare
*/
public DifferenceParam(String table, int current, int compare) {
this(table, "code", current, compare);
}
/**
* Constructor providing a specific column to compare on, e.g. <br/>
* {#code select * from a minus select * from b where a.[comparator] = b.[comparator] } <br/>
* #param table
* #param comparator
* #param current
* #param compare
*/
public DifferenceParam(String table, String comparator, int current, int compare) {
this.table = table;
this.comparator = comparator;
this.current = current;
this.compare = compare;
}
/** Appropriate setters and getters to follow **/
}
The handler implementation is irrelevant at the moment, because I get an exception well in advance... The query I'm executing is:
<select id="getCodeSetModifications" parameterType="DifferenceParam" resultType="Code">
select *
from
(
select * from ${param.table} where revision_seq = #{param.current}
minus
select * from ${param.table} where revision_seq = #{param.compare}
) a, ${param.table} b
where a.${param.comparator} = b.${param.comparator}
and b.revision_seq = #{param.compare}
</select>
Here is the interface as well
public List<Code> getCodeSetModifications(#Param("param") DifferenceParam param);
The problem I'm having is that execution via a mapper e.g.,
session.getMapper(DifferenceParam.class);
works just fine, but when I invoke through a select on the session I get the following exception.
Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class com.mmm.his.cer.cerval.uidifference.map.param.DifferenceParam'
Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class com.mmm.his.cer.cerval.uidifference.map.param.DifferenceParam'
I've debugged as far as I can go into Mybatis, but am having no luck.
Thanks in advance...
When you use session.getMapper(DifferenceParam.class);, mybatis looks for #Param annotation and uses it's value in query.
When you invoke session.select(statement, parameter, handler);, such mapping doesn't occur.
Try to add public DifferenceParam getParam() { return this; } to DifferenceParam to workaround this.
when the MyBatis query only have one param. Don't need #{param.} reference.
because it default use the only param.
so when u use ${param.table}, it actually using #{DifferenceParam.param.table}.
because it think the ${param.} inside of the #{DifferenceParam.}

Janino dynamic compile interface class

I have build an application for dynamic compile java source code and fetch the compiled class information and stored to object.
The application required source directory and full qualify class name (ex. MOCG.entity.Person) for adding file to the application.
I use the Janino compiler in this application. I used to implement by javax.tools.ToolProvider compiler but I dont know how to compile multiple file, and it cannot automatically compile related class.
For now my code work just fine but when I try to compile an interface class or abstract class it always return error :
Caused by: org.codehaus.commons.compiler.CompileException: File /Users/chillyprig/IdeaProjects/Mockito/src/lab05/p1/dao/CourseDAO.java, Line 22, Column 9: Identifier expected in member declaration
at org.codehaus.janino.Parser.throwCompileException(Parser.java:2593)
at org.codehaus.janino.Parser.parseInterfaceBody(Parser.java:613)
at org.codehaus.janino.Parser.parseInterfaceDeclarationRest(Parser.java:518)
at org.codehaus.janino.Parser.parsePackageMemberTypeDeclaration(Parser.java:186)
at org.codehaus.janino.Parser.parseCompilationUnit(Parser.java:74)
at org.codehaus.janino.JavaSourceIClassLoader.findIClass(JavaSourceIClassLoader.java:150)
... 46 more
This is an input file :
/**
* Created with IntelliJ IDEA.
* User: Dto
* Date: 12/2/12
* Time: 8:26 AM
* To change this template use File | Settings | File Templates.
*/
package lab05.p1.dao;
import java.util.List;
import java.util.Set;
/**
* This is the example of the DAO interface, you have to implement the implementation class to complete the DAO classes
* #author dto
*/
public interface CourseDAO {
/**
* Get all the courses
* #return all courses stored in the persistence
*/
List<Course> getCourses();
/**
* Get all students which enroll to the courses
* #return all students in the persistence
*/
Set<Student> getStudents();
/**
* Get the course by query the name provided
* #param name the name of the course which the user wants
* #return the course which contains the same name
* null if the course with specific name is not existed
*/
Course getCourseByName(String name);
/**
* Get the Student by id
* #param id the id of the student which we want to find
* #return the student object with the specific id
* The empty student object if the student with the specific id is not exist
*/
Student getStudentById(String id);
}
This is my snipped code for compilation :
private Class compile() throws ClassNotFoundException, SourceDirectoryNotfoundException {
ClassLoader classLoader = null;
try{
classLoader = new JavaSourceClassLoader(
Thread.currentThread().getContextClassLoader(),
new File[] {new File(sourceDir)},
(String) null
);
} catch (NullPointerException e){
throw new SourceDirectoryNotfoundException();
}
Class<?> c = classLoader.loadClass(fullname);
return c;
}
Every suggestion is very appreciate. Any code example would be nice.
Janino is a Java 1.4 compatible compiler -- i.e., it can't handle generics, which were introduced in Java 5. Line 22 is the line that begins List<Course> -- a use of generics, which this compiler can't handle.

Categories

Resources