コード例 #1
0
  private AMethodDeclCG consGetInstanceMethod(String name) {
    AClassTypeCG quoteClassType = new AClassTypeCG();
    quoteClassType.setName(name);

    AIdentifierVarExpCG instanceVar =
        transAssistant.getInfo().getExpAssistant().consIdVar(INSTANCE_FIELD, quoteClassType);

    AEqualsBinaryExpCG nullCompare = new AEqualsBinaryExpCG();
    nullCompare.setType(new ABoolBasicTypeCG());
    nullCompare.setLeft(instanceVar);
    nullCompare.setRight(info.getExpAssistant().consNullExp());

    AIdentifierVarExpCG instanceId =
        transAssistant
            .getInfo()
            .getExpAssistant()
            .consIdVar(INSTANCE_FIELD, quoteClassType.clone());

    ATypeNameCG typeName = new ATypeNameCG();
    typeName.setDefiningClass(null);
    typeName.setName(name);

    ANewExpCG newQuote = new ANewExpCG();
    newQuote.setName(typeName);
    newQuote.setType(quoteClassType);

    AAssignToExpStmCG assignInstance = new AAssignToExpStmCG();
    assignInstance.setTarget(instanceId);
    assignInstance.setExp(newQuote);

    AIfStmCG ensureInstance = new AIfStmCG();
    ensureInstance.setIfExp(nullCompare);
    ensureInstance.setThenStm(assignInstance);

    AReturnStmCG returnInstance = new AReturnStmCG();
    returnInstance.setExp(instanceVar.clone());

    ABlockStmCG body = new ABlockStmCG();
    body.getStatements().add(ensureInstance);
    body.getStatements().add(returnInstance);

    AMethodTypeCG methodType = new AMethodTypeCG();
    methodType.setResult(quoteClassType.clone());

    AMethodDeclCG getInstanceMethod = new AMethodDeclCG();

    getInstanceMethod.setImplicit(false);
    getInstanceMethod.setAbstract(false);
    getInstanceMethod.setAccess(IJavaConstants.PUBLIC);
    getInstanceMethod.setIsConstructor(false);
    getInstanceMethod.setName(GET_INSTANCE_METHOD);
    getInstanceMethod.setStatic(true);

    getInstanceMethod.setMethodType(methodType);
    getInstanceMethod.setBody(body);

    return getInstanceMethod;
  }
コード例 #2
0
  private AMethodDeclCG consQuoteCtor(String name) {
    AExternalTypeCG fieldType = new AExternalTypeCG();
    fieldType.setName(IJavaConstants.INT);

    AIdentifierVarExpCG hashcodeVar =
        transAssistant.getInfo().getExpAssistant().consIdVar(HASHCODE_FIELD, fieldType);

    AEqualsBinaryExpCG hashcodeCompare = new AEqualsBinaryExpCG();
    hashcodeCompare.setType(new ABoolBasicTypeCG());
    hashcodeCompare.setLeft(hashcodeVar);
    hashcodeCompare.setRight(consZero());

    AIdentifierVarExpCG hashCodeId =
        transAssistant.getInfo().getExpAssistant().consIdVar(HASHCODE_FIELD, consFieldType());

    AMethodTypeCG hashCodeMethodType = new AMethodTypeCG();
    hashCodeMethodType.setResult(consFieldType());

    ASuperVarExpCG superVar = new ASuperVarExpCG();
    superVar.setName(HASH_CODE_METHOD);
    superVar.setType(hashCodeMethodType);
    superVar.setIsLambda(false);
    superVar.setIsLocal(false);

    AApplyExpCG superCall = new AApplyExpCG();
    superCall.setType(consFieldType());
    superCall.setRoot(superVar);

    AAssignToExpStmCG assignHashcode = new AAssignToExpStmCG();
    assignHashcode.setTarget(hashCodeId);
    assignHashcode.setExp(superCall);

    AIfStmCG hashcodeCheck = new AIfStmCG();
    hashcodeCheck.setIfExp(hashcodeCompare);
    hashcodeCheck.setThenStm(assignHashcode);

    ABlockStmCG body = new ABlockStmCG();
    body.getStatements().add(hashcodeCheck);

    AClassTypeCG quoteClassType = new AClassTypeCG();
    quoteClassType.setName(name);

    AMethodTypeCG constructorType = new AMethodTypeCG();
    constructorType.setResult(quoteClassType);

    AMethodDeclCG ctor = consDefaultCtorSignature(name);
    ctor.setMethodType(constructorType);
    ctor.setBody(body);

    return ctor;
  }