Skip to content

Commit

Permalink
#8 improvements and bugfixes in jdt manipulator
Browse files Browse the repository at this point in the history
  • Loading branch information
doczir committed Dec 11, 2015
1 parent fbeb562 commit bb2ffd9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.incquerylabs.evm.jdt.jdtmanipulator

import com.incquerylabs.evm.jdt.fqnutil.QualifiedName
import org.eclipse.jdt.core.IField
import org.eclipse.jdt.core.IType

interface IJDTManipulator {

def void createPackage(QualifiedName qualifiedName)
def void createClass(QualifiedName qualifiedName)
def void createField(QualifiedName containerName, String fieldName, QualifiedName type)
def IType createClass(QualifiedName qualifiedName)
def IField createField(QualifiedName containerName, String fieldName, QualifiedName type)
def void createMethod(QualifiedName qualifiedName)

def void deletePackage(QualifiedName qualifiedName)
Expand All @@ -15,16 +17,7 @@ interface IJDTManipulator {
def void deleteMethod(QualifiedName qualifiedName)

def void changePackageName(QualifiedName oldQualifiedName, String name)
def void changeClassName(QualifiedName oldQualifiedName, String name)
def void changeFieldName(QualifiedName oldQualifiedName, String name)
def void updateClass(QualifiedName oldQualifiedName, String name)
def void updateField(QualifiedName oldQualifiedName, QualifiedName type, String name)
def void changeMethodName(QualifiedName oldQualifiedName, String name)

def void changeFieldVisibility(QualifiedName qualifiedName, Visibility visibility)
def void changeMethodVisibility(QualifiedName qualifiedName, Visibility visibility)

def void changeClassAbstract(QualifiedName qualifiedName, boolean isAbstract)
def void changeMethodAbstract(QualifiedName qualifiedName, boolean isAbstract)

def void changeFieldFinal(QualifiedName qualifiedName, boolean isFinal)
def void changeMethodFinal(QualifiedName qualifiedName, boolean isFinal)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ package com.incquerylabs.evm.jdt.jdtmanipulator.impl
import com.incquerylabs.evm.jdt.fqnutil.IJDTElementLocator
import com.incquerylabs.evm.jdt.fqnutil.QualifiedName
import com.incquerylabs.evm.jdt.jdtmanipulator.IJDTManipulator
import com.incquerylabs.evm.jdt.jdtmanipulator.Visibility
import org.eclipse.core.runtime.NullProgressMonitor
import org.eclipse.jdt.core.ICompilationUnit
import org.eclipse.jdt.core.IJavaElement
import org.eclipse.jdt.core.IJavaModel
import org.eclipse.jdt.core.IJavaProject
import org.eclipse.jdt.core.dom.AST
import org.eclipse.jdt.core.dom.ASTParser
import org.eclipse.jdt.core.dom.CompilationUnit
import org.eclipse.jdt.core.dom.ASTVisitor
import org.eclipse.jdt.core.dom.FieldDeclaration
import org.eclipse.jdt.core.dom.VariableDeclarationFragment
import org.eclipse.jface.text.Document

class JDTManipulator implements IJDTManipulator {

// TODO: move this to a common config
static val GENERATION_FOLDER = "src"

static val ASSOCIATION_REQUIRES_QUALIFIED_NAME = "Association name must be a qualified name. Provider: {%s}"

IJavaProject rootProject
IJDTElementLocator elementLocator

Expand All @@ -25,25 +30,35 @@ class JDTManipulator implements IJDTManipulator {
}

override def createClass(QualifiedName qualifiedName) {
val clazz = elementLocator.locateClass(qualifiedName)
if(clazz != null)
return clazz
val genFolder = rootProject.project.getFolder(GENERATION_FOLDER)
val packageRoot = rootProject.getPackageFragmentRoot(genFolder)
val packageName = qualifiedName.parent.map[toString].orElse("")

val package = packageRoot.createPackageFragment(packageName, false, new NullProgressMonitor)
package.createCompilationUnit(qualifiedName.name + ".java", getClassBody(packageName, qualifiedName.name), false, new NullProgressMonitor)
val cu = package.createCompilationUnit(qualifiedName.name + ".java", getClassBody(packageName, qualifiedName.name), false, new NullProgressMonitor)
return cu.types.head
}

private def getClassBody(String packageName, String className) {
'''«IF packageName != ""»package «packageName»;«ENDIF»
'''
«IF packageName != ""»package «packageName»;«ENDIF»
public class «className» {
}
}
'''.toString
}

override def createField(QualifiedName containerName, String fieldName, QualifiedName type) {
elementLocator.locateClass(containerName).createField('''«type.toString» «fieldName»;''', null, false, new NullProgressMonitor)
val clazz = elementLocator.locateClass(containerName)
val field = clazz.fields.findFirst[it.elementName == fieldName]
if(field != null) {
return field
}
return clazz.createField('''«type.toString» «fieldName»;''', null, false, new NullProgressMonitor)
}

override createPackage(QualifiedName qualifiedName) {
Expand All @@ -70,70 +85,70 @@ class JDTManipulator implements IJDTManipulator {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
}

override changeMethodVisibility(QualifiedName qualifiedName, Visibility visibility) {
override changePackageName(QualifiedName oldQualifiedName, String name) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
}

override changeClassAbstract(QualifiedName qualifiedName, boolean isAbstract) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
override updateClass(QualifiedName oldQualifiedName, String name) {
val clazz = elementLocator.locateClass(oldQualifiedName)
clazz.rename(name, true, new NullProgressMonitor)
}

override changeMethodAbstract(QualifiedName qualifiedName, boolean isAbstract) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
override updateField(QualifiedName oldQualifiedName, QualifiedName type, String name) {
val field = elementLocator.locateFieldNode(oldQualifiedName)

val astRoot = field.compilationUnit.createRecordingASTRoot
astRoot.accept(new ASTVisitor {

override visit(FieldDeclaration node) {
val varDeclaration = node.fragments.head as VariableDeclarationFragment
if(varDeclaration.name.toString.equals(oldQualifiedName.name)) {
varDeclaration.name = node.AST.newSimpleName(name)
node.type = node.AST.newSimpleType(type.toJDTQualifiedName(node.AST))
}

return false;
}

})

astRoot.saveToCompilationUnit(field.compilationUnit)
}

override changeFieldFinal(QualifiedName qualifiedName, boolean isFinal) {
override changeMethodName(QualifiedName oldQualifiedName, String name) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
}

override changeMethodFinal(QualifiedName qualifiedName, boolean isFinal) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
private def deleteJavaElement(IJavaElement javaElement) {
val javaModel = javaElement.javaModel as IJavaModel
javaModel.delete(#[javaElement], true, new NullProgressMonitor)
}

override changeFieldVisibility(QualifiedName qualifiedName, Visibility visibility) {
}
private def createRecordingASTRoot(ICompilationUnit cu) {
val parser = ASTParser.newParser(AST.JLS8)
parser.source = cu
val astRoot = parser.createAST(new NullProgressMonitor) as CompilationUnit

override changePackageName(QualifiedName oldQualifiedName, String name) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
}
astRoot.recordModifications

override changeClassName(QualifiedName oldQualifiedName, String name) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
return astRoot
}

override changeFieldName(QualifiedName oldQualifiedName, String name) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
}

override changeMethodName(QualifiedName oldQualifiedName, String name) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
private def toJDTQualifiedName(QualifiedName qn, AST ast) {
qn.toList.reverse.fold(null) [ seed, it |
if (seed == null)
return ast.newSimpleName(it)
else
ast.newQualifiedName(seed, ast.newSimpleName(it))
]
}

private def deleteJavaElement(IJavaElement javaElement) {
val javaModel = javaElement.javaModel as IJavaModel
javaModel.delete(#[javaElement], true, new NullProgressMonitor)
private def saveToCompilationUnit(CompilationUnit astCompilationUnit, ICompilationUnit jmCompilationUnit) {
val document = new Document(jmCompilationUnit.source)
val edits = astCompilationUnit.rewrite(document, jmCompilationUnit.javaProject.getOptions(true))
edits.apply(document)
jmCompilationUnit.buffer.contents = document.get
jmCompilationUnit.save(new NullProgressMonitor, true)
}

// private def createRecordingASTRoot(ICompilationUnit cu) {
// val parser = ASTParser.newParser(AST.JLS8)
// parser.source = cu
// val astRoot = parser.createAST(new NullProgressMonitor) as CompilationUnit
//
// astRoot.recordModifications
//
// return astRoot
// }

// private def toJDTQualifiedName(QualifiedName qn, AST ast) {
// qn.toList.reverse.fold(null) [ seed, it |
// if (seed == null)
// return ast.newSimpleName(it)
// else
// ast.newQualifiedName(seed, ast.newSimpleName(it))
// ]
// }

// private def saveToCompilationUnit(CompilationUnit astCompilationUnit, ICompilationUnit jmCompilationUnit) {
// }


}

0 comments on commit bb2ffd9

Please sign in to comment.