@Contract("null, null, _, _ -> fail") private static void loadMH(GeneratorAdapter adapter, Type reflectorClass, int flags, int mhIndex) { if((flags & Magic.REFLECTOR_METHOD_USE_METHODHANDLE) == 0) return; /* Load MethodHandle field */ adapter.loadThis(); adapter.getField(notNull(reflectorClass, "Reflector class shouldn't be null!"), MHF, MH_ARRAY); /* Load index */ if(mhIndex >= 0 && mhIndex <= 5) /* ICONST_x offset is 3, iow ICONST_0 = 3, ICONST_1 = 4 */ adapter.visitInsn(ICONST_0 + mhIndex); else adapter.visitIntInsn(BIPUSH, mhIndex); /* Load MethodHandle from array */ adapter.visitInsn(AALOAD); }
@Nullable @Contract(pure = true) public static boolean prevElementIsUserRightsMacros(@NotNull final PsiElement element) { Validate.notNull(element); final Class[] skipClasses = {ImpexValueLine.class, PsiComment.class, PsiWhiteSpace.class}; PsiElement prevElement = PsiTreeUtil.skipSiblingsBackward(element, skipClasses); while (null != prevElement) { if (isHeaderLine(prevElement)) { return false; } if (isUserRightsMacros(prevElement)) { return true; } prevElement = PsiTreeUtil.skipSiblingsBackward(prevElement, skipClasses); } return false; }
@Nullable @Contract("null -> null") protected ImpexHeaderTypeName getHeaderTypeNamePsiElementOfAttribute(@Nullable final PsiElement headerAttributePsiElement) { if (null == headerAttributePsiElement || null == headerAttributePsiElement.getNode()) { return null; } final ImpexHeaderLine impexHeaderLine = PsiTreeUtil.getParentOfType( headerAttributePsiElement, ImpexHeaderLine.class ); if (null == impexHeaderLine) { return null; } final ImpexFullHeaderType impexFullHeaderType = impexHeaderLine.getFullHeaderType(); return null == impexFullHeaderType ? null : impexFullHeaderType.getHeaderTypeName(); }
@Nullable @Contract(pure = true) public static boolean nextElementIsHeaderLine(@NotNull final PsiElement element) { Validate.notNull(element); PsiElement nextSibling = element.getNextSibling(); while (null != nextSibling) { if (isImpexValueLine(nextSibling)) { return false; } if (isUserRightsMacros(nextSibling)) { return false; } if (isHeaderLine(nextSibling)) { return true; } nextSibling = nextSibling.getNextSibling(); } return true; }
@Nullable @Contract(pure = true) public static ImpexValueGroup skipAllExceptLineBreaksAndGetImpexValueGroup( @NotNull final PsiElement psiElement ) { Validate.notNull(psiElement); if (isLineBreak(psiElement.getPrevSibling())) { return null; } PsiElement prevSibling = psiElement.getPrevSibling(); while (!isImpexValueLine(prevSibling)) { if (null == prevSibling || isLineBreak(prevSibling)) { return null; } prevSibling = prevSibling.getPrevSibling(); } if (!isImpexValueLine(prevSibling)) { return null; } return PsiTreeUtil.getParentOfType(PsiTreeUtil.lastChild(prevSibling), ImpexValueGroup.class); }
@Contract("null -> false") public static boolean isDecimalNumber(@Nullable PsiType type) { if (type == null) { return false; } return NUMERIC_TYPES.contains(type) || NUMERIC_TYPES.contains(PsiPrimitiveType.getUnboxedType(type)); }
@Contract(pure = true) @NotNull @NonNls public static String[] convert(Pair... origin) { String[] ret = new String[origin.length]; for (int i = 0; i < ret.length; ++i) ret[i] = origin[i].getCombined(); return ret; }
/** * Get class field * * @param fieldName Field's name * @param type Field's type class * @param <V> Field's type * @return {@link FieldWrapper} object or empty, if field wasn't found */ @Contract("null, null -> fail") @SuppressWarnings("unchecked") public <V> Optional<FieldWrapper<V>> getField(String fieldName, Class<V> type) { /* Check arguments */ if(fieldName == null) throw new IllegalStateException("Field name shouldn't be null!"); if(type == null) throw new IllegalStateException("Field type shouldn't be null!"); /* Try to find cached field */ FieldInfo fieldInfo = new FieldInfo(fieldName, type); /* Get field */ Integer found = FIELD_INDEX.get(fieldInfo); Field[] field = new Field[] { found != null ? FIELD_CACHE.get(found) : null }; field[0] = field[0] != null ? field[0] : findDeclaredField(fieldName, type); if(field[0] == null) return Optional.empty(); /* Wrap field */ return Optional.of((FieldWrapper<V>)FIELDWRAPPER_CACHE.computeIfAbsent(found, k -> MethodHandleFieldWrapper.of(this, field[0], type))); }
@Nullable @Contract(pure = true) public static PsiElement getHeaderParametersSeparatorFromHeaderLineByNumber( final int columnNumber, @NotNull final ImpexHeaderLine impexHeaderLine ) { Validate.isTrue(columnNumber >= 0); Validate.notNull(impexHeaderLine); final List<PsiElement> parameterSeparators = CommonPsiUtils.findChildrenByIElementType( impexHeaderLine, ImpexTypes.PARAMETERS_SEPARATOR ); if (columnNumber >= parameterSeparators.size()) { return null; } return parameterSeparators.get(columnNumber); }
/** * Check fields availability in class agains info defined in * {@link FieldDescriptor} objects * * @param clazz Class to perform check on * @param fields {@link FieldDescriptor} objects */ @Contract("null, _ -> fail") public static void checkFields(Class<?> clazz, FieldDescriptor... fields) { ClassWrapper<?> cw = Reflect.wrapClass(clazz); Stream.of(fields).forEach(fieldDescriptor -> { try { Ensure.ensurePresent( cw.getField(fieldDescriptor.getFieldName(), fieldDescriptor.getFieldType()), String.format("Field %s %s not found", fieldDescriptor.getFieldType(), fieldDescriptor.getFieldName()) ); } catch (Exception e) { throw new NullPointerException(e.getLocalizedMessage()); } }); }
/** * Check fields availability in wrapped class agains info defined in * {@link FieldDescriptor} objects * * @param cw Wrapped class to perform check on * @param fields {@link FieldDescriptor} objects */ @Contract("null, _ -> fail") public static void checkFields(ClassWrapper<?> cw, FieldDescriptor... fields) { Ensure.notNull(cw, "ClassWrapper shouldn't be null!"); Stream.of(fields).forEach(fieldDescriptor -> { try { Ensure.ensurePresent( cw.getField(fieldDescriptor.getFieldName(), fieldDescriptor.getFieldType()), String.format("Field %s %s not found", fieldDescriptor.getFieldType(), fieldDescriptor.getFieldName()) ); } catch (Exception e) { throw new NullPointerException(e.getLocalizedMessage()); } }); }
@NotNull @Contract("null, _, null -> fail") private String generateName(ClassWrapper<?> target, int flags, Class<?> intf) { notNull(target, "Target class must not be null!"); notNull(intf, "Interface class must not be null!"); StringBuilder classNameBuilder = new StringBuilder(); classNameBuilder.append(MethodReflector.class.getName()); classNameBuilder.append('.'); classNameBuilder.append("$Target$"); classNameBuilder.append(getClassName(target.getWrappedClass().getName())); classNameBuilder.append('$'); classNameBuilder.append(getClassName(intf.getName())); classNameBuilder.append('$'); classNameBuilder.append(COUNTER.computeIfAbsent(intf, k -> new AtomicInteger(0)).getAndIncrement()); return classNameBuilder.toString(); }
@Contract(pure = false) protected void highlightArea( @NotNull final Editor editor, @NotNull final PsiElement impexFullHeaderParameter ) { Validate.notNull(editor); Validate.notNull(impexFullHeaderParameter); if (isAlreadyHighlighted(editor, impexFullHeaderParameter)) { return; } ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { final PsiElement currentHighlightedElement = highlightedBlocks.remove(editor); if (null != currentHighlightedElement) { modifyHighlightedArea(editor, currentHighlightedElement, true); } highlightedBlocks.put(editor, impexFullHeaderParameter); modifyHighlightedArea(editor, impexFullHeaderParameter, false); } }); }
@NotNull @Contract("null -> fail") public static List<String> splitByWhitespace(@NotNull String args) { @NotNull Matcher matcher = Regexes.WHITESPACE_SPLIT.pattern().matcher(Objects.requireNonNull(args)); //As seen on http://stackoverflow.com/a/7804472 @NotNull List<String> list = new ArrayList<>(); while (matcher.find()) { list.add(matcher.group(1).replace("\"", "")); } return list; }
@NotNull @Contract(pure = true) public Set<String> getTags() { Set<String> tagSet = new HashSet<>(); for (Tag thisTag : tags) tagSet.add(thisTag.getName()); return tagSet; }
@NotNull @Contract(pure = true) private String getValueText(final @NotNull ImpexAttribute impexAttribute) { Validate.notNull(impexAttribute); if (null == impexAttribute.getAnyAttributeValue() || (StringUtils.isBlank(impexAttribute.getAnyAttributeValue() .getText()))) { return impexAttribute.getText(); } return impexAttribute.getAnyAttributeValue().getText(); }
@Override @Contract( pure = true ) public PsiManager getManager() { return xmlTag.getManager(); }
@Contract(pure = true) private boolean quoteAwareStringEquals(@Nullable final String quotedString, @Nullable final String value) { return (null == quotedString) == (null == value) && (null == quotedString || quotedString.equals(value) || ('\'' + quotedString + '\'').equals(value) || ('"' + quotedString + '"').equals(value)); }
@Contract(value = "null -> false", pure = true) public static boolean isUserRightsMacros(@Nullable final PsiElement psiElement) { return psiElement != null && Objects.equals( ImpexTypes.ROOT_MACRO_USAGE, CommonPsiUtils.getNullSafeElementType(psiElement) ) && (psiElement.getText().equals("$START_USERRIGHTS") || psiElement.getText().equals("$END_USERRIGHTS")); }
@Override @Contract( pure = true ) public PsiElement getPrevSibling() { return xmlTag.getPrevSibling(); }
@Contract (pure = true) public boolean contains (@NotNull Predicate<? super E> filter) { //region --> Check params if (filter == null) throw new IllegalArgumentException("Filter param is null"); //endregion return this.stream().anyMatch(filter::test); }
@Override @Contract( pure = true ) public int getStartOffsetInParent() { return xmlTag.getStartOffsetInParent(); }
@Contract(pure = true) protected boolean isAlreadyHighlighted( @NotNull final Editor editor, @Nullable final PsiElement impexFullHeaderParameter ) { Validate.notNull(editor); return this.highlightedBlocks.get(editor) == impexFullHeaderParameter; }
@Override @Nullable @Contract( pure = true ) public PsiReference findReferenceAt(int i) { return xmlTag.findReferenceAt(i); }
@Nullable @Contract(pure = true) public static PsiElement getNextNonWhitespaceElement(@NotNull final PsiElement element) { Validate.notNull(element); PsiElement nextSibling = element.getNextSibling(); while (null != nextSibling && ImpexPsiUtils.isWhiteSpace(nextSibling)) { nextSibling = nextSibling.getNextSibling(); } return nextSibling; }
@Override @NonNls @Contract( pure = true ) public String getText() { return xmlTag.getText(); }
@Override @Contract( pure = true ) public PsiElement getNavigationElement() { return xmlTag.getNavigationElement(); }
@Override @Contract( pure = true ) public PsiElement getOriginalElement() { return xmlTag.getOriginalElement(); }
@Contract(pure = true) private boolean isFoldable(@Nullable final PsiElement element) { return null != element && this.isSupportedType(element) && !(isLineBreak(element) && isHeaderLine(element.getPrevSibling())) && !(isLineBreak(element) && isUserRightsMacros(element.getPrevSibling())) && !(isLineBreak(element) && element.getNextSibling() == null); }
@Contract("_ -> !null") public static DiscreteSeekBar.OnProgressChangeListener forProgressChanged(OnProgressChangedListener listener) { return new SimpleOnProgressChangeListener() { @Override public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) { listener.onProgressChanged(seekBar, value, fromUser); } }; }
@Contract protected void clearHighlightedArea(@NotNull final Editor editor) { Validate.notNull(editor); if (!cache.asMap().isEmpty()) { final List<PsiElement> column = cache.getIfPresent(editor); cache.invalidate(editor); if (null != column) { ApplicationManager.getApplication() .invokeLater( () -> modifyHighlightedArea(editor, column, true)); } } }
@Nullable @Contract("null - > null") public static PetType getPetFromItem(@Nullable ItemStack item) { String id; if (ItemUtils.isEmpty(item) || (id = ItemUtils.getTag(item, ItemUtils.PET_TAG)) == null) { return null; } return PETS.get(id); }
@Override @NotNull @Contract( pure = true ) public PsiReference[] getReferences() { return xmlTag.getReferences(); }
@Override @Nullable @Contract( pure = true ) public PsiElement getContext() { return xmlTag.getContext(); }
@Override @Contract( pure = true ) public boolean isPhysical() { return xmlTag.isPhysical(); }
@Override @Contract( pure = true ) public boolean isEquivalentTo(PsiElement psiElement) { return xmlTag.isEquivalentTo(psiElement); }
@Contract("_ -> !null") public static DiscreteSeekBar.OnProgressChangeListener forStopTrackingTouch(OnStopTrackingTouchListener listener) { return new SimpleOnProgressChangeListener() { @Override public void onStopTrackingTouch(DiscreteSeekBar seekBar) { listener.onStopTrackingTouch(seekBar); } }; }
@Contract("null -> null") private static IE_Admin from(@Nullable JSONObject json) { if(json == null) return null; try { return new IE_Admin(json.getLong("account_id"), json.getString("name"), json.getLong("last_active")); } catch (JSONException e) { return null; } }
@Contract(pure = false) protected void highlightHeaderOfValueUnderCaret(@NotNull final Editor editor) { Validate.notNull(editor); final PsiElement header = ImpexPsiUtils.getHeaderOfValueGroupUnderCaret(editor); if (null == header) { this.clearHighlightedArea(editor); } else { this.highlightArea(editor, header); } }
@Contract(pure = true) static int getIntSafe(@Nullable SharedPreferences pref, @NonNull String key, int default_value) { try { return pref == null ? default_value : pref.getInt(key, default_value); } catch (Exception not_string) { return default_value; } }