Java Code Examples for org.eclipse.jdt.core.IType#getFields()
The following examples show how to use
org.eclipse.jdt.core.IType#getFields() .
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: GroovyUtil.java From bonita-studio with GNU General Public License v2.0 | 6 votes |
/** * Helper method to retrieve the constant field of Custom Groovy Type * * @param className * @return list of String (the values) */ public static List<String> getTypeValues(final String className) { final List<String> result = new ArrayList<>(); try { final IType t = getType(className); if (t == null) { return result; } for (final IField f : t.getFields()) { final String fieldSource = f.getSource(); if (fieldSource != null && fieldSource.indexOf(GROOVY_CONSTANT_SEPARATOR) != -1) { result.add(fieldSource.substring( fieldSource.indexOf(GROOVY_CONSTANT_SEPARATOR) + 1, fieldSource.lastIndexOf(GROOVY_CONSTANT_SEPARATOR))); // ) } } } catch (final Exception e) { BonitaStudioLog.error(e); } return result; }
Example 2
Source File: RefactoringAvailabilityTester.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static IMember[] getPullUpMembers(final IType type) throws JavaModelException { final List<IMember> list = new ArrayList<>(3); if (type.exists()) { IMember[] members = type.getFields(); for (int index = 0; index < members.length; index++) { if (isPullUpAvailable(members[index])) { list.add(members[index]); } } members = type.getMethods(); for (int index = 0; index < members.length; index++) { if (isPullUpAvailable(members[index])) { list.add(members[index]); } } members = type.getTypes(); for (int index = 0; index < members.length; index++) { if (isPullUpAvailable(members[index])) { list.add(members[index]); } } } return list.toArray(new IMember[list.size()]); }
Example 3
Source File: RefactoringAvailabilityTester.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static IMember[] getPushDownMembers(final IType type) throws JavaModelException { final List<IMember> list = new ArrayList<>(3); if (type.exists()) { IMember[] members = type.getFields(); for (int index = 0; index < members.length; index++) { if (isPushDownAvailable(members[index])) { list.add(members[index]); } } members = type.getMethods(); for (int index = 0; index < members.length; index++) { if (isPushDownAvailable(members[index])) { list.add(members[index]); } } } return list.toArray(new IMember[list.size()]); }
Example 4
Source File: CreateFieldOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * By default the new field is positioned after the last existing field * declaration, or as the first member in the type if there are no * field declarations. */ protected void initializeDefaultPosition() { IType parentElement = getType(); try { IField[] fields = parentElement.getFields(); if (fields != null && fields.length > 0) { final IField lastField = fields[fields.length - 1]; if (parentElement.isEnum()) { IField field = lastField; if (!field.isEnumConstant()) { createAfter(lastField); } } else { createAfter(lastField); } } else { IJavaElement[] elements = parentElement.getChildren(); if (elements != null && elements.length > 0) { createBefore(elements[0]); } } } catch (JavaModelException e) { // type doesn't exist: ignore } }
Example 5
Source File: GenerateGetterSetterOperation.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static AccessorField[] getUnimplementedAccessors(IType type) throws JavaModelException { if (!supportsGetterSetter(type)) { return new AccessorField[0]; } List<AccessorField> unimplemented = new ArrayList<>(); IField[] fields = type.getFields(); for (IField field : fields) { int flags = field.getFlags(); if (!Flags.isEnum(flags)) { boolean isStatic = Flags.isStatic(flags); boolean generateGetter = (GetterSetterUtil.getGetter(field) == null); boolean generateSetter = (!Flags.isFinal(flags) && GetterSetterUtil.getSetter(field) == null); if (generateGetter || generateSetter) { unimplemented.add(new AccessorField(field.getElementName(), isStatic, generateGetter, generateSetter)); } } } return unimplemented.toArray(new AccessorField[0]); }
Example 6
Source File: DialogFactoryHelperImpl.java From jenerate with Eclipse Public License 1.0 | 6 votes |
private IField[] getNonStaticNonCacheFields(IType objectClass, PreferencesManager preferencesManager) throws JavaModelException { Set<String> cacheFields = new HashSet<>(); cacheFields.add(preferencesManager.getCurrentPreferenceValue(JeneratePreferences.HASHCODE_CACHING_FIELD)); cacheFields.add(preferencesManager.getCurrentPreferenceValue(JeneratePreferences.TOSTRING_CACHING_FIELD)); IField[] fields; fields = objectClass.getFields(); List<IField> result = new ArrayList<>(); for (int i = 0, size = fields.length; i < size; i++) { if (!Flags.isStatic(fields[i].getFlags()) && !cacheFields.contains(fields[i].getElementName())) { result.add(fields[i]); } } return result.toArray(new IField[result.size()]); }
Example 7
Source File: DeltaConverter.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
/** * We don't include nested types because structural changes of nested types should not affect Xtend classes which * use top level types. * * @deprecated This method is not used anymore. */ @Deprecated protected void traverseType(IType type, NameBasedEObjectDescriptionBuilder acceptor) { try { if (type.exists()) { for (IField field : type.getFields()) { if (!Flags.isSynthetic(field.getFlags())) { String fieldName = field.getElementName(); acceptor.accept(fieldName); } } for (IMethod method : type.getMethods()) { if (!Flags.isSynthetic(method.getFlags())) { String methodName = method.getElementName(); acceptor.accept(methodName); } } } } catch (JavaModelException e) { if (LOGGER.isDebugEnabled()) LOGGER.debug(e, e); } }
Example 8
Source File: RefactoringAvailabilityTester.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static IMember[] getPullUpMembers(final IType type) throws JavaModelException { final List<IMember> list= new ArrayList<IMember>(3); if (type.exists()) { IMember[] members= type.getFields(); for (int index= 0; index < members.length; index++) { if (isPullUpAvailable(members[index])) list.add(members[index]); } members= type.getMethods(); for (int index= 0; index < members.length; index++) { if (isPullUpAvailable(members[index])) list.add(members[index]); } members= type.getTypes(); for (int index= 0; index < members.length; index++) { if (isPullUpAvailable(members[index])) list.add(members[index]); } } return list.toArray(new IMember[list.size()]); }
Example 9
Source File: RefactoringAvailabilityTester.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static IMember[] getPushDownMembers(final IType type) throws JavaModelException { final List<IMember> list= new ArrayList<IMember>(3); if (type.exists()) { IMember[] members= type.getFields(); for (int index= 0; index < members.length; index++) { if (isPushDownAvailable(members[index])) list.add(members[index]); } members= type.getMethods(); for (int index= 0; index < members.length; index++) { if (isPushDownAvailable(members[index])) list.add(members[index]); } } return list.toArray(new IMember[list.size()]); }
Example 10
Source File: AddGetterSetterAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * @param type the type * @return map IField -> GetterSetterEntry[] * @throws JavaModelException if the type does not exist or if an exception occurs while * accessing its corresponding resource */ private Map<IField, GetterSetterEntry[]> createGetterSetterMapping(IType type) throws JavaModelException { IField[] fields= type.getFields(); Map<IField, GetterSetterEntry[]> result= new LinkedHashMap<IField, GetterSetterEntry[]>(); for (int i= 0; i < fields.length; i++) { IField field= fields[i]; int flags= field.getFlags(); if (!Flags.isEnum(flags)) { List<GetterSetterEntry> l= new ArrayList<GetterSetterEntry>(2); if (GetterSetterUtil.getGetter(field) == null) { l.add(new GetterSetterEntry(field, true, Flags.isFinal(flags))); incNumEntries(); } if (GetterSetterUtil.getSetter(field) == null) { l.add(new GetterSetterEntry(field, false, Flags.isFinal(flags))); incNumEntries(); } if (!l.isEmpty()) result.put(field, l.toArray(new GetterSetterEntry[l.size()])); } } return result; }
Example 11
Source File: MoveInnerToTopRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static String[] getFieldNames(IType type) { try { IField[] fields= type.getFields(); List<String> result= new ArrayList<String>(fields.length); for (int i= 0; i < fields.length; i++) { result.add(fields[i].getElementName()); } return result.toArray(new String[result.size()]); } catch (JavaModelException e) { return null; } }
Example 12
Source File: GetterSetterCompletionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set<String> suggestedMethods, Collection<IJavaCompletionProposal> result) throws CoreException { if (prefix.length() == 0) { relevance--; } IField[] fields= type.getFields(); IMethod[] methods= type.getMethods(); for (int i= 0; i < fields.length; i++) { IField curr= fields[i]; if (!JdtFlags.isEnum(curr)) { String getterName= GetterSetterUtil.getGetterName(curr, null); if (Strings.startsWithIgnoreCase(getterName, prefix) && !hasMethod(methods, getterName)) { suggestedMethods.add(getterName); int getterRelevance= relevance; if (JdtFlags.isStatic(curr) && JdtFlags.isFinal(curr)) getterRelevance= relevance - 1; result.add(new GetterSetterCompletionProposal(curr, offset, length, true, getterRelevance)); } if (!JdtFlags.isFinal(curr)) { String setterName= GetterSetterUtil.getSetterName(curr, null); if (Strings.startsWithIgnoreCase(setterName, prefix) && !hasMethod(methods, setterName)) { suggestedMethods.add(setterName); result.add(new GetterSetterCompletionProposal(curr, offset, length, false, relevance)); } } } } }
Example 13
Source File: StubCreator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
protected void appendEnumConstants(final IType type) throws JavaModelException { final IField[] fields= type.getFields(); final List<IField> list= new ArrayList<IField>(fields.length); for (int index= 0; index < fields.length; index++) { final IField field= fields[index]; if (Flags.isEnum(field.getFlags())) list.add(field); } for (int index= 0; index < list.size(); index++) { if (index > 0) fBuffer.append(","); //$NON-NLS-1$ fBuffer.append(list.get(index).getElementName()); } fBuffer.append(";"); //$NON-NLS-1$ }
Example 14
Source File: WizardUtils.java From txtUML with Eclipse Public License 1.0 | 5 votes |
/** * @return an empty optional if the given type does not have any field, * otherwise the name of the model package and its java project * respectively, which contains the types of the given diagramType */ public static Optional<Pair<String, String>> getModelByFields(IType diagramType) { try { List<String> referencedProjects = new ArrayList<>( Arrays.asList(diagramType.getJavaProject().getRequiredProjectNames())); referencedProjects.add(diagramType.getJavaProject().getElementName()); for (IField field : diagramType.getFields()) { String typeSignature = field.getTypeSignature(); // generic type parameters int typeParamStartIdx = typeSignature.indexOf("<Q"); if (typeParamStartIdx != -1) { typeSignature = typeSignature.substring(typeParamStartIdx + 1, typeSignature.indexOf(';') + 1); } String[][] resolvedTypes = resolveType(diagramType, typeSignature.substring(1, typeSignature.length() - 1)); List<String[]> resolvedTypeList = new ArrayList<>(Arrays.asList(resolvedTypes)); for (String[] type : resolvedTypeList) { Optional<Pair<String, String>> model = ModelUtils.getModelOf(type[0], referencedProjects); if (model.isPresent()) { return model; } } } IType[] superTypes = diagramType.newSupertypeHierarchy(null).getAllSupertypes(diagramType); if (superTypes.length != 0) { IType superType = superTypes[0]; return getModelByFields(superType); } } catch (JavaModelException | NoSuchElementException e) { } return Optional.empty(); }
Example 15
Source File: GenerateDelegateMethodsHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static boolean supportsGenerateDelegateMethods(IType type) throws JavaModelException { if (type == null || type.getCompilationUnit() == null || type.isAnnotation() || type.isInterface()) { return false; } IField[] fields = type.getFields(); int count = 0; for (IField field : fields) { if (!JdtFlags.isEnum(field) && !hasPrimitiveType(field) && !isArray(field)) { count++; } } return count > 0; }
Example 16
Source File: SourceAssistProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private boolean hasFields(IType type, boolean includeStatic) throws JavaModelException { for (IField field : type.getFields()) { if (includeStatic || !JdtFlags.isStatic(field)) { return true; } } return false; }
Example 17
Source File: NLSSearchResultRequestor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Finds the key defined by the given match. The assumption is that the key is the only argument * and it is a string literal i.e. quoted ("...") or a string constant i.e. 'static final * String' defined in the same class. * * @param keyPositionResult reference parameter: will be filled with the position of the found * key * @param enclosingElement enclosing java element * @return a string denoting the key, {@link #NO_KEY} if no key can be found and * <code>null</code> otherwise * @throws CoreException if a problem occurs while accessing the <code>enclosingElement</code> */ private String findKey(Position keyPositionResult, IJavaElement enclosingElement) throws CoreException { ICompilationUnit unit= (ICompilationUnit)enclosingElement.getAncestor(IJavaElement.COMPILATION_UNIT); if (unit == null) return null; String source= unit.getSource(); if (source == null) return null; IJavaProject javaProject= unit.getJavaProject(); IScanner scanner= null; if (javaProject != null) { String complianceLevel= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true); String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true); scanner= ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel); } else { scanner= ToolFactory.createScanner(false, false, false, false); } scanner.setSource(source.toCharArray()); scanner.resetTo(keyPositionResult.getOffset() + keyPositionResult.getLength(), source.length()); try { if (scanner.getNextToken() != ITerminalSymbols.TokenNameDOT) return null; if (scanner.getNextToken() != ITerminalSymbols.TokenNameIdentifier) return null; String src= new String(scanner.getCurrentTokenSource()); int tokenStart= scanner.getCurrentTokenStartPosition(); int tokenEnd= scanner.getCurrentTokenEndPosition(); if (scanner.getNextToken() == ITerminalSymbols.TokenNameLPAREN) { // Old school // next must be key string. Ignore methods which do not take a single String parameter (Bug 295040). int nextToken= scanner.getNextToken(); if (nextToken != ITerminalSymbols.TokenNameStringLiteral && nextToken != ITerminalSymbols.TokenNameIdentifier) return null; tokenStart= scanner.getCurrentTokenStartPosition(); tokenEnd= scanner.getCurrentTokenEndPosition(); int token; while ((token= scanner.getNextToken()) == ITerminalSymbols.TokenNameDOT) { if ((nextToken= scanner.getNextToken()) != ITerminalSymbols.TokenNameIdentifier) { return null; } tokenStart= scanner.getCurrentTokenStartPosition(); tokenEnd= scanner.getCurrentTokenEndPosition(); } if (token != ITerminalSymbols.TokenNameRPAREN) return null; if (nextToken == ITerminalSymbols.TokenNameStringLiteral) { keyPositionResult.setOffset(tokenStart + 1); keyPositionResult.setLength(tokenEnd - tokenStart - 1); return source.substring(tokenStart + 1, tokenEnd); } else if (nextToken == ITerminalSymbols.TokenNameIdentifier) { keyPositionResult.setOffset(tokenStart); keyPositionResult.setLength(tokenEnd - tokenStart + 1); IType parentClass= (IType)enclosingElement.getAncestor(IJavaElement.TYPE); IField[] fields= parentClass.getFields(); String identifier= source.substring(tokenStart, tokenEnd + 1); for (int i= 0; i < fields.length; i++) { if (fields[i].getElementName().equals(identifier)) { if (!Signature.getSignatureSimpleName(fields[i].getTypeSignature()).equals("String")) //$NON-NLS-1$ return null; Object obj= fields[i].getConstant(); return obj instanceof String ? ((String)obj).substring(1, ((String)obj).length() - 1) : NO_KEY; } } } return NO_KEY; } else { IJavaElement[] keys= unit.codeSelect(tokenStart, tokenEnd - tokenStart + 1); // an interface can't be a key if (keys.length == 1 && keys[0].getElementType() == IJavaElement.TYPE && ((IType) keys[0]).isInterface()) return null; keyPositionResult.setOffset(tokenStart); keyPositionResult.setLength(tokenEnd - tokenStart + 1); return src; } } catch (InvalidInputException e) { throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); } }