Java Code Examples for org.eclipse.jdt.core.IType#getJavaProject()
The following examples show how to use
org.eclipse.jdt.core.IType#getJavaProject() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaElExpressionProposalComputer.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 6 votes |
public static JavaElExpressionProposalComputer newUsingJavaType( IType javaType, String followingText, int followingTextOffset) { // This is the type referenced by the first fragment of the EL expression IType snippetReceivingType = javaType; String snippet = followingText; int snippetOffset = followingTextOffset; int lastPeriodIndex = followingText.lastIndexOf('.'); if (lastPeriodIndex != -1) { String textUntilLastPeriod = followingText.substring(0, lastPeriodIndex); // Find the right-most resolvable type, and update our snippet to be the // the text following the fragment that corresponds to that type snippetReceivingType = UiBinderUtilities.resolveJavaElExpression( javaType, textUntilLastPeriod, null); if (snippetReceivingType == null) { return null; } snippet = followingText.substring(lastPeriodIndex + 1); snippetOffset = followingTextOffset + lastPeriodIndex + 1; } return new JavaElExpressionProposalComputer(javaType.getJavaProject(), snippet, snippetOffset, snippet.length(), snippetReceivingType); }
Example 2
Source File: JUnitStatusRegistry.java From jdt-codemining with Eclipse Public License 1.0 | 5 votes |
public ITestSuiteElement findTestSuite(IType element) { IJavaProject project = element.getJavaProject(); String className = element.getFullyQualifiedName(); Map<String, Map<String, ITestCaseElement>> testClasses = projects.get(project); if (testClasses == null) { return null; } Map<String, ITestCaseElement> tests = testClasses.get(className); if (tests == null) { return null; } return (ITestSuiteElement) ((ITestCaseElement) tests.values().toArray()[0]).getParentContainer(); }
Example 3
Source File: RemoteServiceUtilities.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
/** * Returns <code>true</code> if the type is or extends the RemoteService * interface. */ public static boolean isSyncInterface(IType type) throws JavaModelException { IJavaProject javaProject = type.getJavaProject(); if (!GWTNature.isGWTProject(javaProject.getProject())) { return false; } ITypeHierarchy hierarchy = type.newSupertypeHierarchy(null); IType remoteServiceInterface = javaProject.findType(REMOTE_SERVICE_QUALIFIED_NAME); return remoteServiceInterface != null && hierarchy.contains(remoteServiceInterface); }
Example 4
Source File: ClientBundleResource.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
private String getSourceAnnotationValue(IType clientBundle) throws JavaModelException { IJavaProject javaProject = clientBundle.getJavaProject(); assert (javaProject.isOnClasspath(file)); IPackageFragment resourcePckg = javaProject.findPackageFragment(file.getParent().getFullPath()); // If the resource is not in the same package as our ClientBundle, we need // an @Source with the full classpath-relative path to the resource. if (!clientBundle.getPackageFragment().equals(resourcePckg)) { return ResourceUtils.getClasspathRelativePath(resourcePckg, file.getName()).toString(); } // If the resource has a different name than the method, we need an @Source, // although in this case we don't need the full path. String fileNameWithoutExt = ResourceUtils.filenameWithoutExtension(file); if (!ResourceUtils.areFilenamesEqual(fileNameWithoutExt, methodName)) { return file.getName(); } // If resource doesn't have one of the default extensions, we need @Source. IType resourceType = JavaModelSearch.findType(javaProject, resourceTypeName); if (!hasDefaultExtension(file, resourceType)) { return file.getName(); } // If the resource is in ClientBundle package and its name (without file // extension) matches the method name, no need for @Source return null; }
Example 5
Source File: PropertiesQuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static boolean isEclipseNLSUsed(IType accessor) { IJavaProject javaProject= accessor.getJavaProject(); if (javaProject == null || !javaProject.exists()) return false; try { IType nls= javaProject.findType("org.eclipse.osgi.util.NLS"); //$NON-NLS-1$ if(nls==null) return false; ITypeHierarchy supertypeHierarchy= accessor.newSupertypeHierarchy(null); return supertypeHierarchy.contains(nls); } catch (JavaModelException e) { return false; } }
Example 6
Source File: ResolveClasspathsHandler.java From java-debug with Eclipse Public License 1.0 | 4 votes |
/** * Get java project from type. * * @param fullyQualifiedTypeName * fully qualified name of type * @return java project * @throws CoreException * CoreException */ public static List<IJavaProject> getJavaProjectFromType(String fullyQualifiedTypeName) throws CoreException { // If only one Java project exists in the whole workspace, return the project directly. List<IJavaProject> javaProjects = JdtUtils.listJavaProjects(ResourcesPlugin.getWorkspace().getRoot()); if (javaProjects.size() <= 1) { return javaProjects; } String[] splitItems = fullyQualifiedTypeName.split("/"); // If the main class name contains the module name, should trim the module info. if (splitItems.length == 2) { fullyQualifiedTypeName = splitItems[1]; } final String moduleName = splitItems.length == 2 ? splitItems[0] : null; final String className = fullyQualifiedTypeName; SearchPattern pattern = SearchPattern.createPattern( fullyQualifiedTypeName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH); IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); ArrayList<IJavaProject> projects = new ArrayList<>(); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) { Object element = match.getElement(); if (element instanceof IType) { IType type = (IType) element; IJavaProject project = type.getJavaProject(); if (className.equals(type.getFullyQualifiedName()) && (moduleName == null || moduleName.equals(JdtUtils.getModuleName(project)))) { projects.add(project); } } } }; SearchEngine searchEngine = new SearchEngine(); searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null /* progress monitor */); return projects.stream().distinct().collect(Collectors.toList()); }
Example 7
Source File: JdtBasedTypeFactory.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
@Override public JvmDeclaredType createType(IType jdtType) { IJavaProject project = jdtType.getJavaProject(); return createType(jdtType, project); }
Example 8
Source File: PushDownRefactoringProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * {@inheritDoc} */ @Override public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { try { final Map<String, String> arguments= new HashMap<String, String>(); String project= null; final IType declaring= getDeclaringType(); final IJavaProject javaProject= declaring.getJavaProject(); if (javaProject != null) project= javaProject.getElementName(); int flags= JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE; try { if (declaring.isLocal() || declaring.isAnonymous()) flags|= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT; } catch (JavaModelException exception) { JavaPlugin.log(exception); } final String description= fMembersToMove.length == 1 ? Messages.format(RefactoringCoreMessages.PushDownRefactoring_descriptor_description_short_multi, BasicElementLabels.getJavaElementName(fMembersToMove[0].getElementName())) : RefactoringCoreMessages.PushDownRefactoring_descriptor_description_short; final String header= fMembersToMove.length == 1 ? Messages.format(RefactoringCoreMessages.PushDownRefactoring_descriptor_description_full, new String[] { JavaElementLabels.getElementLabel(fMembersToMove[0], JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED) }) : Messages.format(RefactoringCoreMessages.PushDownRefactoring_descriptor_description, new String[] { JavaElementLabels.getElementLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED) }); final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header); final String[] settings= new String[fMembersToMove.length]; for (int index= 0; index < settings.length; index++) settings[index]= JavaElementLabels.getElementLabel(fMembersToMove[index], JavaElementLabels.ALL_FULLY_QUALIFIED); comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.PushDownRefactoring_pushed_members_pattern, settings)); addSuperTypeSettings(comment, true); final PushDownDescriptor descriptor= RefactoringSignatureDescriptorFactory.createPushDownDescriptor(project, description, comment.asString(), arguments, flags); if (fCachedDeclaringType != null) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCachedDeclaringType)); for (int index= 0; index < fMembersToMove.length; index++) { arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (index + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fMembersToMove[index])); for (int offset= 0; offset < fMemberInfos.length; offset++) { if (fMemberInfos[offset].getMember().equals(fMembersToMove[index])) { switch (fMemberInfos[offset].getAction()) { case MemberActionInfo.PUSH_ABSTRACT_ACTION: arguments.put(ATTRIBUTE_ABSTRACT + (index + 1), Boolean.valueOf(true).toString()); break; case MemberActionInfo.PUSH_DOWN_ACTION: arguments.put(ATTRIBUTE_PUSH + (index + 1), Boolean.valueOf(true).toString()); break; } } } } return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.PushDownRefactoring_change_name, fChangeManager.getAllChanges()); } finally { pm.done(); clearCaches(); } }
Example 9
Source File: ExtractSupertypeProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * {@inheritDoc} */ @Override public Change createChange(final IProgressMonitor monitor) throws CoreException, OperationCanceledException { try { final Map<String, String> arguments= new HashMap<String, String>(); String project= null; final IType declaring= getDeclaringType(); final IJavaProject javaProject= declaring.getJavaProject(); if (javaProject != null) project= javaProject.getElementName(); int flags= JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE; try { if (declaring.isLocal() || declaring.isAnonymous()) flags|= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT; } catch (JavaModelException exception) { JavaPlugin.log(exception); } final String description= Messages.format(RefactoringCoreMessages.ExtractSupertypeProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fTypeName)); final String header= Messages.format(RefactoringCoreMessages.ExtractSupertypeProcessor_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fDestinationType, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(fCachedDeclaringType, JavaElementLabels.ALL_FULLY_QUALIFIED)}); final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header); final IType[] types= getTypesToExtract(); String[] settings= new String[types.length]; for (int index= 0; index < settings.length; index++) settings[index]= JavaElementLabels.getElementLabel(types[index], JavaElementLabels.ALL_FULLY_QUALIFIED); comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ExtractSupertypeProcessor_subtypes_pattern, settings)); comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractSupertypeProcessor_refactored_element_pattern, JavaElementLabels.getElementLabel(fDestinationType, JavaElementLabels.ALL_FULLY_QUALIFIED))); settings= new String[fMembersToMove.length]; for (int index= 0; index < settings.length; index++) settings[index]= JavaElementLabels.getElementLabel(fMembersToMove[index], JavaElementLabels.ALL_FULLY_QUALIFIED); comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ExtractInterfaceProcessor_extracted_members_pattern, settings)); addSuperTypeSettings(comment, true); final ExtractSuperclassDescriptor descriptor= RefactoringSignatureDescriptorFactory.createExtractSuperclassDescriptor(project, description, comment.asString(), arguments, flags); arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fTypeName); arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, getDeclaringType())); arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplace).toString()); arguments.put(ATTRIBUTE_INSTANCEOF, Boolean.valueOf(fInstanceOf).toString()); arguments.put(ATTRIBUTE_STUBS, Boolean.valueOf(fCreateMethodStubs).toString()); arguments.put(ATTRIBUTE_EXTRACT, new Integer(fMembersToMove.length).toString()); for (int offset= 0; offset < fMembersToMove.length; offset++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fMembersToMove[offset])); arguments.put(ATTRIBUTE_DELETE, new Integer(fDeletedMethods.length).toString()); for (int offset= 0; offset < fDeletedMethods.length; offset++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + fMembersToMove.length + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fDeletedMethods[offset])); arguments.put(ATTRIBUTE_ABSTRACT, new Integer(fAbstractMethods.length).toString()); for (int offset= 0; offset < fAbstractMethods.length; offset++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + fMembersToMove.length + fDeletedMethods.length + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fAbstractMethods[offset])); arguments.put(ATTRIBUTE_TYPES, new Integer(fTypesToExtract.length).toString()); for (int offset= 0; offset < fTypesToExtract.length; offset++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + fMembersToMove.length + fDeletedMethods.length + fAbstractMethods.length + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fTypesToExtract[offset])); final DynamicValidationRefactoringChange change= new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.ExtractSupertypeProcessor_extract_supertype, fChangeManager.getAllChanges()); final IFile file= ResourceUtil.getFile(declaring.getCompilationUnit()); if (fSuperSource != null && fSuperSource.length() > 0) change.add(new CreateCompilationUnitChange(declaring.getPackageFragment().getCompilationUnit(JavaModelUtil.getRenamedCUName(declaring.getCompilationUnit(), fTypeName)), fSuperSource, file.getCharset(false))); return change; } finally { monitor.done(); clearCaches(); } }
Example 10
Source File: PullUpRefactoringProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * {@inheritDoc} */ @Override public Change createChange(final IProgressMonitor monitor) throws CoreException, OperationCanceledException { try { final Map<String, String> arguments= new HashMap<String, String>(); String project= null; final IType declaring= getDeclaringType(); final IJavaProject javaProject= declaring.getJavaProject(); if (javaProject != null) project= javaProject.getElementName(); int flags= JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE; try { if (declaring.isLocal() || declaring.isAnonymous()) flags|= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT; } catch (JavaModelException exception) { JavaPlugin.log(exception); } final String description= fMembersToMove.length == 1 ? Messages.format(RefactoringCoreMessages.PullUpRefactoring_descriptor_description_short, new String[] { JavaElementLabels.getElementLabel(fMembersToMove[0], JavaElementLabels.ALL_DEFAULT), JavaElementLabels.getElementLabel(fDestinationType, JavaElementLabels.ALL_DEFAULT)}) : Messages.format(RefactoringCoreMessages.PullUpRefactoring_descriptor_description_short_multiple, BasicElementLabels.getJavaElementName(fDestinationType.getElementName())); final String header= fMembersToMove.length == 1 ? Messages.format(RefactoringCoreMessages.PullUpRefactoring_descriptor_description_full, new String[] { JavaElementLabels.getElementLabel(fMembersToMove[0], JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(fDestinationType, JavaElementLabels.ALL_FULLY_QUALIFIED)}) : Messages.format(RefactoringCoreMessages.PullUpRefactoring_descriptor_description, new String[] { JavaElementLabels.getElementLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(fDestinationType, JavaElementLabels.ALL_FULLY_QUALIFIED)}); final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header); comment.addSetting(Messages.format(RefactoringCoreMessages.MoveStaticMembersProcessor_target_element_pattern, JavaElementLabels.getElementLabel(fDestinationType, JavaElementLabels.ALL_FULLY_QUALIFIED))); addSuperTypeSettings(comment, true); final PullUpDescriptor descriptor= RefactoringSignatureDescriptorFactory.createPullUpDescriptor(project, description, comment.asString(), arguments, flags); arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fDestinationType)); arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplace).toString()); arguments.put(ATTRIBUTE_INSTANCEOF, Boolean.valueOf(fInstanceOf).toString()); arguments.put(ATTRIBUTE_STUBS, Boolean.valueOf(fCreateMethodStubs).toString()); arguments.put(ATTRIBUTE_PULL, new Integer(fMembersToMove.length).toString()); for (int offset= 0; offset < fMembersToMove.length; offset++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fMembersToMove[offset])); arguments.put(ATTRIBUTE_DELETE, new Integer(fDeletedMethods.length).toString()); for (int offset= 0; offset < fDeletedMethods.length; offset++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + fMembersToMove.length + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fDeletedMethods[offset])); arguments.put(ATTRIBUTE_ABSTRACT, new Integer(fAbstractMethods.length).toString()); for (int offset= 0; offset < fAbstractMethods.length; offset++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (offset + fMembersToMove.length + fDeletedMethods.length + 1), JavaRefactoringDescriptorUtil.elementToHandle(project, fAbstractMethods[offset])); return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.PullUpRefactoring_Pull_Up, fChangeManager.getAllChanges()); } finally { monitor.done(); clearCaches(); } }
Example 11
Source File: PullUpRefactoringProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
protected void rewriteTypeOccurrences(final TextEditBasedChangeManager manager, final CompilationUnitRewrite sourceRewrite, final ICompilationUnit copy, final Set<String> replacements, final RefactoringStatus status, final IProgressMonitor monitor) { try { monitor.beginTask("", 100); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.PullUpRefactoring_checking); final IType declaring= getDeclaringType(); final IJavaProject project= declaring.getJavaProject(); final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setWorkingCopyOwner(fOwner); parser.setResolveBindings(true); parser.setProject(project); parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project)); parser.createASTs(new ICompilationUnit[] { copy}, new String[0], new ASTRequestor() { @Override public final void acceptAST(final ICompilationUnit unit, final CompilationUnit node) { try { final IType subType= (IType) JavaModelUtil.findInCompilationUnit(unit, declaring); final AbstractTypeDeclaration subDeclaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(subType, node); if (subDeclaration != null) { final ITypeBinding subBinding= subDeclaration.resolveBinding(); if (subBinding != null) { String name= null; ITypeBinding superBinding= null; final ITypeBinding[] superBindings= Bindings.getAllSuperTypes(subBinding); for (int index= 0; index < superBindings.length; index++) { name= superBindings[index].getName(); if (name.startsWith(fDestinationType.getElementName())) superBinding= superBindings[index]; } if (superBinding != null) { solveSuperTypeConstraints(unit, node, subType, subBinding, superBinding, new SubProgressMonitor(monitor, 80), status); if (!status.hasFatalError()) rewriteTypeOccurrences(manager, this, sourceRewrite, unit, node, replacements, status, new SubProgressMonitor(monitor, 120)); } } } } catch (JavaModelException exception) { JavaPlugin.log(exception); status.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractInterfaceProcessor_internal_error)); } } @Override public final void acceptBinding(final String key, final IBinding binding) { // Do nothing } }, new NullProgressMonitor()); } finally { monitor.done(); } }
Example 12
Source File: CompilerSourceUtils.java From jenerate with Eclipse Public License 1.0 | 2 votes |
/** * Determines if the source code of a certain {@link IType} is compiled for Java5 or above * * @param objectClass the class for which to check the source compiler version * @return {@code true} if the compiler is equals to Java5 or above, {@code false} otherwise */ public static boolean isSourceLevelGreaterThanOrEqualTo5(IType objectClass) { IJavaProject project = objectClass.getJavaProject(); float source = Float.parseFloat(project.getOption(JavaCore.COMPILER_SOURCE, true)); return source >= 1.5; }