Eclipse中自动生成get/set时携带注释给get/set

 编码的时候通常要用到 JavaBean ,而在我们经常把注释写在字段上面,但生成的Get/Set方法不会生成,通过修改Eclipse源码可解决。效果如下:

  1. /**  
  2.  * 员工ID  
  3.  */  
  4. private String userid;   
  5. /**  
  6.  * 获取员工ID  
  7.  * @return userid 员工ID  
  8.  */  
  9. public String getUserid() {   
  10.     return userid;   
  11. }   
  12. /**  
  13.  * 设置员工ID  
  14.  * @param userid 员工ID  
  15.  */  
  16. public void setUserid(String userid) {   
  17.     this.userid = userid;   
  18. }  

 

解决方案如下:

通过反编译技术,修改ECLIPSE的org.eclipse.jdt.ui_3.3.1.r331_v20070906.jar包中的
org.eclipse.jdt.internal.corext.codemanipulation.GetterSetterUtil类
下的getSetterStub及getGetterStub函数,来实现生成getter/setter时,注释使用成员的注释(如果成员的注释有的话,如果没有则使用注释模板生成,如果选择了生成getter/setter需要生成code comment的话)

getSetterStub函数修改的地方为:
将原:
  if(addComments)
  {
  String comment = CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, lineDelim);

修改为:
  if(addComments)
  {
  String comment = CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, lineDelim);
  ISourceRange sr = field.getJavadocRange();
  if( null != sr )
  comment = field.getSource().substring( 0, sr.getLength() );


getGetterStub函数修改的地方为:
将原:
  if(addComments)
  {
  String comment = CodeGeneration.getGetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, field.getElementName(), typeName, accessorName, lineDelim);

修改为:
  if(addComments)
  {
  String comment = CodeGeneration.getGetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, field.getElementName(), typeName, accessorName, lineDelim);
  ISourceRange sr = field.getJavadocRange();
  if( null != sr )
  comment = field.getSource().substring( 0, sr.getLength() );

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的JavaFX贪吃蛇代码,附带有注释,供您参考: ```java import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.geometry.Point2D; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.input.KeyCode; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.stage.Stage; import java.util.ArrayList; import java.util.List; import java.util.Random; public class SnakeGame extends Application { private static final int TILE_SIZE = 20; // 每个方格的大小 private static final int WIDTH = 20; // 游戏界面的宽度(单位:方格) private static final int HEIGHT = 20; // 游戏界面的高度(单位:方格) private List<Point2D> snake = new ArrayList<>(); // 蛇的身体,用一个点的列表表示 private Point2D food; // 食物的位置,用一个点表示 private Direction direction = Direction.RIGHT; // 蛇移动的方向,默认向右移动 private boolean gameOver; // 游戏是否结束 private GraphicsContext graphicsContext; // 画布的上下文 // 游戏的主循环 private AnimationTimer gameLoop = new AnimationTimer() { private long lastUpdate = 0; @Override public void handle(long now) { if (lastUpdate == 0) { lastUpdate = now; return; } if (now - lastUpdate < 1000000000 / 8) { return; } lastUpdate = now; // 更新蛇的位置 updateSnake(); // 检查蛇是否吃到了食物 checkFood(); // 重新绘制画面 draw(); } }; // 启动游戏 private void startGame() { // 初始化蛇的位置 snake.clear(); snake.add(new Point2D(WIDTH / 2, HEIGHT / 2)); // 初始化食物的位置 food = new Point2D(new Random().nextInt(WIDTH), new Random().nextInt(HEIGHT)); // 标记游戏未结束 gameOver = false; // 启动游戏循环 gameLoop.start(); } // 更新蛇的位置 private void updateSnake() { // 根据移动的方向,计算蛇头的新位置 Point2D head = snake.get(0); switch (direction) { case UP: head = new Point2D(head.getX(), head.getY() - 1); break; case DOWN: head = new Point2D(head.getX(), head.getY() + 1); break; case LEFT: head = new Point2D(head.getX() - 1, head.getY()); break; case RIGHT: head = new Point2D(head.getX() + 1, head.getY()); break; } // 将蛇头插入到列表的头部 snake.add(0, head); // 如果蛇头吃到了食物,就不需要移动蛇尾,否则需要移动 if (head.equals(food)) { // 生成新的食物位置 food = new Point2D(new Random().nextInt(WIDTH), new Random().nextInt(HEIGHT)); } else { // 移除蛇尾 snake.remove(snake.size() - 1); } // 检查蛇是否碰到了边界或者自身 if (head.getX() < 0 || head.getX() >= WIDTH || head.getY() < 0 || head.getY() >= HEIGHT) { gameOver = true; } for (int i = 1; i < snake.size(); i++) { if (head.equals(snake.get(i))) { gameOver = true; break; } } } // 检查蛇是否吃到了食物 private void checkFood() { if (snake.get(0).equals(food)) { food = new Point2D(new Random().nextInt(WIDTH), new Random().nextInt(HEIGHT)); } } // 重新绘制画面 private void draw() { // 清空画布 graphicsContext.setFill(Color.WHITE); graphicsContext.fillRect(0, 0, WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE); // 绘制蛇的身体 graphicsContext.setFill(Color.GREEN); for (Point2D point : snake) { graphicsContext.fillRect(point.getX() * TILE_SIZE, point.getY() * TILE_SIZE, TILE_SIZE, TILE_SIZE); } // 绘制食物 graphicsContext.setFill(Color.RED); graphicsContext.fillRect(food.getX() * TILE_SIZE, food.getY() * TILE_SIZE, TILE_SIZE, TILE_SIZE); // 如果游戏结束,就在画布上显示“Game Over” if (gameOver) { graphicsContext.setFill(Color.BLACK); graphicsContext.fillText("Game Over", WIDTH * TILE_SIZE / 2 - 30, HEIGHT * TILE_SIZE / 2); } } @Override public void start(Stage primaryStage) throws Exception { // 创建画布和画布的上下文 Canvas canvas = new Canvas(WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE); graphicsContext = canvas.getGraphicsContext2D(); // 创建场景和根节点 StackPane root = new StackPane(); root.getChildren().add(canvas); Scene scene = new Scene(root); // 处理键盘事件 scene.setOnKeyPressed(e -> { if (e.getCode() == KeyCode.UP && direction != Direction.DOWN) { direction = Direction.UP; } else if (e.getCode() == KeyCode.DOWN && direction != Direction.UP) { direction = Direction.DOWN; } else if (e.getCode() == KeyCode.LEFT && direction != Direction.RIGHT) { direction = Direction.LEFT; } else if (e.getCode() == KeyCode.RIGHT && direction != Direction.LEFT) { direction = Direction.RIGHT; } else if (e.getCode() == KeyCode.SPACE && gameOver) { startGame(); } }); // 启动游戏 startGame(); // 显示场景 primaryStage.setScene(scene); primaryStage.setTitle("Snake Game"); primaryStage.show(); } // 枚举类型,表示蛇移动的方向 private enum Direction { UP, DOWN, LEFT, RIGHT } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值