private String getValue(QualifiedName name, ASTNode astNode, final JstType jstType) {

    TranslateInfo tInfo = TranslateCtx.ctx().getTranslateInfo(jstType);

    String typeName = name.getQualifier().getFullyQualifiedName();
    String fullName = tInfo.getImported(typeName);
    try {
      Class<?> toClass = Class.forName(fullName);
      Field field = toClass.getField(name.getName().toString());
      return (String) field.get(toClass);
    } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchFieldException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return null;
  }
Example #2
0
  public static MarkerAnnotation createValAnnotation(
      AST ast, Annotation original, int start, int end) {
    MarkerAnnotation out = null;
    try {
      out = Reflection.markerAnnotationConstructor.newInstance(ast);
    } catch (InstantiationException e) {
      throw Lombok.sneakyThrow(e);
    } catch (IllegalAccessException e) {
      throw Lombok.sneakyThrow(e);
    } catch (InvocationTargetException e) {
      throw Lombok.sneakyThrow(e);
    }

    if (out != null) {
      SimpleName valName = ast.newSimpleName("val");
      valName.setSourceRange(start, end - start + 1);
      if (original.type instanceof SingleTypeReference) {
        out.setTypeName(valName);
        setIndex(valName, 1);
      } else {
        SimpleName lombokName = ast.newSimpleName("lombok");
        lombokName.setSourceRange(start, end - start + 1);
        setIndex(lombokName, 1);
        setIndex(valName, 2);
        QualifiedName fullName = ast.newQualifiedName(lombokName, valName);
        setIndex(fullName, 1);
        fullName.setSourceRange(start, end - start + 1);
        out.setTypeName(fullName);
      }
      out.setSourceRange(start, end - start + 1);
    }

    return out;
  }
 /*
  * @see ASTVisitor#visit(QualifiedName)
  */
 @Override
 public boolean visit(QualifiedName node) {
   node.getQualifier().accept(this);
   this.fBuffer.append("."); // $NON-NLS-1$
   node.getName().accept(this);
   return false;
 }
  /*
   * Must be one of:
   * <ul>
   * <li>[result].length</li>
   * </ul>
   */
  private boolean validateLengthQuery(Expression lengthQuery) {
    if (lengthQuery instanceof QualifiedName) {
      QualifiedName qualifiedName = (QualifiedName) lengthQuery;
      SimpleName name = qualifiedName.getName();
      if (!LENGTH_QUERY.equals(name.getIdentifier())) return false;

      Name arrayAccess = qualifiedName.getQualifier();
      ITypeBinding accessType = arrayAccess.resolveTypeBinding();
      if (accessType == null) return false;

      if (!accessType.isArray()) return false;

      IBinding arrayBinding = arrayAccess.resolveBinding();
      if (arrayBinding == null) return false;

      fArrayBinding = arrayBinding;
      fArrayAccess = arrayAccess;
      return true;
    } else if (lengthQuery instanceof FieldAccess) {
      FieldAccess fieldAccess = (FieldAccess) lengthQuery;
      SimpleName name = fieldAccess.getName();
      if (!LENGTH_QUERY.equals(name.getIdentifier())) return false;

      Expression arrayAccess = fieldAccess.getExpression();
      ITypeBinding accessType = arrayAccess.resolveTypeBinding();
      if (accessType == null) return false;

      if (!accessType.isArray()) return false;

      IBinding arrayBinding = getBinding(arrayAccess);
      if (arrayBinding == null) return false;

      fArrayBinding = arrayBinding;
      fArrayAccess = arrayAccess;
      return true;
    }

    return false;
  }