use of org.eclipse.jdt.internal.core.SourceType in project che by eclipse.
the class SourceTypeConverter method convert.
/*
* Convert an initializerinfo into a parsed initializer declaration
*/
private Initializer convert(InitializerElementInfo initializerInfo, CompilationResult compilationResult) throws JavaModelException {
Block block = new Block(0);
Initializer initializer = new Initializer(block, ClassFileConstants.AccDefault);
int start = initializerInfo.getDeclarationSourceStart();
int end = initializerInfo.getDeclarationSourceEnd();
initializer.sourceStart = initializer.declarationSourceStart = start;
initializer.sourceEnd = initializer.declarationSourceEnd = end;
initializer.modifiers = initializerInfo.getModifiers();
/* convert local and anonymous types */
IJavaElement[] children = initializerInfo.getChildren();
int typesLength = children.length;
if (typesLength > 0) {
Statement[] statements = new Statement[typesLength];
for (int i = 0; i < typesLength; i++) {
SourceType type = (SourceType) children[i];
TypeDeclaration localType = convert(type, compilationResult);
if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
expression.type = localType.superclass;
localType.superclass = null;
localType.superInterfaces = null;
localType.allocation = expression;
statements[i] = expression;
} else {
statements[i] = localType;
}
}
block.statements = statements;
}
return initializer;
}
use of org.eclipse.jdt.internal.core.SourceType in project che by eclipse.
the class SourceTypeConverter method convert.
/*
* Convert a set of source element types into a parsed compilation unit declaration
* The argument types are then all grouped in the same unit. The argument types must
* at least contain one type.
*/
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException {
this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
if (sourceTypes.length == 0)
return this.unit;
SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
this.cu = (ICompilationUnit) cuHandle;
final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo();
if (this.has1_5Compliance && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) {
// Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843
if ((this.flags & LOCAL_TYPE) == 0) {
return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
} else {
return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
}
}
/* only positions available */
int start = topLevelTypeInfo.getNameSourceStart();
int end = topLevelTypeInfo.getNameSourceEnd();
/* convert package and imports */
String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
if (packageName.length > 0)
// if its null then it is defined in the default package
this.unit.currentPackage = createImportReference(packageName, start, end, false, ClassFileConstants.AccDefault);
IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
int importCount = importDeclarations.length;
this.unit.imports = new ImportReference[importCount];
for (int i = 0; i < importCount; i++) {
ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
String nameWithoutStar = importDeclaration.getNameWithoutStar();
this.unit.imports[i] = createImportReference(Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()), sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(), importDeclaration.isOnDemand(), sourceImport.getModifiers());
}
/* convert type(s) */
try {
int typeCount = sourceTypes.length;
final TypeDeclaration[] types = new TypeDeclaration[typeCount];
/*
* We used a temporary types collection to prevent this.unit.types from being null during a call to
* convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
* See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
*/
for (int i = 0; i < typeCount; i++) {
SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
}
this.unit.types = types;
return this.unit;
} catch (AnonymousMemberFound e) {
return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
}
}
use of org.eclipse.jdt.internal.core.SourceType in project jbosstools-hibernate by jbosstools.
the class CollectEntityInfo method visit.
public boolean visit(TypeDeclaration node) {
ITypeBinding typeBinding = node.resolveBinding();
String nodeName = typeBinding == null ? null : typeBinding.getBinaryName();
if (fullyQualifiedName == null || !fullyQualifiedName.equalsIgnoreCase(nodeName)) {
return false;
}
boolean isAbstruct = entityInfo.isAbstractFlag() || Modifier.isAbstract(node.getModifiers()) || node.isInterface();
entityInfo.setAbstractFlag(isAbstruct);
if (isAbstruct) {
entityInfo.setAddEntityFlag(false);
entityInfo.setAddMappedSuperclassFlag(true);
}
entityInfo.setInterfaceFlag(node.isInterface());
Type superType = node.getSuperclassType();
if (superType != null) {
ITypeBinding tb = superType.resolveBinding();
if (tb != null) {
// $NON-NLS-1$
String entityFullyQualifiedName = "";
if (tb.getJavaElement() instanceof SourceType) {
SourceType sourceT = (SourceType) tb.getJavaElement();
try {
entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
} catch (JavaModelException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e);
}
}
entityInfo.addDependency(entityFullyQualifiedName);
entityInfo.setFullyQualifiedParentName(entityFullyQualifiedName);
}
}
List<?> superInterfaces = node.superInterfaceTypes();
Iterator<?> it = superInterfaces.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof SimpleType) {
// TODO process interfaces
SimpleType st = (SimpleType) obj;
String fullyQualifiedName = st.getName().getFullyQualifiedName();
if (JPAConst.IMPORT_SERIALIZABLE.compareTo(fullyQualifiedName) == 0) {
entityInfo.setAddSerializableInterfaceFlag(false);
} else if (JPAConst.ANNOTATION_SERIALIZABLE.compareTo(fullyQualifiedName) == 0) {
entityInfo.setAddSerializableInterfaceFlag(false);
entityInfo.addRequiredImport(JPAConst.IMPORT_SERIALIZABLE);
}
}
}
node.resolveBinding();
return true;
}
use of org.eclipse.jdt.internal.core.SourceType in project jbosstools-hibernate by jbosstools.
the class CompilationUnitCollector method processJavaElements.
/**
* Process object - java element to collect all it's children CompilationUnits
* @param obj
* @param bCollect
*/
public void processJavaElements(Object obj, boolean bCollect) {
if (obj instanceof ICompilationUnit) {
ICompilationUnit cu = (ICompilationUnit) obj;
addCompilationUnit(cu, bCollect);
} else if (obj instanceof File) {
File file = (File) obj;
if (file.getProject() != null) {
IJavaProject javaProject = JavaCore.create(file.getProject());
ICompilationUnit[] cus = Utils.findCompilationUnits(javaProject, file.getFullPath());
if (cus != null) {
for (int i = 0; i < cus.length; i++) {
addCompilationUnit(cus[i], bCollect);
}
}
}
} else if (obj instanceof JavaProject) {
JavaProject javaProject = (JavaProject) obj;
IPackageFragmentRoot[] pfr = null;
try {
pfr = javaProject.getAllPackageFragmentRoots();
} catch (JavaModelException e) {
// just ignore it!
// HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
}
if (pfr != null) {
for (int i = 0; i < pfr.length; i++) {
processJavaElements(pfr[i], bCollect);
}
}
} else if (obj instanceof PackageFragment) {
PackageFragment packageFragment = (PackageFragment) obj;
ICompilationUnit[] cus = null;
try {
cus = packageFragment.getCompilationUnits();
} catch (JavaModelException e) {
// just ignore it!
// HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
}
if (cus != null && cus.length > 0) {
if (bCollect) {
selection2UpdateList.add(obj);
bCollect = false;
}
for (int i = 0; i < cus.length; i++) {
addCompilationUnit(cus[i], bCollect);
}
}
} else if (obj instanceof PackageFragmentRoot) {
JavaElement javaElement = (JavaElement) obj;
JavaElementInfo javaElementInfo = null;
try {
javaElementInfo = (JavaElementInfo) javaElement.getElementInfo();
} catch (JavaModelException e) {
// just ignore it!
// HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
}
if (javaElementInfo != null) {
IJavaElement[] je = javaElementInfo.getChildren();
for (int i = 0; i < je.length; i++) {
processJavaElements(je[i], true);
}
}
} else if (obj instanceof JavaElement) {
JavaElement javaElement = (JavaElement) obj;
ICompilationUnit cu = javaElement.getCompilationUnit();
addCompilationUnit(cu, bCollect);
} else if (obj instanceof SourceType) {
if (bCollect) {
selection2UpdateList.add(obj);
bCollect = false;
}
SourceType sourceType = (SourceType) obj;
processJavaElements(sourceType.getJavaModel(), bCollect);
} else {
// ignore
// System.out.println("1 Blah! " + selection); //$NON-NLS-1$
}
}
use of org.eclipse.jdt.internal.core.SourceType in project evosuite by EvoSuite.
the class ExtendSuiteAction method addTestJob.
// @Override
// public void selectionChanged(IAction action, ISelection selection) {
// currentSelection.clear();
//
// if (selection instanceof IStructuredSelection) {
// IStructuredSelection sel = (IStructuredSelection) selection;
//
// for (Object o : sel.toList()) {
// if (o instanceof IJavaElement) {
// IJavaElement jEl = (IJavaElement) o;
// try {
// IResource jRes = jEl.getCorrespondingResource();
// if (jRes != null) {
// jRes.accept(new IResourceVisitor() {
// @Override
// public boolean visit(IResource resource)
// throws CoreException {
// if ("java".equals(resource.getFileExtension()))
// currentSelection.add(resource);
// return true;
// }
// });
// }
// } catch (JavaModelException e) {
// System.err.println("Error while traversing resources!" + e);
// } catch (CoreException e) {
// System.err.println("Error while traversing resources!" + e);
// }
// }
// }
// }
// }
//
// @Override
// public void run(IAction action) {
// if (currentSelection.isEmpty()) {
// MessageDialog.openError(shell, "Evosuite",
// "Unable to generate test cases for selection: Cannot find .java files.");
// } else if (currentSelection.size() > 1) {
// MessageDialog.openError(shell, "Evosuite",
// "Please only select one class at a time.");
// } else {
//
// for (IResource res : currentSelection) {
// IProject proj = res.getProject();
// fixJUnitClassPath(JavaCore.create(proj));
// generateTests(res);
// }
// }
// }
/**
* Add a new test generation job to the job queue
*
* @param target
*/
@Override
protected void addTestJob(final IResource target) {
IJavaElement element = JavaCore.create(target);
IJavaElement packageElement = element.getParent();
String packageName = packageElement.getElementName();
final String suiteClass = (!packageName.equals("") ? packageName + "." : "") + target.getName().replace(".java", "").replace(File.separator, ".");
System.out.println("Building new job for " + suiteClass);
DetermineSUT det = new DetermineSUT();
IJavaProject jProject = JavaCore.create(target.getProject());
try {
String classPath = target.getWorkspace().getRoot().findMember(jProject.getOutputLocation()).getLocation().toOSString();
String SUT = det.getSUTName(suiteClass, classPath);
// choose
SelectionDialog typeDialog = JavaUI.createTypeDialog(shell, new ProgressMonitorDialog(shell), target.getProject(), IJavaElementSearchConstants.CONSIDER_CLASSES, false);
Object[] sutDefault = new Object[1];
sutDefault[0] = SUT;
typeDialog.setInitialSelections(sutDefault);
typeDialog.setTitle("Please select the class under test");
typeDialog.open();
// Type selected by the user
Object[] result = typeDialog.getResult();
if (result.length > 0) {
SourceType sourceType = (SourceType) result[0];
SUT = sourceType.getFullyQualifiedName();
} else {
return;
}
Job job = new TestExtensionJob(shell, target, SUT, suiteClass);
job.setPriority(Job.SHORT);
IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
ISchedulingRule rule = ruleFactory.createRule(target.getProject());
// IFolder folder = proj.getFolder(ResourceUtil.EVOSUITE_FILES);
job.setRule(rule);
job.setUser(true);
// start as soon as possible
job.schedule();
} catch (JavaModelException e) {
e.printStackTrace();
} catch (NoJUnitClassException e) {
MessageDialog.openError(shell, "Evosuite", "Cannot find JUnit tests in " + suiteClass);
}
}
Aggregations