Search in sources :

Example 6 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class EclipseShellCallback method getJavaProject.

private IJavaProject getJavaProject(String javaProjectName) throws ShellException {
    IJavaProject javaProject = projects.get(javaProjectName);
    if (javaProject == null) {
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IProject project = root.getProject(javaProjectName);
        if (project.exists()) {
            boolean isJavaProject;
            try {
                isJavaProject = project.hasNature(JavaCore.NATURE_ID);
            } catch (CoreException e) {
                throw new ShellException(e.getStatus().getMessage(), e);
            }
            if (isJavaProject) {
                javaProject = JavaCore.create(project);
            } else {
                StringBuffer sb = new StringBuffer();
                sb.append("Project ");
                sb.append(javaProjectName);
                sb.append(" is not a Java project");
                throw new ShellException(sb.toString());
            }
        } else {
            StringBuffer sb = new StringBuffer();
            sb.append("Project ");
            sb.append(javaProjectName);
            sb.append(" does not exist");
            throw new ShellException(sb.toString());
        }
        projects.put(javaProjectName, javaProject);
    }
    return javaProject;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) ShellException(org.mybatis.generator.exception.ShellException) IProject(org.eclipse.core.resources.IProject)

Example 7 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class DefaultShellCallback method getDirectory.

/* (non-Javadoc)
     * @see org.mybatis.generator.api.ShellCallback#getDirectory(java.lang.String, java.lang.String)
     */
public File getDirectory(String targetProject, String targetPackage) throws ShellException {
    // targetProject is interpreted as a directory that must exist
    //
    // targetPackage is interpreted as a sub directory, but in package
    // format (with dots instead of slashes). The sub directory will be
    // created
    // if it does not already exist
    File project = new File(targetProject);
    if (!project.isDirectory()) {
        throw new ShellException(getString(//$NON-NLS-1$
        "Warning.9", targetProject));
    }
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    StringTokenizer st = new StringTokenizer(targetPackage, ".");
    while (st.hasMoreTokens()) {
        sb.append(st.nextToken());
        sb.append(File.separatorChar);
    }
    File directory = new File(project, sb.toString());
    if (!directory.isDirectory()) {
        boolean rc = directory.mkdirs();
        if (!rc) {
            throw new ShellException(getString(//$NON-NLS-1$
            "Warning.10", directory.getAbsolutePath()));
        }
    }
    return directory;
}
Also used : StringTokenizer(java.util.StringTokenizer) File(java.io.File) ShellException(org.mybatis.generator.exception.ShellException)

Example 8 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class MavenShellCallback method getDirectory.

@Override
public File getDirectory(String targetProject, String targetPackage) throws ShellException {
    if (!"MAVEN".equals(targetProject)) {
        return super.getDirectory(targetProject, targetPackage);
    }
    // targetProject is the output directory from the MyBatis generator
    // Mojo. It will be created if necessary
    //
    // targetPackage is interpreted as a sub directory, but in package
    // format (with dots instead of slashes).  The sub directory will be created
    // if it does not already exist
    File project = mybatisGeneratorMojo.getOutputDirectory();
    if (!project.exists()) {
        project.mkdirs();
    }
    if (!project.isDirectory()) {
        throw new ShellException(//$NON-NLS-1$
        Messages.getString(//$NON-NLS-1$
        "Warning.9", project.getAbsolutePath()));
    }
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    StringTokenizer st = new StringTokenizer(targetPackage, ".");
    while (st.hasMoreTokens()) {
        sb.append(st.nextToken());
        sb.append(File.separatorChar);
    }
    File directory = new File(project, sb.toString());
    if (!directory.isDirectory()) {
        boolean rc = directory.mkdirs();
        if (!rc) {
            throw new ShellException(//$NON-NLS-1$
            Messages.getString(//$NON-NLS-1$
            "Warning.10", directory.getAbsolutePath()));
        }
    }
    return directory;
}
Also used : StringTokenizer(java.util.StringTokenizer) File(java.io.File) ShellException(org.mybatis.generator.exception.ShellException)

Example 9 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class JavaFileMerger method getMergedSource.

@SuppressWarnings({ "unchecked", "rawtypes" })
public String getMergedSource() throws ShellException, InvalidExistingFileException {
    NewJavaFileVisitor newJavaFileVisitor = visitNewJavaFile();
    IDocument document = new Document(existingJavaSource);
    // delete generated stuff, and collect imports
    ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(javaDocTags);
    CompilationUnit cu = getCompilationUnitFromSource(existingJavaSource);
    AST ast = cu.getAST();
    cu.recordModifications();
    cu.accept(visitor);
    TypeDeclaration typeDeclaration = visitor.getTypeDeclaration();
    if (typeDeclaration == null) {
        throw new InvalidExistingFileException(ErrorCode.NO_TYPES_DEFINED_IN_FILE);
    }
    // reconcile the superinterfaces
    List<Type> newSuperInterfaces = getNewSuperInterfaces(typeDeclaration.superInterfaceTypes(), newJavaFileVisitor);
    for (Type newSuperInterface : newSuperInterfaces) {
        typeDeclaration.superInterfaceTypes().add(ASTNode.copySubtree(ast, newSuperInterface));
    }
    // set the superclass
    if (newJavaFileVisitor.getSuperclass() != null) {
        typeDeclaration.setSuperclassType((Type) ASTNode.copySubtree(ast, newJavaFileVisitor.getSuperclass()));
    } else {
        typeDeclaration.setSuperclassType(null);
    }
    // interface or class?
    if (newJavaFileVisitor.isInterface()) {
        typeDeclaration.setInterface(true);
    } else {
        typeDeclaration.setInterface(false);
    }
    // reconcile the imports
    List<ImportDeclaration> newImports = getNewImports(cu.imports(), newJavaFileVisitor);
    for (ImportDeclaration newImport : newImports) {
        Name name = ast.newName(newImport.getName().getFullyQualifiedName());
        ImportDeclaration newId = ast.newImportDeclaration();
        newId.setName(name);
        cu.imports().add(newId);
    }
    TextEdit textEdit = cu.rewrite(document, null);
    try {
        textEdit.apply(document);
    } catch (BadLocationException e) {
        throw new ShellException("BadLocationException removing prior fields and methods");
    }
    // regenerate the CompilationUnit to reflect all the deletes and changes
    CompilationUnit strippedCu = getCompilationUnitFromSource(document.get());
    // find the top level public type declaration
    TypeDeclaration topLevelType = null;
    Iterator iter = strippedCu.types().iterator();
    while (iter.hasNext()) {
        TypeDeclaration td = (TypeDeclaration) iter.next();
        if (td.getParent().equals(strippedCu) && (td.getModifiers() & Modifier.PUBLIC) > 0) {
            topLevelType = td;
            break;
        }
    }
    // now add all the new methods and fields to the existing
    // CompilationUnit with a ListRewrite
    ASTRewrite rewrite = ASTRewrite.create(topLevelType.getRoot().getAST());
    ListRewrite listRewrite = rewrite.getListRewrite(topLevelType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
    Iterator<ASTNode> astIter = newJavaFileVisitor.getNewNodes().iterator();
    int i = 0;
    while (astIter.hasNext()) {
        ASTNode node = astIter.next();
        if (node.getNodeType() == ASTNode.TYPE_DECLARATION) {
            String name = ((TypeDeclaration) node).getName().getFullyQualifiedName();
            if (visitor.containsInnerClass(name)) {
                continue;
            }
        } else if (node instanceof FieldDeclaration) {
            addExistsAnnotations((BodyDeclaration) node, visitor.getFieldAnnotations((FieldDeclaration) node));
        } else if (node instanceof MethodDeclaration) {
            addExistsAnnotations((BodyDeclaration) node, visitor.getMethodAnnotations((MethodDeclaration) node));
        }
        listRewrite.insertAt(node, i++, null);
    }
    textEdit = rewrite.rewriteAST(document, JavaCore.getOptions());
    try {
        textEdit.apply(document);
    } catch (BadLocationException e) {
        throw new ShellException("BadLocationException adding new fields and methods");
    }
    String newSource = document.get();
    return newSource;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Name(org.eclipse.jdt.core.dom.Name) Iterator(java.util.Iterator) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ShellException(org.mybatis.generator.exception.ShellException) Type(org.eclipse.jdt.core.dom.Type) TextEdit(org.eclipse.text.edits.TextEdit) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 10 with ShellException

use of org.mybatis.generator.exception.ShellException in project generator by mybatis.

the class EclipseShellCallback method getExistingFileContents.

private String getExistingFileContents(File existingFile, String fileEncoding) throws ShellException {
    if (!existingFile.exists()) {
        // this should not happen because MyBatis Generator only returns the
        // file
        // calculated by the eclipse callback
        StringBuilder sb = new StringBuilder();
        sb.append("The file ");
        sb.append(existingFile.getAbsolutePath());
        sb.append(" does not exist");
        throw new ShellException(sb.toString());
    }
    try {
        StringBuilder sb = new StringBuilder();
        FileInputStream fis = new FileInputStream(existingFile);
        InputStreamReader isr;
        if (fileEncoding == null) {
            isr = new InputStreamReader(fis);
        } else {
            isr = new InputStreamReader(fis, fileEncoding);
        }
        BufferedReader br = new BufferedReader(isr);
        char[] buffer = new char[1024];
        int returnedBytes = br.read(buffer);
        while (returnedBytes != -1) {
            sb.append(buffer, 0, returnedBytes);
            returnedBytes = br.read(buffer);
        }
        br.close();
        return sb.toString();
    } catch (IOException e) {
        StringBuilder sb = new StringBuilder();
        sb.append("IOException reading the file ");
        sb.append(existingFile.getAbsolutePath());
        throw new ShellException(sb.toString(), e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ShellException(org.mybatis.generator.exception.ShellException) FileInputStream(java.io.FileInputStream)

Aggregations

ShellException (org.mybatis.generator.exception.ShellException)12 File (java.io.File)4 CoreException (org.eclipse.core.runtime.CoreException)4 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)3 StringTokenizer (java.util.StringTokenizer)2 IFolder (org.eclipse.core.resources.IFolder)2 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 IProject (org.eclipse.core.resources.IProject)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 Path (org.eclipse.core.runtime.Path)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1