本文整理汇总了Java中org.eclipse.jdt.core.IJavaElement.COMPILATION_UNIT属性的典型用法代码示例。如果您正苦于以下问题:Java IJavaElement.COMPILATION_UNIT属性的具体用法?Java IJavaElement.COMPILATION_UNIT怎么用?Java IJavaElement.COMPILATION_UNIT使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.eclipse.jdt.core.IJavaElement
的用法示例。
在下文中一共展示了IJavaElement.COMPILATION_UNIT属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTarget
/**
* Returns the target of this resource change.
* @param elem the changed element
* @return the target of the change, or an empty string if the target is not either a project, package, or file
*/
private String getTarget(IJavaElement elem) {
if (elem == null) {
return "";
}
int type = elem.getElementType();
if (type == IJavaElement.JAVA_PROJECT) {
return "Project";
} else if (type == IJavaElement.PACKAGE_DECLARATION) {
return "Package";
} else if (type == IJavaElement.COMPILATION_UNIT) {
return "File";
}
return elem.getElementName() + "@" + elem.getElementType();
}
示例2: getName
/**
* Obtains the name of the specified element.
* @param elem the element
* @param type the type of the element
* @return the name string
*/
public static String getName(IJavaElement elem) {
int type = elem.getElementType();
if (type == IJavaElement.JAVA_PROJECT) {
return elem.getResource().getName();
} else if (type == IJavaElement.PACKAGE_DECLARATION) {
IPackageFragment jpackage = (IPackageFragment)elem;
return jpackage.getElementName();
} else if (type == IJavaElement.COMPILATION_UNIT) {
return elem.getResource().getName();
}
return "";
}
示例3: getMethodDeclaration
public static MethodDeclaration getMethodDeclaration(String methodName) {
IJavaElement javaElem = EditorUtility.getActiveEditorJavaInput();
if (javaElem.getElementType() == IJavaElement.COMPILATION_UNIT) {
ICompilationUnit iCompUnit = (ICompilationUnit) javaElem;
ASTNode astNode = Crystal.getInstance()
.getASTNodeFromCompilationUnit(iCompUnit);
if (astNode != null
&& astNode.getNodeType() == ASTNode.COMPILATION_UNIT) {
CompilationUnit compUnit = (CompilationUnit) astNode;
for (Object declaration : compUnit.types()) {
if (declaration instanceof TypeDeclaration) {
for (MethodDeclaration method : ((TypeDeclaration) declaration)
.getMethods()) {
if (methodName.contentEquals(method.getName()
.getFullyQualifiedName())) {
return method;
}
}
}
}
}
}
return null;
}
示例4: getTypeOfOpenJavaEditor
public static IType getTypeOfOpenJavaEditor() {
IJavaElement activeEditorJavaInput = EditorUtility.getActiveEditorJavaInput();
if (activeEditorJavaInput != null) {
if (activeEditorJavaInput.getElementType() == IJavaElement.COMPILATION_UNIT) {
try {
ICompilationUnit iCompilationUnit = (ICompilationUnit) activeEditorJavaInput;
String elementName = iCompilationUnit.getElementName();
elementName = elementName.substring(0,
elementName.lastIndexOf('.'));
IType[] types = iCompilationUnit.getTypes();
for (IType type : types) {
String fullyQualifiedName = type
.getFullyQualifiedName();
if (fullyQualifiedName.contains(elementName)) {
return type;
}
}
} catch (JavaModelException e1) {
e1.printStackTrace();
}
}
} else {
System.err.println("Java Editor is not open");
}
return null;
}
示例5: launch
private void launch(Object[] elements, String mode) {
try {
IJavaElement[] ijes = null;
List<IJavaElement> jes = new ArrayList<IJavaElement>();
for (int i = 0; i < elements.length; i++) {
Object selected= elements[i];
if (selected instanceof IJavaElement) {
IJavaElement element= (IJavaElement) selected;
switch (element.getElementType()) {
case IJavaElement.COMPILATION_UNIT:
jes.add(element);
break;
}
}
}
ijes = new IJavaElement[jes.size()];
jes.toArray(ijes);
ILaunchConfigurationWorkingCopy wc = buildLaunchConfiguration(ijes);
if (wc==null) return;
ILaunchConfiguration config= findExistingORCreateLaunchConfiguration(wc, mode);
DebugUITools.launch(config, mode);
} catch (Exception e) {
ResourceManager.logException(e);
MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), "GW4E Launcher", (Image)null, "Unable to launch. See error in Error view.", MessageDialog.ERROR, new String[] { "Close" }, 0);
dialog.open();
}
}
示例6: findElementAtCursor
/**
* Try to identify a nested java element of the given element from a textual
* selection in its source code. This might be possible if the given element
* is a compilation unit or class file.
*
* @param unit
* unit to search
* @param selection
* selection within this unit
* @return nested element or the unit itself
*/
private IJavaElement findElementAtCursor(IJavaElement unit,
ISelection selection) {
int pos = -1;
if (selection instanceof ITextSelection) {
pos = ((ITextSelection) selection).getOffset();
}
if (selection instanceof IMarkSelection) {
pos = ((IMarkSelection) selection).getOffset();
}
if (pos == -1)
return unit;
IJavaElement element = null;
try {
switch (unit.getElementType()) {
case IJavaElement.COMPILATION_UNIT:
element = ((ICompilationUnit) unit).getElementAt(pos);
break;
case IJavaElement.CLASS_FILE:
element = ((IClassFile) unit).getElementAt(pos);
break;
}
} catch (JavaModelException e) {
// we ignore this
}
return element == null ? unit : element;
}
示例7: organizeImportsInProject
/**
* Organize imports when select a project.
*
* @param proj
* the target project
* @return
*/
public WorkspaceEdit organizeImportsInProject(IProject proj) {
WorkspaceEdit rootEdit = new WorkspaceEdit();
HashSet<IJavaElement> result = new HashSet<>();
collectCompilationUnits(JavaCore.create(proj), result, null);
for (IJavaElement elem : result) {
if (elem.getElementType() == IJavaElement.COMPILATION_UNIT) {
organizeImportsInCompilationUnit((ICompilationUnit) elem, rootEdit);
}
}
return rootEdit;
}
示例8: organizeImportsInDirectory
/**
* Organize imports underlying a directory
*
* @param folderUri
* Selected folder URI
* @param proj
* the folder associated project
* @return
* @throws CoreException
*/
public WorkspaceEdit organizeImportsInDirectory(String folderUri, IProject proj) throws CoreException {
WorkspaceEdit rootEdit = new WorkspaceEdit();
IPackageFragment fragment = null;
if (JDTUtils.toURI(folderUri) != null) {
fragment = JDTUtils.resolvePackage(folderUri);
}
// Select an individual package
if (fragment != null) {
organizeImportsInPackageFragment(fragment, rootEdit);
} else if (proj != null) {
// Search the packages under the selected folder:
IJavaProject javaProject = JavaCore.create(proj);
IPath rootPath = ResourceUtils.filePathFromURI(folderUri);
IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
HashSet<IJavaElement> result = new HashSet<>();
for (IPackageFragmentRoot root : roots) {
if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
String packageRoot = root.getResource().getLocation().toString();
if (packageRoot.toLowerCase().indexOf(rootPath.toString().toLowerCase()) >= 0) {
collectCompilationUnits(javaProject, result, null);
}
}
}
for (IJavaElement elem : result) {
if (elem.getElementType() == IJavaElement.COMPILATION_UNIT) {
organizeImportsInCompilationUnit((ICompilationUnit) elem, rootEdit);
}
}
}
return rootEdit;
}
示例9: organizeImportsInPackageFragment
public void organizeImportsInPackageFragment(IPackageFragment fragment, WorkspaceEdit rootEdit) throws CoreException {
HashSet<IJavaElement> result = new HashSet<>();
collectCompilationUnits(fragment.getParent(), result, fragment.getElementName());
for (IJavaElement elem : result) {
if (elem.getElementType() == IJavaElement.COMPILATION_UNIT) {
organizeImportsInCompilationUnit((ICompilationUnit) elem, rootEdit);
}
}
}
示例10: collectCompilationUnits
private void collectCompilationUnits(Object element, Collection<IJavaElement> result, String packagePrefix) {
try {
if (element instanceof IJavaElement) {
IJavaElement elem = (IJavaElement) element;
if (elem.exists()) {
switch (elem.getElementType()) {
case IJavaElement.TYPE:
if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
result.add(elem.getParent());
}
break;
case IJavaElement.COMPILATION_UNIT:
result.add(elem);
break;
case IJavaElement.IMPORT_CONTAINER:
result.add(elem.getParent());
break;
case IJavaElement.PACKAGE_FRAGMENT:
collectCompilationUnits((IPackageFragment) elem, result);
break;
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
collectCompilationUnits((IPackageFragmentRoot) elem, result, packagePrefix);
break;
case IJavaElement.JAVA_PROJECT:
IPackageFragmentRoot[] roots = ((IJavaProject) elem).getPackageFragmentRoots();
for (int k = 0; k < roots.length; k++) {
collectCompilationUnits(roots[k], result, null);
}
break;
}
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem collection compilation unit ", e);
}
}
示例11: getResource
private static IResource getResource(IJavaElement element) {
if (element.getElementType() == IJavaElement.COMPILATION_UNIT) {
return ((ICompilationUnit) element).getResource();
} else if (element instanceof IOpenable) {
return element.getResource();
} else {
return null;
}
}
示例12: mapKind
public static SymbolKind mapKind(IJavaElement element) {
switch (element.getElementType()) {
case IJavaElement.ANNOTATION:
return SymbolKind.Property; // TODO: find a better mapping
case IJavaElement.CLASS_FILE:
case IJavaElement.COMPILATION_UNIT:
return SymbolKind.File;
case IJavaElement.FIELD:
return SymbolKind.Field;
case IJavaElement.IMPORT_CONTAINER:
case IJavaElement.IMPORT_DECLARATION:
return SymbolKind.Module;
case IJavaElement.INITIALIZER:
return SymbolKind.Constructor;
case IJavaElement.LOCAL_VARIABLE:
case IJavaElement.TYPE_PARAMETER:
return SymbolKind.Variable;
case IJavaElement.METHOD:
return SymbolKind.Method;
case IJavaElement.PACKAGE_DECLARATION:
return SymbolKind.Package;
case IJavaElement.TYPE:
try {
return ( ((IType)element).isInterface() ? SymbolKind.Interface : SymbolKind.Class);
} catch (JavaModelException e) {
return SymbolKind.Class;
}
}
return SymbolKind.String;
}
示例13: getASTNode
public static ASTNode getASTNode(int offset, int length, IEditorPart part) {
IJavaElement activeEditorJavaInput = null;
if (part != null) {
IEditorInput editorInput = part.getEditorInput();
if (editorInput != null) {
activeEditorJavaInput = JavaUI
.getEditorInputJavaElement(editorInput);
}
} else {
activeEditorJavaInput = EditorUtility.getActiveEditorJavaInput();
part = getActivePart();
}
if (activeEditorJavaInput != null
&& activeEditorJavaInput.getElementType() == IJavaElement.COMPILATION_UNIT) {
ICompilationUnit compilationUnit = (ICompilationUnit) activeEditorJavaInput;
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(compilationUnit);
parser.setResolveBindings(true);
ASTNode root = parser.createAST(null);
ASTNode node = NodeFinder.perform(root, offset, length);
return node;
}
return null;
}
示例14: shouldCache
/**
* Only cache ASTs for compilation units in working copy mode (open in a
* buffer)
*/
private boolean shouldCache(ITypeRoot input) {
if (input.getElementType() != IJavaElement.COMPILATION_UNIT) {
return false;
}
ICompilationUnit cu = (ICompilationUnit) input;
return cu.getOwner() == null && cu.isWorkingCopy();
}
示例15: appendElementLabel
/**
* Appends the label for a Java element with the flags as defined by this class.
*
* @param element the element to render
* @param flags the rendering flags.
*/
public void appendElementLabel(IJavaElement element, long flags) {
int type= element.getElementType();
IPackageFragmentRoot root= null;
if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
root= JavaModelUtil.getPackageFragmentRoot(element);
}
if (root != null && getFlag(flags, JavaElementLabels.PREPEND_ROOT_PATH)) {
appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
fBuilder.append(JavaElementLabels.CONCAT_STRING);
}
switch (type) {
case IJavaElement.METHOD:
appendMethodLabel((IMethod) element, flags);
break;
case IJavaElement.FIELD:
appendFieldLabel((IField) element, flags);
break;
case IJavaElement.LOCAL_VARIABLE:
appendLocalVariableLabel((ILocalVariable) element, flags);
break;
case IJavaElement.TYPE_PARAMETER:
appendTypeParameterLabel((ITypeParameter) element, flags);
break;
case IJavaElement.INITIALIZER:
appendInitializerLabel((IInitializer) element, flags);
break;
case IJavaElement.TYPE:
appendTypeLabel((IType) element, flags);
break;
case IJavaElement.CLASS_FILE:
appendClassFileLabel((IClassFile) element, flags);
break;
case IJavaElement.COMPILATION_UNIT:
appendCompilationUnitLabel((ICompilationUnit) element, flags);
break;
case IJavaElement.PACKAGE_FRAGMENT:
appendPackageFragmentLabel((IPackageFragment) element, flags);
break;
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags);
break;
case IJavaElement.IMPORT_CONTAINER:
case IJavaElement.IMPORT_DECLARATION:
case IJavaElement.PACKAGE_DECLARATION:
appendDeclarationLabel(element, flags);
break;
case IJavaElement.JAVA_PROJECT:
case IJavaElement.JAVA_MODEL:
fBuilder.append(element.getElementName());
break;
default:
fBuilder.append(element.getElementName());
}
if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) {
fBuilder.append(JavaElementLabels.CONCAT_STRING);
appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
}
}