private PsiElement findDeclaration(PsiElement element, String type) { if (!(element instanceof XmlAttributeValue)) { return null; } JSObjectLiteralExpression exports = WeexFileUtil.getExportsStatement(element); if (exports == null) { return null; } else { String valueName = CodeUtil.getVarNameFromMustache(((XmlAttributeValue) element).getValue()); if ("function".equals(type)) { return WeexFileUtil.getFunctionDeclaration(value, valueName); } else { return WeexFileUtil.getVarDeclaration(value, valueName); } } }
@Nullable private static JSProperty findProperty(@Nullable JSObjectLiteralExpression objectLiteralExpression, String name) { if(objectLiteralExpression == null) { return null; } for(JSProperty property : objectLiteralExpression.getProperties()) { if(Comparing.equal(property.getName(), name)) { return property; } } return null; }
@Override public void visitJSThisExpression(JSThisExpression jsThisExpression) { super.visitJSThisExpression(jsThisExpression); final JSObjectLiteralExpression containingObject = PsiTreeUtil.getParentOfType(jsThisExpression, JSObjectLiteralExpression.class); if (containingObject != null) { return; } final JSFunction containingFunction = PsiTreeUtil.getParentOfType(jsThisExpression, JSFunction.class); if (containingFunction != null) { return; } final XmlAttributeValue containingAttribute = PsiTreeUtil.getParentOfType(jsThisExpression, XmlAttributeValue.class); if (containingAttribute != null) { return; } final PsiFile containingFile = jsThisExpression.getContainingFile(); if (containingFile instanceof JSExpressionCodeFragment || containingFile.getContext() instanceof XmlAttributeValue) { return; } registerError(jsThisExpression); }
@Nullable public static JSProperty getProperty(@NotNull PsiElement position) { JSProperty property = PsiTreeUtil.getParentOfType(position, JSProperty.class, false); if (property != null) { JSObjectLiteralExpression objectLiteralExpression = ObjectUtils.tryCast(property.getParent(), JSObjectLiteralExpression.class); if (objectLiteralExpression != null) { return property; } } return null; }
@RequiredReadAction @Override @SuppressWarnings("unchecked") public Map parseValue(@NotNull Class type, @NotNull Type genericType, @NotNull PsiElement value) throws JomBadValueExpressionException { if(!(value instanceof JSObjectLiteralExpression)) { throw new JomBadValueExpressionException(); } Pair<Class, Type> valueType = JomCollectionValue.findValueTypeInsideGeneric(genericType, 1); // K, V JSProperty[] properties = ((JSObjectLiteralExpression) value).getProperties(); Map map = new LinkedHashMap(properties.length); for(JSProperty property : properties) { String name = property.getName(); if(name == null) { continue; } try { Object object = JomValueConverter.convertToObject(valueType.getFirst(), valueType.getSecond(), property.getValue()); map.put(name, object); } catch(JomBadValueExpressionException e) { // we dont interest in bad value } } return map; }
@NotNull @Override protected T compute() { //noinspection unchecked return (T) JomProxyInvocationHandler.createProxy(myFileDescriptor.getDefinitionClass(), PsiTreeUtil.findChildOfType(myPsiFile, JSObjectLiteralExpression.class)); }
@Override public void visitJSObjectLiteralExpression(JSObjectLiteralExpression jsObjectLiteralExpression) { super.visitJSObjectLiteralExpression(jsObjectLiteralExpression); final JSProperty[] properties = jsObjectLiteralExpression.getProperties(); final boolean[] matched = new boolean[properties.length]; Arrays.fill(matched, false); for (int i = 0; i < properties.length; i++) { if (matched[i]) { continue; } final JSProperty property1 = properties[i]; for (int j = i + 1; j < properties.length; j++) { if (matched[j]) { continue; } final JSProperty property2 = properties[j]; final String property1Name = property1.getName(); final String property2Name = property2.getName(); if(property1Name !=null && property2Name!=null && property1Name.equals(property2Name)) { registerError(property2.getFirstChild()); if (!matched[i]) { registerError(property1.getFirstChild()); } matched[i] = true; matched[j] = true; } } } }
@RequiredReadAction @SuppressWarnings("unchecked") public static Object convertToObject(@NotNull Class type, @NotNull Type genericType, @Nullable JSExpression value) throws JomBadValueExpressionException { if(value == null) { throw new JomBadValueExpressionException("No value for property"); } JomValueConverter.Converter converter = JomValueConverter.ourDefaultConverters.get(type); if(converter != null) { return converter.parseValue(type, genericType, value); } if(type.isArray()) { if(value instanceof JSArrayLiteralExpression) { Class componentType = type.getComponentType(); JSExpression[] expressions = ((JSArrayLiteralExpression) value).getExpressions(); List list = new ArrayList(expressions.length); for(JSExpression expression : expressions) { try { Object o = JomValueConverter.convertToObject(componentType, componentType, expression); list.add(o); } catch(JomBadValueExpressionException e) { // we dont interest in bad value } } return list.toArray((Object[]) Array.newInstance(componentType, list.size())); } } if(JomElement.class.isAssignableFrom(type)) { return JomProxyInvocationHandler.createProxy(type, value instanceof JSObjectLiteralExpression ? (JSObjectLiteralExpression) value : null); } throw new UnsupportedOperationException("Unsupported type: " + genericType); }
@NotNull public static JomElement createProxy(@NotNull Class<?> interfaceClass, @Nullable final JSObjectLiteralExpression objectLiteralExpression) { return (JomElement) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new JomProxyInvocationHandler(objectLiteralExpression)); }
public JomProxyInvocationHandler(@Nullable JSObjectLiteralExpression objectLiteralExpression) { myObjectLiteralExpression = objectLiteralExpression; }