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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations