Java Code Examples for org.eclipse.jdt.core.IType#getParent()

The following examples show how to use org.eclipse.jdt.core.IType#getParent() . 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: JavaOutlinePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks whether a given Java element is an inner type.
 *
 * @param element the java element
 * @return <code>true</code> iff the given element is an inner type
 */
private boolean isInnerType(IJavaElement element) {

	if (element != null && element.getElementType() == IJavaElement.TYPE) {
		IType type= (IType)element;
		try {
			return type.isMember();
		} catch (JavaModelException e) {
			IJavaElement parent= type.getParent();
			if (parent != null) {
				int parentElementType= parent.getElementType();
				return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
			}
		}
	}

	return false;
}
 
Example 2
Source File: NameLookup.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean isPrimaryType(String name, IType type, boolean partialMatch) {
	/*
	 * Please have a look at: NameLookup#NameLookup
	 * The HashTable this.typesInWorkingCopies contains values which are HashTables themselves.
	 * The values of these HashTables are either of IType or IType[].
	 * These values are types belonging to a compilation unit. Please check:
	 * CompilationUnit#getTypes().
	 * Therefore the parents of these types would be compilation units.
	 */
	ICompilationUnit cu = (ICompilationUnit) type.getParent();
	String cuName = cu.getElementName().substring(0, cu.getElementName().lastIndexOf('.'));
	if (partialMatch) {
		return cuName.regionMatches(0, name, 0, name.length());
	} else {
		return cuName.equals(name);
	}
}
 
Example 3
Source File: JavaElementResourceMapping.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static ResourceMapping create(IType type) {
	// top level types behave like the CU
	IJavaElement parent= type.getParent();
	if (parent instanceof ICompilationUnit) {
		return create((ICompilationUnit)parent);
	}
	return null;
}
 
Example 4
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isConvertAnonymousAvailable(final IType type) throws JavaModelException {
	if (Checks.isAvailable(type)) {
		final IJavaElement element = type.getParent();
		if (element instanceof IField && JdtFlags.isEnum((IMember) element)) {
			return false;
		}
		return type.isAnonymous();
	}
	return false;
}
 
Example 5
Source File: AbstractRefactoringTest.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
protected void assertTypeRename(IType oldType, String newName)
    throws Exception {
  // Get the references before the rename
  Set<IIndexedJavaRef> oldRefs = JavaQueryParticipant.findWorkspaceReferences(
      oldType, true);
  assertTrue(oldRefs.size() > 0);

  // Rename the type
  rename(RenameSupport.create(oldType, newName,
      RenameSupport.UPDATE_REFERENCES));

  // Get the type after the name change
  IType newType = null;
  IJavaElement parent = oldType.getParent();
  if (parent instanceof IType) {
    newType = ((IType) parent).getType(newName);
  } else if (parent instanceof ICompilationUnit) {
    newType = ((ICompilationUnit) parent).getType(newName);
  } else {
    fail("Old type parent is invalid");
  }

  // After the rename, verify that the references are updated
  Set<IIndexedJavaRef> newRefs = JavaQueryParticipant.findWorkspaceReferences(
      newType, true);
  assertEquals(oldRefs.size(), newRefs.size());
}
 
Example 6
Source File: JsniCompletionProposal.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private String computeCtorCompletion() {
  StringBuilder sb = new StringBuilder();
  sb.append(JSNI_CTOR_METHOD);
  sb.append('(');

  try {
    // References to constructors of non-static inner classes must take an
    // instance of their enclosing class as the first parameter.
    String qualifiedTypeName = Signature.toString(new String(
        wrappedProposal.getDeclarationSignature()));
    IType type = javaProject.findType(qualifiedTypeName);
    if (type != null) {
      // See if the type is a non-static inner class
      IJavaElement typeParent = type.getParent();
      if (typeParent.getElementType() == IJavaElement.TYPE
          && !Flags.isStatic(type.getFlags())) {
        // Calculate the (binary) type signature of the enclosing type
        String outerTypeName = ((IType) typeParent).getFullyQualifiedName();
        String outerTypeSig = Signature.createTypeSignature(outerTypeName,
            true).replace('.', '/');

        // Add as first parameter of ctor references
        sb.append(outerTypeSig);
      }
    }
  } catch (JavaModelException e) {
    GWTPluginLog.logError(e);
  }

  sb.append(getParamTypesSignature(wrappedProposal));
  sb.append(')');
  return sb.toString();
}
 
Example 7
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isConvertAnonymousAvailable(final IType type) throws JavaModelException {
	if (Checks.isAvailable(type)) {
		final IJavaElement element= type.getParent();
		if (element instanceof IField && JdtFlags.isEnum((IMember) element))
			return false;
		return type.isAnonymous();
	}
	return false;
}
 
Example 8
Source File: JavaElementResourceMapping.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ResourceMapping create(IType type) {
	// top level types behave like the CU
	IJavaElement parent= type.getParent();
	if (parent instanceof ICompilationUnit) {
		return create((ICompilationUnit)parent);
	}
	return null;
}
 
Example 9
Source File: SortMembersAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ICompilationUnit getSelectedCompilationUnit(IStructuredSelection selection) {
	if (selection.size() == 1) {
		Object element= selection.getFirstElement();
		if (element instanceof ICompilationUnit) {
			return (ICompilationUnit) element;
		} else if (element instanceof IType) {
			IType type= (IType) element;
			if (type.getParent() instanceof ICompilationUnit) { // only top level types
				return type.getCompilationUnit();
			}
		}
	}
	return null;
}
 
Example 10
Source File: ClassFileEditorInputFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public IAdaptable createElement(IMemento memento) {
	String identifier= memento.getString(KEY);
	if (identifier == null)
		return null;

	IJavaElement element= JavaCore.create(identifier);
	try {
		if (!element.exists() && element instanceof IClassFile) {
			/*
			 * Let's try to find the class file,
			 * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83221
			 */
			IClassFile cf= (IClassFile)element;
			IType type= cf.getType();
			IJavaProject project= element.getJavaProject();
			if (project != null) {
				type= project.findType(type.getFullyQualifiedName());
				if (type == null)
					return null;
				element= type.getParent();
			}
		}
		return EditorUtility.getEditorInput(element);
	} catch (JavaModelException x) {
		// Don't report but simply return null
		return null;
	}
}
 
Example 11
Source File: JavaOutlineInformationControl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isInnerType(IJavaElement element) {
	if (element != null && element.getElementType() == IJavaElement.TYPE) {
		IType type= (IType)element;
		try {
			return type.isMember();
		} catch (JavaModelException e) {
			IJavaElement parent= type.getParent();
			if (parent != null) {
				int parentElementType= parent.getElementType();
				return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
			}
		}
	}
	return false;
}
 
Example 12
Source File: CreateAsyncInterfaceProposal.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public static List<IJavaCompletionProposal> createProposals(IInvocationContext context, IProblemLocation problem)
    throws JavaModelException {
  String syncTypeName = problem.getProblemArguments()[0];
  IJavaProject javaProject = context.getCompilationUnit().getJavaProject();
  IType syncType = JavaModelSearch.findType(javaProject, syncTypeName);
  if (syncType == null || !syncType.isInterface()) {
    return Collections.emptyList();
  }

  CompilationUnit cu = context.getASTRoot();
  ASTNode coveredNode = problem.getCoveredNode(cu);
  TypeDeclaration syncTypeDecl = (TypeDeclaration) coveredNode.getParent();
  assert (cu.getAST().hasResolvedBindings());

  ITypeBinding syncTypeBinding = syncTypeDecl.resolveBinding();
  assert (syncTypeBinding != null);

  String asyncName = RemoteServiceUtilities.computeAsyncTypeName(problem.getProblemArguments()[0]);
  AST ast = context.getASTRoot().getAST();
  Name name = ast.newName(asyncName);

  /*
   * HACK: NewCUUsingWizardProposal wants a name that has a parent expression so we create an
   * assignment so that the name has a valid parent
   */
  ast.newAssignment().setLeftHandSide(name);

  IJavaElement typeContainer = syncType.getParent();
  if (typeContainer.getElementType() == IJavaElement.COMPILATION_UNIT) {
    typeContainer = syncType.getPackageFragment();
  }

  // Add a create async interface proposal
  CreateAsyncInterfaceProposal createAsyncInterfaceProposal = new CreateAsyncInterfaceProposal(
      context.getCompilationUnit(), name, K_INTERFACE, typeContainer, 2, syncTypeBinding);

  // Add the stock create interface proposal
  NewCompilationUnitUsingWizardProposal fallbackProposal = new NewCompilationUnitUsingWizardProposal(
      context.getCompilationUnit(), name, K_INTERFACE, context.getCompilationUnit().getParent(), 1);

  return Arrays.<IJavaCompletionProposal>asList(createAsyncInterfaceProposal, fallbackProposal);
}
 
Example 13
Source File: MemberFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isLocalType(IType type) {
	IJavaElement parent= type.getParent();
	return parent instanceof IMember && !(parent instanceof IType);
}