/** * Registers the nested DTO created for a JPA entity * * @param entity The JPA entity * @param nestedDTO The nested DTO created for the JPA entity */ public void addNestedDTO(JavaClass entity, JavaClass nestedDTO) { JavaSourceFacet java = this.project.getFacet(JavaSourceFacet.class); DTOPair dtoPair = dtos.containsKey(entity) ? dtos.get(entity) : new DTOPair(); if (nestedDTO != null) { try { if (!java.getJavaResource(nestedDTO).exists() || prompt.promptBoolean("DTO [" + nestedDTO.getQualifiedName() + "] already, exists. Overwrite?")) { dtoPair.nestedDTO = java.saveJavaSource(nestedDTO); } } catch (FileNotFoundException fileEx) { throw new RuntimeException(fileEx); } } dtos.put(entity, dtoPair); }
@Command("new-plugin") public void newPlugin(final PipeOut out, @Option(required = true, name = "named", description = "The plugin name") final String pluginName, @Option(name = "alias", description = "The plugin alias") final String alias) throws FileNotFoundException { String pluginAlias = Strings.isNullOrEmpty(alias) ? pluginName.replaceAll("([^A-Za-z0-9-])|[Plugin]", "").toLowerCase() : alias; if (Strings.isNullOrEmpty(pluginAlias)) { throw new RuntimeException("You should specify a valid alias for this plugin"); } JavaSourceFacet java = project.getFacet(JavaSourceFacet.class); String className = canonicalize(pluginName); String packg = prompt.promptCommon( "In which package you'd like to create [" + className + "], or enter for default", PromptType.JAVA_PACKAGE, java.getBasePackage()); Map<Object, Object> context = new HashMap<Object, Object>(); context.put("name", className); context.put("alias", pluginAlias); CompiledTemplateResource pluginSource = compiler.compileResource(getClass().getResourceAsStream( "/org/jboss/forge/dev/PluginTemplate.jv")); CompiledTemplateResource testSource = compiler.compileResource(getClass().getResourceAsStream( "/org/jboss/forge/dev/PluginTemplateTest.jv")); JavaResource pluginResource = java.saveJavaSource(JavaParser.parse(JavaClass.class, pluginSource.render(context)) .setPackage(packg)); java.saveTestJavaSource(JavaParser.parse(JavaClass.class, testSource.render(context)).setPackage(packg)); pickup.fire(new PickupResource(pluginResource)); }
Resource<?> r = java.getSourceFolder(); DirectoryResource base = java.getBasePackageResource();
private Field<JavaClass> addFieldTo(final JavaClass targetEntity, final Class<?> fieldType, final String fieldName, final Class<? extends java.lang.annotation.Annotation> annotation) throws FileNotFoundException { if (targetEntity.hasField(fieldName)) { throw new IllegalStateException("Entity already has a field named [" + fieldName + "]"); } JavaSourceFacet java = project.getFacet(JavaSourceFacet.class); Field<JavaClass> field = targetEntity.addField(); field.setName(fieldName).setPrivate().setType(fieldType).addAnnotation(annotation); Class<?> fieldTypeForImport = fieldType; if (fieldType.getComponentType() != null) { fieldTypeForImport = fieldType.getComponentType(); } if (!fieldTypeForImport.getCanonicalName().startsWith("java.lang.") && !fieldTypeForImport.isPrimitive() && !fieldTypeForImport.getCanonicalName().equals(targetEntity.getCanonicalName())) { targetEntity.addImport(fieldTypeForImport); } Refactory.createGetterAndSetter(targetEntity, field); updateToString(targetEntity); java.saveJavaSource(targetEntity); shell.println("Added field to " + targetEntity.getQualifiedName() + ": " + field); return field; }
@Command("new-bean") public void newBean( @Option(required = true, name = "type") final JavaResource resource, @Option(required = true, name = "scoped") final BeanScope scope, @Option(required = false, name = "overwrite") final boolean overwrite ) throws FileNotFoundException { if (resource.exists() && !overwrite) { throw new RuntimeException("Type already exists [" + resource.getFullyQualifiedName() + "] Re-run with '--overwrite' to continue."); } JavaSourceFacet java = project.getFacet(JavaSourceFacet.class); JavaClass javaClass = JavaParser.create(JavaClass.class); javaClass.setName(java.calculateName(resource)); javaClass.setPackage(java.calculatePackage(resource)); if (BeanScope.CUSTOM == scope) { String annoType = prompt.promptCommon("Enter the qualified custom scope type:", PromptType.JAVA_CLASS); javaClass.addAnnotation(annoType); } else if (BeanScope.DEPENDENT != scope) { javaClass.addAnnotation(scope.getAnnotation()); } java.saveJavaSource(javaClass); pickup.fire(new PickupResource(java.getJavaResource(javaClass))); }
PromptType.JAVA_PACKAGE, javaSourceFacet.getBasePackage()); JavaResource resource = javaSourceFacet.getJavaResource(javaClass); if (!resource.exists()) javaSourceFacet.saveJavaSource(ejb); shell.setCurrentResource(resource);
static JavaSource<?> sourceForName(final Project project, final String type) { try { JavaSourceFacet javaSourceFact = project.getFacet(JavaSourceFacet.class); return javaSourceFact.getJavaResource(type).getJavaSource(); } catch (FileNotFoundException e) { // Not a Forge-based type return null; } }
viewBean.setPackage(java.getBasePackage() + ".view"); result.add(ScaffoldUtil.createOrOverwrite(this.prompt, java.getJavaResource(viewBean), viewBean.toString(), overwrite)); result.add(ScaffoldUtil.createOrOverwrite(this.prompt, java.getJavaResource(viewUtils), viewUtils.toString(), true));
DirectoryResource sourceFolder = facet.getSourceFolder(); createTopLevelPackage(sourceFolder, javaPackage); if (createMain) facet.saveJavaSource(JavaParser .create(JavaClass.class) .setPackage(javaPackage)
@Override public String getEntityPackage() { JavaSourceFacet sourceFacet = project.getFacet(JavaSourceFacet.class); return sourceFacet.getBasePackage() + "." + DEFAULT_ENTITY_PACKAGE; }
@Override public DirectoryResource getEntityPackageDir() { JavaSourceFacet sourceFacet = project.getFacet(JavaSourceFacet.class); DirectoryResource entityRoot = sourceFacet.getBasePackageResource().getChildDirectory(DEFAULT_ENTITY_PACKAGE); if (!entityRoot.exists()) { entityRoot.mkdirs(); } return entityRoot; }
java.saveJavaSource(entity);
@Command("new-qualifier") public void newQualifier( @Option(required = true, name = "type") final JavaResource resource, @Option(required = false, name = "overwrite") final boolean overwrite, @Option(required = false, name = "inherited") final boolean inherited ) throws FileNotFoundException { if (resource.exists() && !overwrite) { throw new RuntimeException("Type already exists [" + resource.getFullyQualifiedName() + "] Re-run with '--overwrite' to continue."); } JavaSourceFacet java = project.getFacet(JavaSourceFacet.class); JavaAnnotation qualifier = JavaParser.create(JavaAnnotation.class); qualifier.setName(java.calculateName(resource)); qualifier.setPackage(java.calculatePackage(resource)); qualifier.addAnnotation(Qualifier.class); if (inherited) { qualifier.addAnnotation(Inherited.class); } qualifier.addAnnotation(Retention.class).setEnumValue(RUNTIME); qualifier.addAnnotation(Target.class).setEnumValue(METHOD, FIELD, PARAMETER, TYPE); qualifier.addAnnotation(Documented.class); java.saveJavaSource(qualifier); pickup.fire(new PickupResource(java.getJavaResource(qualifier))); }
static JavaSource<?> sourceForName(final Project project, final String type) { try { JavaSourceFacet javaSourceFact = project.getFacet(JavaSourceFacet.class); return javaSourceFact.getJavaResource(type).getJavaSource(); } catch (FileNotFoundException e) { // Not a Forge-based type return null; } }
public String getPackageName() { if (project.hasFacet(RestApplicationFacet.class)) { return configuration.getString(REST_APPLICATIONCLASS_PACKAGE); } else { return java.getBasePackage() + ".rest"; } }
@Command(value = "Valid", help = "Adds @Valid constraint on the specified property") public void addValidConstraint( @Option(name = "onProperty", completer = PropertyCompleter.class, required = true) String property, @Option(name = "onAccessor", flagOnly = true) boolean onAccessor) throws FileNotFoundException { final Annotation<JavaClass> constraint = addConstraintOnProperty(property, onAccessor, Valid.class); javaSourceFacet.saveJavaSource(constraint.getOrigin()); outputConstraintAdded(property, Valid.class); }
/** * Registers the root DTO created for a JPA entity * * @param entity The JPA entity * @param rootDTO The root DTO created for the JPA entity */ public void addRootDTO(JavaClass entity, JavaClass rootDTO) { JavaSourceFacet java = this.project.getFacet(JavaSourceFacet.class); DTOPair dtoPair = dtos.containsKey(entity) ? dtos.get(entity) : new DTOPair(); if (rootDTO != null) { try { if (!java.getJavaResource(rootDTO).exists() || prompt.promptBoolean("DTO [" + rootDTO.getQualifiedName() + "] already, exists. Overwrite?")) { dtoPair.rootDTO = java.saveJavaSource(rootDTO); } } catch (FileNotFoundException fileEx) { throw new RuntimeException(fileEx); } } dtos.put(entity, dtoPair); }
scope.setName(java.calculateName(resource)); scope.setPackage(java.calculatePackage(resource)); scope.addAnnotation(Documented.class); java.saveJavaSource(scope); pickup.fire(new PickupResource(java.getJavaResource(scope)));
private JavaClass tryGetJavaClass(String qualifiedFieldType) { try { JavaResource javaResource = java.getJavaResource(Packages.toFileSyntax(qualifiedFieldType)); JavaSource<?> javaSource = javaResource.getJavaSource(); if (javaSource instanceof JavaClass) { return (JavaClass) javaSource; } } catch (FileNotFoundException fileEx) { // Ignore, since the source file may not be available } return null; } }
public String convert(final PromptType promptType, String value) { Project project = projectInstance.get(); if (value != null) { if (PromptType.JAVA_CLASS.equals(promptType)) { if (value.startsWith("~.") && (project != null)) { if (project.hasFacet(JavaSourceFacet.class)) { value = value.replaceFirst("~", project.getFacet(JavaSourceFacet.class).getBasePackage()); } } } else if (PromptType.JAVA_PACKAGE.equals(promptType)) { if (value.startsWith("~.") && (project != null)) { if (project.hasFacet(JavaSourceFacet.class)) { value = value.replaceFirst("~", project.getFacet(JavaSourceFacet.class).getBasePackage()); } } } } return value; }