Java Code Examples for org.netbeans.api.java.source.JavaSource#getFileObjects()

The following examples show how to use org.netbeans.api.java.source.JavaSource#getFileObjects() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Resolvers.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static FileObject getFileForJavaSource(@NonNull final JavaSource js) {
    final Collection<FileObject> fos = js.getFileObjects();
    if (fos.size() != 1) {
        throw new IllegalArgumentException(
            String.format("Expecting 1 file, got: %d files.",fos.size())); //NOI18N
    }
    return fos.iterator().next();
}
 
Example 2
Source File: GenericResourceGenerator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Set<FileObject> generate(ProgressHandle pHandle) throws IOException {
    initProgressReporting(pHandle, false);
    
    reportProgress(NbBundle.getMessage(GenericResourceGenerator.class,
            "MSG_GeneratingClass", bean.getPackageName() + "." + bean.getName()));  //NOI18N
    JavaSource source;
    if (bean.isRootResource()) {
        source = JavaSourceHelper.createJavaSource(
                getTemplate(), getDestDir(), bean.getPackageName(), bean.getName());
    } else {
        Map<String,String> params = new HashMap<String,String>();
        String[] uriParams = bean.getUriParams();
        StringBuilder fieldList = new StringBuilder();
        StringBuilder paramList = new StringBuilder();
        StringBuilder assignmentList = new StringBuilder();
        StringBuilder argumentList = new StringBuilder();
        for (int i=0; i<uriParams.length; i++) {
            String param = uriParams[i];
            if (i == 0) {
                fieldList.append("private String "); //NOI18N
            } else {
                fieldList.append(", "); //NOI18N
                paramList.append(", "); //NOI18N
                argumentList.append(", "); //NOI18N
                assignmentList.append(" "); //NOI18N
            }
            fieldList.append(param);
            argumentList.append(param);
            paramList.append("String ").append(param); //NOI18N
            assignmentList.append("this.").append(param).append("=").append(param).append(";"); //NOI18N
        }
        if (fieldList.length() > 0) {
            fieldList.append(";");  //NOI18N
        }
        params.put(FIELD_LIST, fieldList.toString());
        params.put(PARAM_LIST, paramList.toString());
        params.put(ASSIGNMENT_LIST, assignmentList.toString());
        params.put(ARGUMENT_LIST, argumentList.toString());
        source = JavaSourceHelper.createJavaSource(
                getTemplate(), params, getDestDir(), bean.getPackageName(), bean.getName());
    }
    if (bean.getInputParameters().size() > 0) {
        addInputParamFields(source);
        addConstructorWithInputParams(source);
    }
    
    modifyResourceClass(source);
    return new HashSet<FileObject>(source.getFileObjects());
}
 
Example 3
Source File: ApplicationSubclassGenerator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void reconfigApplicationClass(String appClassFqn, final Collection<String> classNames) throws IOException {
    JavaSource javaSource = MiscPrivateUtilities.getJavaSourceFromClassName(restSupport.getProject(), appClassFqn);
    if ( javaSource == null ){
        return;
    }
    ModificationResult res = javaSource.runModificationTask( new Task<WorkingCopy>() {

        @Override
        public void run(WorkingCopy workingCopy) throws Exception {
            workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
            CompilationUnitTree tree = workingCopy.getCompilationUnit();
            for (Tree typeDeclaration : tree.getTypeDecls()){
                if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){
                    MethodTree getClasses = null;
                    MethodTree restResources = null;
                    MethodTree restResources2 = null;
                    ClassTree classTree = (ClassTree) typeDeclaration;
                    List<? extends Tree> members = classTree.getMembers();
                    for (Tree member : members) {
                        if ( member.getKind().equals(Tree.Kind.METHOD)){
                            MethodTree method = (MethodTree)member;
                            String name = method.getName().toString();
                            if ( name.equals(RestConstants.GET_CLASSES)){
                                getClasses = method;
                            }
                            else if ( name.equals(GET_REST_RESOURCE_CLASSES)){
                                restResources = method;
                            } else if ( name.equals(RestConstants.GET_REST_RESOURCE_CLASSES2)){
                                restResources2 = method;
                            }
                        }
                    }
                    TreeMaker maker = workingCopy.getTreeMaker();
                    ClassTree modified = classTree;

                    if (getClasses != null && restResources != null) {
                        // this is old code generator replaced in NB 7.3.1
                        // as part of EE7 upgrade:
                        modified = removeResourcesMethod( restResources,
                                maker, modified);
                        modified = createMethodsOlderVersion(classNames,
                                maker, modified, workingCopy);
                    } else {
                        if (restResources2 != null) {
                            modified = removeResourcesMethod( restResources2,
                                    maker, modified);
                            modified = createMethods(classNames, getClasses,
                                    maker, modified, workingCopy);
                        }
                    }

                    workingCopy.rewrite(classTree, modified);
                }
            }
        }

    });
    res.commit();

    Collection<FileObject> files = javaSource.getFileObjects();
    if ( files.isEmpty() ){
        return;
    }
    FileObject fileObject = files.iterator().next();
    DataObject dataObject = DataObject.find(fileObject);
    if ( dataObject!= null){
        SaveCookie cookie = dataObject.getLookup().lookup(SaveCookie.class);
        if ( cookie!= null ){
            cookie.save();
        }
    }
}