Search in sources :

Example 11 with SourceType

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) SourceType(org.eclipse.jdt.internal.core.SourceType) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) SourceType(org.eclipse.jdt.internal.core.SourceType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 12 with SourceType

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;
}
Also used : NameLookup(org.eclipse.jdt.internal.core.NameLookup) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SourceType(org.eclipse.jdt.internal.core.SourceType) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) AssertionFailedException(org.eclipse.core.runtime.AssertionFailedException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) TransformerException(javax.xml.transform.TransformerException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) PatternSyntaxException(java.util.regex.PatternSyntaxException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IType(org.eclipse.jdt.core.IType)

Example 13 with SourceType

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()]);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SourceType(org.eclipse.jdt.internal.core.SourceType) HashSet(java.util.HashSet) IType(org.eclipse.jdt.core.IType)

Example 14 with SourceType

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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) SourceType(org.eclipse.jdt.internal.core.SourceType) ArrayList(java.util.ArrayList) IMethod(org.eclipse.jdt.core.IMethod) StyledString(org.eclipse.jface.viewers.StyledString) IType(org.eclipse.jdt.core.IType)

Example 15 with SourceType

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);
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) SourceType(org.eclipse.jdt.internal.core.SourceType) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IJavaProject(org.eclipse.jdt.core.IJavaProject) NoJUnitClassException(org.evosuite.junit.DetermineSUT.NoJUnitClassException) DetermineSUT(org.evosuite.junit.DetermineSUT) IResourceRuleFactory(org.eclipse.core.resources.IResourceRuleFactory) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

SourceType (org.eclipse.jdt.internal.core.SourceType)15 JavaModelException (org.eclipse.jdt.core.JavaModelException)9 IJavaElement (org.eclipse.jdt.core.IJavaElement)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IType (org.eclipse.jdt.core.IType)4 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)4 ISourceType (org.eclipse.jdt.internal.compiler.env.ISourceType)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 Type (org.eclipse.jdt.core.dom.Type)3 IResourceRuleFactory (org.eclipse.core.resources.IResourceRuleFactory)2 CoreException (org.eclipse.core.runtime.CoreException)2 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)2 Job (org.eclipse.core.runtime.jobs.Job)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)2 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)2 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)2 SimpleType (org.eclipse.jdt.core.dom.SimpleType)2