Exemple #1
0
  /**
   * Returns the corresponding Java type for given full-qualified class name.
   *
   * @param project the JDT project the class belongs to
   * @param className the full qualified class name of the requested Java type
   * @return the requested Java type or null if the class is not defined or the project is not
   *     accessible
   */
  public static IType getJavaType(IProject project, String className) {
    IJavaProject javaProject = JdtUtils.getJavaProject(project);

    if (className != null) {

      // For inner classes replace '$' by '.'
      String unchangedClassName = null;
      int pos = className.lastIndexOf('$');
      if (pos > 0) {
        unchangedClassName = className;
        className = className.replace('$', '.');
      }

      try {
        IType type = null;
        // First look for the type in the Java project
        if (javaProject != null) {
          type = javaProject.findType(className, new NullProgressMonitor());

          if (type == null && unchangedClassName != null) {
            type =
                findTypeWithInnerClassesInvolved(
                    javaProject, unchangedClassName, new NullProgressMonitor());
          }

          if (type != null) {
            return type;
          }
        }

        // Then look for the type in the referenced Java projects
        for (IProject refProject : project.getReferencedProjects()) {
          IJavaProject refJavaProject = JdtUtils.getJavaProject(refProject);
          if (refJavaProject != null) {
            type = refJavaProject.findType(className);

            if (type == null && unchangedClassName != null) {
              type =
                  findTypeWithInnerClassesInvolved(
                      javaProject, unchangedClassName, new NullProgressMonitor());
            }

            if (type != null) {
              return type;
            }
          }
        }

        // fall back and try to locate the class using AJDT
        return getAjdtType(project, className);
      } catch (CoreException e) {
        SpringCore.log("Error getting Java type '" + className + "'", e);
      }
    }

    return null;
  }
 @Before
 public void createProject() throws Exception {
   project =
       StsTestUtil.createPredefinedProject(
           "jdt-annotation-tests", "org.springframework.ide.eclipse.beans.core.tests");
   javaProject = JdtUtils.getJavaProject(project);
   classloader = JdtUtils.getClassLoader(project, ApplicationContext.class.getClassLoader());
   factory = new JdtMetadataReaderFactory(javaProject, classloader);
 }
  /** Filters out every {@link IFile} which is has unknown root elements in its XML content. */
  @Override
  protected Set<IFile> filterMatchingFiles(Set<IFile> files) {
    // if project is a java project remove bin dirs from the list
    Set<String> outputDirectories = new HashSet<String>();
    IJavaProject javaProject = JdtUtils.getJavaProject(project);
    if (javaProject != null) {
      try {
        // add default output directory
        outputDirectories.add(javaProject.getOutputLocation().toString());

        // add source folder specific output directories
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
          if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
              && entry.getOutputLocation() != null) {
            outputDirectories.add(entry.getOutputLocation().toString());
          }
        }
      } catch (JavaModelException e) {
        BeansCorePlugin.log(e);
      }
    }

    Set<IFile> detectedFiles = new LinkedHashSet<IFile>();
    for (IFile file : files) {
      boolean skip = false;
      // first check if the file sits in an output directory
      String path = file.getFullPath().toString();
      for (String outputDirectory : outputDirectories) {
        if (path.startsWith(outputDirectory)) {
          skip = true;
        }
      }
      if (skip) {
        continue;
      }

      // check if the file is known Spring xml file
      IStructuredModel model = null;
      try {
        try {
          model = StructuredModelManager.getModelManager().getExistingModelForRead(file);
        } catch (RuntimeException e) {
          // sometimes WTP throws a NPE in concurrency situations
        }
        if (model == null) {
          model = StructuredModelManager.getModelManager().getModelForRead(file);
        }
        if (model != null) {
          IDOMDocument document = ((DOMModelImpl) model).getDocument();
          if (document != null && document.getDocumentElement() != null) {
            String namespaceUri = document.getDocumentElement().getNamespaceURI();
            if (applyNamespaceFilter(file, namespaceUri)) {
              detectedFiles.add(file);
            }
          }
        }
      } catch (IOException e) {
        BeansCorePlugin.log(e);
      } catch (CoreException e) {
        BeansCorePlugin.log(e);
      } finally {
        if (model != null) {
          model.releaseFromRead();
        }
      }
    }
    return detectedFiles;
  }