Java Code Examples for org.eclipse.jdt.core.IPackageFragment#getJavaProject()

The following examples show how to use org.eclipse.jdt.core.IPackageFragment#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: ElementTypeTeller.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks a package if it belong to an existing model. Searches for a
 * package-info.java compilation unit in the package or one of the ancestor
 * packages and checks if it has the {@link Model} annotation.
 */
public static boolean isModelPackage(IPackageFragment pack) {
	try {
		IJavaProject javaProject = pack.getJavaProject();
		String packageName = pack.getElementName();
		for (IPackageFragmentRoot pfRoot : javaProject.getPackageFragmentRoots()) {
			if (!pfRoot.isExternal()) {
				if (isModelPackage(pfRoot, packageName)) {
					return true;
				}
			}
		}
	} catch (JavaModelException e) {
		// TODO: use PluginLogWrapper
		e.printStackTrace();
	}
	return false;
}
 
Example 2
Source File: JavaBuilderState.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return The {@code TypeNames} which are direct children of the given package.
 */
protected TypeNames _getQualifiedTypeNames(IPackageFragment packageFragment) {
	TypeNames qualifiedTypeNames = new TypeNames(packageFragment.getJavaProject());

	Object references = getReferences();
	if (references == null) {
		return qualifiedTypeNames;
	}

	IResource resource = packageFragment.getResource();
	if (resource == null) {
		return qualifiedTypeNames;
	}

	String packageName = packageFragment.getElementName();
	IPath packagePath = resource.getProjectRelativePath();
	int srcPathSegmentCount = getPackageFragmentRoot(packageFragment).getResource().getProjectRelativePath().segmentCount();
	Iterable<?> keys;
	if (references instanceof Map) {
		keys = ((Map<?,?>) references).keySet();
	} else {
		keys = Arrays.asList(((SimpleLookupTable)references).keyTable);
	}
	for (Object typeLocator : keys) {
		if (typeLocator instanceof String) {
			IPath typeLocatorPath = packageFragment.getJavaProject().getProject().getFile((String) typeLocator)
					.getProjectRelativePath();
			if (packagePath.isPrefixOf(typeLocatorPath)) {
				IPath qualifiedPath = typeLocatorPath.removeFirstSegments(srcPathSegmentCount).removeFileExtension();
				String typePackageName = qualifiedPath.removeLastSegments(1).toString().replace("/", ".");
				if (packageName.equals(typePackageName)) {
					String simpleTypeName = qualifiedPath.lastSegment().toString();
					qualifiedTypeNames.addAll(
							getQualifiedTypeNames((String) typeLocator, packageName, simpleTypeName, packageFragment.getJavaProject()));
				}
			}
		}
	}
	return qualifiedTypeNames;
}
 
Example 3
Source File: JavaElementLinks.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static IType resolvePackageInfoType(IPackageFragment pack, String refTypeName) throws JavaModelException {
	// Note: The scoping rules of JLS7 6.3 are broken for package-info.java, see https://bugs.eclipse.org/216451#c4
	// We follow the javadoc tool's implementation and only support fully-qualified type references:
	IJavaProject javaProject = pack.getJavaProject();
	return javaProject.findType(refTypeName, (IProgressMonitor) null);

}
 
Example 4
Source File: PackagesViewFlatContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Object findElementToRefresh(IPackageFragment fragment) {
	if (fViewer.testFindItem(fragment) == null) {
		if(fInputIsProject)
			return fragment.getJavaProject();
		else return fragment.getParent();
	}
	return fragment;
}
 
Example 5
Source File: PackagesViewFlatContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Object findElementToRefresh(LogicalPackage logicalPackage) {
	if (fViewer.testFindItem(logicalPackage) == null) {
		IPackageFragment fragment= logicalPackage.getFragments()[0];
		return fragment.getJavaProject();
	}
	return logicalPackage;
}
 
Example 6
Source File: LogicalPackage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public LogicalPackage(IPackageFragment fragment){
	Assert.isNotNull(fragment);
	fPackages= new HashSet<IPackageFragment>();
	fJavaProject= fragment.getJavaProject();
	Assert.isNotNull(fJavaProject);
	add(fragment);
	fName= fragment.getElementName();
}
 
Example 7
Source File: GeneratedCssResource.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
private void createType(IPackageFragment pckg, boolean addComments)
    throws CoreException {
  IJavaProject javaProject = pckg.getJavaProject();
  final IProgressMonitor monitor = new NullProgressMonitor();

  // Method name should already have been sanitized and validated, so all we
  // should have to do to get a type name is just capitalize it
  String simpleName = StringUtilities.capitalize(getMethodName());

  // See if the type name is already used
  String qualifiedName = JavaModelUtil.concatenateName(pckg.getElementName(),
      simpleName);
  IType existingType = JavaModelSearch.findType(javaProject, qualifiedName);
  if (existingType != null) {
    if (ClientBundleUtilities.isCssResource(javaProject, existingType)) {
      // If the existing type is a CssResource, we'll assume that it wraps
      // this CSS file and use it for our ClientBundle accessor return type
      // instead of trying to generate another CssResource here.
      customCssResourceType = existingType;
      return;
    } else {
      // If it's not a CssResource, then we'll need to generate a CssResource
      // ourself, but we can't use the name. So, let's compute a similar name
      // that is not already in use.
      simpleName = StringUtilities.computeUniqueName(
          getExistingTopLevelTypeNames(pckg), simpleName);
    }
  }

  // Parse the CSS and see if there were problems
  CssParseResult result = parseCss();
  final IStatus status = result.getStatus();

  // Bail out when errors occur
  if (status.getSeverity() == IStatus.ERROR) {
    throw new CoreException(status);
  }

  // For warnings, just display them in a dialog (on the UI thread of course)
  // TODO: would nice if we could aggregate these and show them all at the end
  if (status.getSeverity() == IStatus.WARNING) {
    Display.getDefault().syncExec(new Runnable() {
      public void run() {
        MessageDialog.openWarning(null, "CSS Parsing", status.getMessage());
      }
    });
  }

  // Extract the CSS class names
  final Set<String> cssClassNames = ExtractClassNamesVisitor.exec(result.getStylesheet());

  TypeCreator gen = new TypeCreator(pckg, simpleName,
      TypeCreator.ElementType.INTERFACE,
      new String[] {ClientBundleUtilities.CSS_RESOURCE_TYPE_NAME},
      addComments) {
    @Override
    protected void createTypeMembers(IType newType, ImportRewrite imports)
        throws CoreException {
      // Create an accessor method for each CSS class
      for (String cssClass : cssClassNames) {
        newType.createMethod(computeCssClassMethodSource(newType, cssClass),
            null, true, monitor);
      }
    }
  };
  customCssResourceType = gen.createType();
}
 
Example 8
Source File: JavaElementLinks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static IType resolvePackageInfoType(IPackageFragment pack, String refTypeName) throws JavaModelException {
		// Note: The scoping rules of JLS7 6.3 are broken for package-info.java, see https://bugs.eclipse.org/216451#c4
		// We follow the javadoc tool's implementation and only support fully-qualified type references:
		IJavaProject javaProject= pack.getJavaProject();
		return javaProject.findType(refTypeName, (IProgressMonitor) null);
		
		// This implementation would make sense, but the javadoc tool doesn't support it:
//		IClassFile classFile= pack.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
//		if (classFile.exists()) {
//			return resolveType(classFile.getType(), refTypeName);
//		}
//		
//		// check if refTypeName is a qualified name
//		int firstDot= refTypeName.indexOf('.');
//		if (firstDot != -1) {
//			String typeNameRest= refTypeName.substring(firstDot + 1);
//			String simpleTypeName= refTypeName.substring(0, firstDot);
//			IType simpleType= resolvePackageInfoType(pack, simpleTypeName);
//			if (simpleType != null) {
//				// a type-qualified name
//				return resolveType(simpleType, typeNameRest);
//			} else {
//				// a fully-qualified name
//				return javaProject.findType(refTypeName, (IProgressMonitor) null);
//			}
//		}
//		
//		ICompilationUnit cu= pack.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
//		if (! cu.exists()) {
//			// refTypeName is a simple name in the package-info.java from the source attachment. Sorry, we give up here...
//			return null;
//		}
//		
//		// refTypeName is a simple name in a CU. Let's play the shadowing rules of JLS7 6.4.1:
//		// 1) single-type import
//		// 2) enclosing package
//		// 3) java.lang.* (JLS7 7.3)
//		// 4) on-demand import
//		IImportDeclaration[] imports= cu.getImports();
//		for (int i= 0; i < imports.length; i++) {
//			IImportDeclaration importDecl= imports[i];
//			String name= importDecl.getElementName();
//			if (Flags.isStatic(importDecl.getFlags())) {
//				imports[i]= null;
//			} else 	if (! importDecl.isOnDemand()) {
//				if (name.endsWith('.' + refTypeName)) {
//					// 1) single-type import
//					IType type= javaProject.findType(name, (IProgressMonitor) null);
//					if (type != null)
//						return type;
//				}
//				imports[i]= null;
//			}
//		}
//		
//		// 2) enclosing package
//		IType type= javaProject.findType(pack.getElementName() + '.' + refTypeName, (IProgressMonitor) null);
//		if (type != null)
//			return type;
//		
//		// 3) java.lang.* (JLS7 7.3)
//		type= javaProject.findType("java.lang." + refTypeName, (IProgressMonitor) null); //$NON-NLS-1$
//		if (type != null)
//			return type;
//		
//		// 4) on-demand import
//		for (int i= 0; i < imports.length; i++) {
//			IImportDeclaration importDecl= imports[i];
//			if (importDecl != null) {
//				String name= importDecl.getElementName();
//				name= name.substring(0, name.length() - 1); //remove the *
//				type= javaProject.findType(name + refTypeName, (IProgressMonitor) null);
//				if (type != null)
//					return type;
//			}
//		}
//		return null;
	}