-1

I defined Point class like below.

package com.sample.app.model;

public class Point {
    public int x;
    public int y;

}

When I try to construct other class with Point class data, I seen package name is corrupted.

CtClass pointClass = classPool.getAndRename("com.sample.app.model.Point", "com.sample.app.model.MyPoint");

Generated MyPoint.class code looks like below.

package model;

public class MyPoint {
  public int x;

  public int y;
}

As you see above snippet, package name is given as model, instead of "com.sample.app.model".

Surprisingly, When I print the new class name, it prints the package name correctly.

Class<?> myPointClass = pointClass.toClass();

System.out.println(myPointClass.getName());

But When I decompile the class file using Java decompiler, I see package name corruption.

Hari Krishna
  • 3,658
  • 1
  • 36
  • 57
  • Sounds more like a potential defect entry on the bug tracker of javaassist. Beyond that: probably the "in memory" representation of that class is correct ... but what gets written to disc is wrong. – GhostCat Dec 16 '19 at 13:57
  • is this your goal: How changing a class name for defining a new class? – JRichardsz Dec 16 '19 at 14:00
  • I want to construct new class from existing class content – Hari Krishna Dec 16 '19 at 14:00

1 Answers1

0

Maybe getAndRename method is not for your requirement.

From official documentation: http://www.javassist.org/tutorial/tutorial.html

Changing a class name for defining a new class

A new class can be defined as a copy of an existing class. The program below does that:

ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get("Point");
cc.setName("Line");
JRichardsz
  • 14,356
  • 6
  • 59
  • 94