@Before
  @Override
  public void beforeTest() throws Exception {
    super.beforeTest();
    initializeJavaProject();

    // setup validation
    queueInputLines("", "");
    getShell().execute(VALIDATION_PLUGIN_NAME + " setup");

    // create a class
    assertTrue(getProject().hasFacet(JavaSourceFacet.class));

    final JavaSourceFacet javaSourceFacet = getProject().getFacet(JavaSourceFacet.class);
    assertNotNull(javaSourceFacet);

    final JavaClass fooClass =
        JavaParser.create(JavaClass.class)
            .setPackage(javaSourceFacet.getBasePackage())
            .setName(CLASS_NAME);
    fooClass.addField("private String " + PROPERTY_NAME + ";");
    fooClass.addMethod("public String " + PROPERTY_ACESSOR_NAME + "(){return foo;}");

    JavaResource fooResource = javaSourceFacet.saveJavaSource(fooClass);
    assertNotNull(fooResource);

    // pick-up the created resource
    getShell().execute("pick-up " + fooResource.getFullyQualifiedName());
  }
 @Override
 public String calculateName(JavaResource resource) {
   String fullPath = Packages.fromFileSyntax(resource.getFullyQualifiedName());
   String pkg = calculatePackage(resource);
   String name = fullPath.substring(fullPath.lastIndexOf(pkg) + pkg.length() + 1);
   name = name.substring(0, name.lastIndexOf(".java"));
   return name;
 }
Exemplo n.º 3
0
  @SuppressWarnings("unchecked")
  @DefaultCommand(help = "Create a JPA @Entity")
  public void newEntity(
      @Option(required = true, name = "named", description = "The @Entity name")
          final String entityName,
      @Option(
              required = false,
              name = "package",
              type = PromptType.JAVA_PACKAGE,
              description = "The package name")
          final String packageName)
      throws Throwable {
    final PersistenceFacet jpa = project.getFacet(PersistenceFacet.class);
    final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

    String entityPackage;

    if ((packageName != null) && !"".equals(packageName)) {
      entityPackage = packageName;
    } else if (getPackagePortionOfCurrentDirectory() != null) {
      entityPackage = getPackagePortionOfCurrentDirectory();
    } else {
      entityPackage =
          shell.promptCommon(
              "In which package you'd like to create this @Entity, or enter for default",
              PromptType.JAVA_PACKAGE,
              jpa.getEntityPackage());
    }

    JavaClass javaClass =
        JavaParser.create(JavaClass.class)
            .setPackage(entityPackage)
            .setName(entityName)
            .setPublic()
            .addAnnotation(Entity.class)
            .getOrigin()
            .addInterface(Serializable.class);

    Field<JavaClass> id = javaClass.addField("private Long id = null;");
    id.addAnnotation(Id.class);
    id.addAnnotation(GeneratedValue.class).setEnumValue("strategy", GenerationType.AUTO);
    id.addAnnotation(Column.class)
        .setStringValue("name", "id")
        .setLiteralValue("updatable", "false")
        .setLiteralValue("nullable", "false");

    Field<JavaClass> version = javaClass.addField("private int version = 0;");
    version.addAnnotation(Version.class);
    version.addAnnotation(Column.class).setStringValue("name", "version");

    Refactory.createGetterAndSetter(javaClass, id);
    Refactory.createGetterAndSetter(javaClass, version);
    Refactory.createToStringFromFields(javaClass, id);
    createHashCodeAndEquals(javaClass);

    JavaResource javaFileLocation = java.saveJavaSource(javaClass);

    shell.println("Created @Entity [" + javaClass.getQualifiedName() + "]");

    /** Pick up the generated resource. */
    shell.execute("pick-up " + javaFileLocation.getFullyQualifiedName());
  }