Пример #1
0
 @Override
 public Void visitFieldDeclaration(FieldDeclaration node) {
   visit(node.getKeyword(), " ");
   visit(node.getFields());
   writer.print(";");
   return null;
 }
 // Declared in VariableDeclaration.jrag at line 124
 private FieldDeclaration rewriteRule0() {
   {
     FieldDeclaration decl =
         getVariableDecl(0).createFieldDeclarationFrom(getModifiers(), getTypeAccess());
     decl.setStart(start); // copy location information
     decl.setEnd(end); // copy location information
     return decl;
   }
 }
Пример #3
0
 @Override
 public boolean visit(FieldDeclaration node) {
   String typName = node.getType().toString();
   List<VariableDeclarationFragment> vars = node.fragments();
   for (VariableDeclarationFragment var : vars) {
     map.put(var.getName().getIdentifier(), typName);
   }
   return true;
 }
 public boolean visit(FieldDeclaration declaration) throws Exception {
   // This is constant declaration:
   ISourceElementRequestor.FieldInfo info = new ISourceElementRequestor.FieldInfo();
   info.modifiers = Modifiers.AccConstant | Modifiers.AccPublic | Modifiers.AccFinal;
   info.name = declaration.getName();
   info.nameSourceStart = declaration.getNameStart();
   info.nameSourceEnd = declaration.getNameEnd() - 1;
   info.declarationStart = declaration.sourceStart();
   fRequestor.enterField(info);
   fRequestor.exitField(declaration.sourceEnd() - 1);
   return true;
 }
Пример #5
0
 /** @apilevel internal */
 private HashMap memberFieldsMap_compute() {
   HashMap map = new HashMap(localFieldsMap());
   for (Iterator outerIter = superinterfacesIterator(); outerIter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) outerIter.next();
     for (Iterator iter = typeDecl.fieldsIterator(); iter.hasNext(); ) {
       FieldDeclaration f = (FieldDeclaration) iter.next();
       if (f.accessibleFrom(this) && !f.isPrivate() && !localFieldsMap().containsKey(f.name())) {
         putSimpleSetElement(map, f.name(), f);
       }
     }
   }
   return map;
 }
Пример #6
0
 /** @apilevel internal */
 private SimpleSet memberFields_compute(String name) {
   SimpleSet fields = localFields(name);
   if (!fields.isEmpty()) return fields;
   for (Iterator outerIter = superinterfacesIterator(); outerIter.hasNext(); ) {
     TypeDecl typeDecl = (TypeDecl) outerIter.next();
     for (Iterator iter = typeDecl.memberFields(name).iterator(); iter.hasNext(); ) {
       FieldDeclaration f = (FieldDeclaration) iter.next();
       if (f.accessibleFrom(this) && !f.isPrivate()) {
         fields = fields.add(f);
       }
     }
   }
   return fields;
 }
Пример #7
0
  // Declared in AnonymousClasses.jrag at line 52
  private AnonymousDecl rewriteRule0() {
    {
      setModifiers(new Modifiers(new List().add(new Modifier("final"))));

      ConstructorDecl constructor = new ConstructorDecl();
      addBodyDecl(constructor);

      constructor.setModifiers((Modifiers) constructorDecl().getModifiers().fullCopy());
      String name = "Anonymous" + nextAnonymousIndex();
      setID(name);
      constructor.setID(name);

      List parameterList = new List();
      for (int i = 0; i < constructorDecl().getNumParameter(); i++) {
        parameterList.add(
            new ParameterDeclaration(
                constructorDecl().getParameter(i).type().createBoundAccess(),
                constructorDecl().getParameter(i).name()));
      }
      constructor.setParameterList(parameterList);

      List argList = new List();
      for (int i = 0; i < constructor.getNumParameter(); i++)
        argList.add(new VarAccess(constructor.getParameter(i).name()));
      constructor.setConstructorInvocation(
          new ExprStmt(new SuperConstructorAccess("super", argList)));
      constructor.setBlock(new Block());

      HashSet set = new HashSet();
      for (int i = 0; i < getNumBodyDecl(); i++) {
        if (getBodyDecl(i) instanceof InstanceInitializer) {
          InstanceInitializer init = (InstanceInitializer) getBodyDecl(i);
          set.addAll(init.exceptions());
        } else if (getBodyDecl(i) instanceof FieldDeclaration) {
          FieldDeclaration f = (FieldDeclaration) getBodyDecl(i);
          if (f.isInstanceVariable()) {
            set.addAll(f.exceptions());
          }
        }
      }
      List exceptionList = new List();
      for (Iterator iter = set.iterator(); iter.hasNext(); ) {
        TypeDecl exceptionType = (TypeDecl) iter.next();
        if (exceptionType.isNull()) exceptionType = typeNullPointerException();
        exceptionList.add(exceptionType.createQualifiedAccess());
      }
      constructor.setExceptionList(exceptionList);
      return this;
    }
  }
Пример #8
0
 FieldDeclarationWrapper(FieldDeclaration declaration) {
   this.declaration = declaration;
   if (declaration.getVariables().size() != 1) {
     throw new UnsupportedOperationException(
         "Not yet implemented: multiple variables in one field decl.");
   }
 }
 // Declared in VariableDeclaration.jrag at line 135
 private List rewriteTypeDecl_getBodyDecl() {
   {
     List varList = new List();
     for (int j = 0; j < getNumVariableDecl(); j++) {
       FieldDeclaration f =
           getVariableDecl(j)
               .createFieldDeclarationFrom(
                   (Modifiers) getModifiers().fullCopy(), (Access) getTypeAccess().fullCopy());
       if (j == 0) f.setStart(start);
       else {
         f.getModifiersNoTransform().clearLocations();
         f.getTypeAccessNoTransform().clearLocations();
       }
       f.setFieldDecl(this);
       varList.add(f);
     }
     return varList;
   }
 }
Пример #10
0
    ClassDefItem toClassDefItem() {
      if (!declared) {
        throw new IllegalStateException(
            "Undeclared type "
                + type
                + " declares members: "
                + fields.keySet()
                + " "
                + methods.keySet());
      }

      DexOptions dexOptions = new DexOptions();
      dexOptions.targetApiLevel = DexFormat.API_NO_EXTENDED_OPCODES;

      CstType thisType = type.constant;

      ClassDefItem out =
          new ClassDefItem(
              thisType, flags, supertype.constant, interfaces.ropTypes, new CstString(sourceFile));

      for (MethodDeclaration method : methods.values()) {
        EncodedMethod encoded = method.toEncodedMethod(dexOptions);
        if (method.isDirect()) {
          out.addDirectMethod(encoded);
        } else {
          out.addVirtualMethod(encoded);
        }
      }
      for (FieldDeclaration field : fields.values()) {
        EncodedField encoded = field.toEncodedField();
        if (field.isStatic()) {
          out.addStaticField(encoded, Constants.getConstant(field.staticValue));
        } else {
          out.addInstanceField(encoded);
        }
      }

      return out;
    }
Пример #11
0
  @Override
  public void add(FieldInfo field) {
    FieldDeclaration fieldDeclaration;

    if (field instanceof FieldDeclarationWrapper) {
      val wrapper = (FieldDeclarationWrapper) field;
      fieldDeclaration = (FieldDeclaration) wrapper.declaration.clone();
      fieldDeclaration.setAnnotations(Collections.emptyList());
      changeTypeContext(wrapper.getContext(), getContext(), fieldDeclaration);
    } else {
      fieldDeclaration = new FieldDeclaration();
      val vars = new ArrayList<VariableDeclarator>();
      vars.add(new VariableDeclarator(new VariableDeclaratorId("unknown")));
      fieldDeclaration.setVariables(vars);
      new FieldDeclarationWrapper(fieldDeclaration).setAll(field);
    }

    addMember(fieldDeclaration);

    val result = new FieldDeclarationWrapper(fieldDeclaration);
    if (!field.similar(result))
      throw new TransformationException(
          "After adding to class, didn't match. added: " + field + " result: " + result);
  }
Пример #12
0
  public void visit(FieldDeclaration n, Object arg) {
    if (n.getJavaDoc() != null) {
      n.getJavaDoc().accept(this, arg);
    }
    printMemberAnnotations(n.getAnnotations(), arg);
    printModifiers(n.getModifiers());
    n.getType().accept(this, arg);

    for (Iterator<VariableDeclarator> i = n.getVariables().iterator(); i.hasNext(); ) {
      VariableDeclarator var = i.next();
      var.accept(this, arg);
      if (i.hasNext()) {}
    }
  }
Пример #13
0
  public boolean visit(FieldDeclaration decl) throws Exception {
    // This is constant declaration:
    int modifiers = decl.getModifiers();
    modifiers = markAsDeprecated(modifiers, decl);

    modifyDeclaration(
        decl,
        new DeclarationInfo(
            IModelElement.FIELD,
            modifiers,
            decl.sourceStart(),
            decl.sourceEnd() - decl.sourceStart(),
            decl.getNameStart(),
            decl.getNameEnd() - decl.getNameStart(),
            decl.getName(),
            null,
            encodeDocInfo(decl),
            null,
            null));

    return visitGeneral(decl);
  }
 /*
  * @see ASTVisitor#visit(FieldDeclaration)
  */
 @Override
 public boolean visit(FieldDeclaration node) {
   if (node.getJavadoc() != null) {
     node.getJavadoc().accept(this);
   }
   if (node.getAST().apiLevel() >= JLS3) {
     printModifiers(node.modifiers());
   }
   node.getType().accept(this);
   this.fBuffer.append(" "); // $NON-NLS-1$
   for (Iterator<VariableDeclarationFragment> it = node.fragments().iterator(); it.hasNext(); ) {
     VariableDeclarationFragment f = it.next();
     f.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(", "); // $NON-NLS-1$
     }
   }
   this.fBuffer.append(";"); // $NON-NLS-1$
   return false;
 }
Пример #15
0
  /**
   * @ast method
   * @aspect GenerateClassfile
   * @declaredat /home/uoji/JastAddJ/Java1.4Backend/GenerateClassfile.jrag:142
   */
  public void generateClassfile() {
    super.generateClassfile();
    String fileName = destinationPath() + File.separator + constantPoolName() + ".class";
    if (options().verbose()) System.out.println("Writing class file to " + fileName);
    try {
      ConstantPool cp = constantPool();
      // force building of constant pool
      cp.addClass(constantPoolName());
      cp.addClass("java/lang/Object");
      for (int i = 0; i < getNumSuperInterfaceId(); i++) {
        cp.addClass(getSuperInterfaceId(i).type().constantPoolName());
      }
      for (Iterator iter = bcFields().iterator(); iter.hasNext(); ) {
        FieldDeclaration field = (FieldDeclaration) iter.next();
        cp.addUtf8(field.name());
        cp.addUtf8(field.type().typeDescriptor());
        field.attributes();
      }
      for (Iterator iter = bcMethods().iterator(); iter.hasNext(); ) {
        Object obj = iter.next();
        if (obj instanceof MethodDecl) {
          MethodDecl m = (MethodDecl) obj;
          cp.addUtf8(m.name());
          cp.addUtf8(m.descName());
          m.attributes();
        }
      }
      attributes();

      if (hasClinit()) {
        cp.addUtf8("<clinit>");
        cp.addUtf8("()V");
        clinit_attributes();
      }

      // actual classfile generation
      File dest = new File(fileName);
      File parentFile = dest.getParentFile();
      if (parentFile != null) parentFile.mkdirs();

      FileOutputStream f = new FileOutputStream(fileName);
      DataOutputStream out = new DataOutputStream(new BufferedOutputStream(f));
      out.writeInt(magicHeader());
      out.writeChar(minorVersion());
      out.writeChar(majorVersion());
      cp.emit(out);
      int flags = flags();
      if (isNestedType()) flags = mangledFlags(flags);
      if (isInterfaceDecl()) flags |= Modifiers.ACC_INTERFACE;
      out.writeChar(flags);
      out.writeChar(cp.addClass(constantPoolName()));
      out.writeChar(cp.addClass("java/lang/Object"));
      if (getNumSuperInterfaceId() == 1 && getSuperInterfaceId(0).type().isObject())
        out.writeChar(0);
      else out.writeChar(getNumSuperInterfaceId());
      for (int i = 0; i < getNumSuperInterfaceId(); i++) {
        TypeDecl typeDecl = getSuperInterfaceId(i).type();
        if (typeDecl.isInterfaceDecl()) out.writeChar(cp.addClass(typeDecl.constantPoolName()));
      }
      Collection fields = bcFields();
      out.writeChar(fields.size());
      for (Iterator iter = fields.iterator(); iter.hasNext(); ) {
        FieldDeclaration field = (FieldDeclaration) iter.next();
        out.writeChar(field.flags());
        out.writeChar(cp.addUtf8(field.name()));
        out.writeChar(cp.addUtf8(field.type().typeDescriptor()));
        out.writeChar(field.attributes().size());
        for (Iterator itera = field.attributes().iterator(); itera.hasNext(); )
          ((Attribute) itera.next()).emit(out);
      }
      Collection methods = bcMethods();
      out.writeChar(methods.size() + (hasClinit() ? 1 : 0));
      for (Iterator iter = methods.iterator(); iter.hasNext(); ) {
        BodyDecl b = (BodyDecl) iter.next();
        b.generateMethod(out, cp);
      }
      if (hasClinit()) {
        out.writeChar(Modifiers.ACC_STATIC);
        out.writeChar(cp.addUtf8("<clinit>"));
        out.writeChar(cp.addUtf8("()V"));
        out.writeChar(clinit_attributes().size());
        for (Iterator itera = clinit_attributes().iterator(); itera.hasNext(); )
          ((Attribute) itera.next()).emit(out);
      }
      out.writeChar(attributes().size());
      for (Iterator itera = attributes().iterator(); itera.hasNext(); )
        ((Attribute) itera.next()).emit(out);

      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #16
0
 @Override
 public void setAccessFlags(AccessFlags accessFlags) {
   declaration.setModifiers(accessFlags.access);
 }
 final void setType(OJClass type) throws CannotAlterException {
   definition.setTypeSpecifier(TypeName.forOJClass(type));
 }
Пример #18
0
 @Override
 public String getName() {
   return declaration.getVariables().get(0).getId().getName();
 }
 final void setName(String name) throws CannotAlterException {
   definition.setVariable(name);
 }
 final void setModifiers(int mods) throws CannotAlterException {
   definition.setModifiers(new ModifierList(mods));
 }
 OJModifier getModifiers() {
   return OJModifier.forParseTree(definition.getModifiers());
 }
 OJClass getType() {
   String type_name = definition.getTypeSpecifier().toString();
   return Toolbox.forNameAnyway(env, type_name);
 }
Пример #23
0
 @Override
 public void setName(String name) {
   declaration.getVariables().get(0).getId().setName(name);
 }
 String getName() {
   return definition.getName();
 }
Пример #25
0
 static void changeTypeContext(ResolutionContext old, ResolutionContext new_, FieldDeclaration f) {
   f.setType(changeTypeContext(old, new_, f.getType()));
 }
Пример #26
0
 @Override
 public List<Annotation> getAnnotations() {
   return SourceInfo.this.getAnnotationsInternal(declaration.getAnnotations());
 }
Пример #27
0
 @Override
 public void setType(Type type) {
   declaration.setType(SourceInfo.this.setType(type, declaration.getType()));
 }
Пример #28
0
 @Override
 public Type getType() {
   return getContext().resolve(declaration.getType());
 }
Пример #29
0
 @Override
 public AccessFlags getAccessFlags() {
   return new AccessFlags(declaration.getModifiers());
 }