use of org.eclipse.jdt.internal.core.SourceType in project jbosstools-hibernate by jbosstools.
the class ProjectUtils method getParentTypename.
public static String getParentTypename(IJavaProject proj, String fullyQualifiedName) {
String res = null;
ICompilationUnit icu = findCompilationUnit(proj, fullyQualifiedName);
if (icu == null) {
return res;
}
org.eclipse.jdt.core.dom.CompilationUnit cu = getCompilationUnit(icu, true);
if (cu == null) {
return res;
}
List<?> types = cu.types();
for (int i = 0; i < types.size() && res == null; i++) {
Object obj = types.get(i);
if (!(obj instanceof TypeDeclaration)) {
continue;
}
TypeDeclaration td = (TypeDeclaration) obj;
Type superType = td.getSuperclassType();
if (superType != null) {
ITypeBinding tb = superType.resolveBinding();
if (tb != null) {
if (tb.getJavaElement() instanceof SourceType) {
SourceType sourceT = (SourceType) tb.getJavaElement();
try {
res = sourceT.getFullyQualifiedParameterizedName();
} catch (JavaModelException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e);
}
}
}
}
}
return res;
}
use of org.eclipse.jdt.internal.core.SourceType in project eclipse.pde by eclipse-pde.
the class Util method findSourceTypeinJavaProject.
/**
* Tries to find SourceType of name typeName in the project. It returns null
* if it cannot find SourceType of name typeName
*
* @param javaProject
* @param typeName
* @return
*/
public static IType findSourceTypeinJavaProject(IJavaProject javaProject, String typeName) {
IType type = null;
try {
String pkgName = typeName.substring(0, typeName.lastIndexOf('.'));
if (javaProject instanceof JavaProject) {
JavaProject jp = (JavaProject) javaProject;
NameLookup newNameLookup = null;
try {
newNameLookup = jp.newNameLookup(DefaultWorkingCopyOwner.PRIMARY);
} catch (JavaModelException e) {
ApiPlugin.log(e);
}
IPackageFragment[] findPackageFragment = newNameLookup.findPackageFragments(pkgName, false);
for (IPackageFragment iJavaElement : findPackageFragment) {
type = newNameLookup.findType(typeName.substring(typeName.lastIndexOf('.') + 1, typeName.length()), iJavaElement, false, NameLookup.ACCEPT_ALL);
if (type instanceof SourceType) {
break;
}
}
}
} catch (Exception e) {
// return null
}
return type;
}
use of org.eclipse.jdt.internal.core.SourceType in project grails-ide by spring-attic.
the class MemberParser method getAllTypes.
/**
*Will search within the given type and all of it's children - including methods
* and anonymous types for other types.
* @param types the scope of the search
* @return all types within the given scope
* @throws JavaModelException
*/
public static IType[] getAllTypes(IType[] types) throws JavaModelException {
if (types == null)
return null;
final Set results = new HashSet();
// get all the obvious type declarations
for (int mainTypeNum = 0; mainTypeNum < types.length; mainTypeNum++) {
IType[] declaredTypes = types[mainTypeNum].getTypes();
for (int declaredTypeNum = 0; declaredTypeNum < declaredTypes.length; declaredTypeNum++) {
results.add(declaredTypes[declaredTypeNum]);
}
// get all the type's method's type declarations
types = getAllTypes(getAllMethods(types));
for (int methodTypes = 0; methodTypes < types.length; methodTypes++) {
results.add(types[methodTypes]);
}
}
if (results.isEmpty())
return null;
// possibly change to new IType
return (IType[]) results.toArray(new SourceType[results.size()]);
}
use of org.eclipse.jdt.internal.core.SourceType in project efxclipse-eclipse by eclipse-efx.
the class JDTHelper method getTypeData.
public TypeData getTypeData(IJavaProject jproject, IType jdtType) {
if (jdtType == null) {
return null;
}
TypeData data = typeCache.get(jdtType.getFullyQualifiedName());
if (data == null) {
try {
List<IMethod> allMethods = new ArrayList<IMethod>();
allMethods.addAll(Arrays.asList(jdtType.getMethods()));
IType parentType = jdtType;
while (parentType != null && parentType.getSuperclassName() != null) {
if (parentType instanceof SourceType) {
String[][] typeDefs = parentType.resolveType(parentType.getSuperclassName());
if (typeDefs != null) {
for (String[] type : typeDefs) {
parentType = jproject.findType(type[0] + "." + type[1]);
}
}
} else {
parentType = jproject.findType(parentType.getSuperclassName());
}
if (parentType != null) {
allMethods.addAll(Arrays.asList(parentType.getMethods()));
}
}
data = createData(allMethods, jproject);
if (!(jdtType instanceof SourceType)) {
typeCache.put(jdtType.getFullyQualifiedName(), data);
}
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return data;
}
use of org.eclipse.jdt.internal.core.SourceType in project evosuite-plus-plus by llmhyy.
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