javassist.expr.FieldAccess Java Examples

The following examples show how to use javassist.expr.FieldAccess. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReparentClassTransformer.java    From DroidAssist with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean execute(CtClass inputClass, String inputClassName, FieldAccess fieldAccess)
        throws CannotCompileException, NotFoundException {
    if (!fieldAccess.isStatic()
            && fieldAccess.getClassName().equals(getSourceDeclaringClassName())) {

        Logger.warning(getCategoryName() + " reset parent for class " + inputClassName
                + " adapt super field access" +
                " at " + inputClassName + ".java" + ":" + fieldAccess.getLineNumber());

        String fieldName = fieldAccess.getFieldName();
        if (fieldAccess.isReader()) {
            fieldAccess.replace("$_=super." + fieldName + ";");
        } else if (fieldAccess.isWriter()) {
            fieldAccess.replace("super." + fieldName + "=$1;");
        }
        return true;
    }
    return false;
}
 
Example #2
Source File: Dagger2Extension.java    From EasyMVP with Apache License 2.0 6 votes vote down vote up
private void replacePresenterWithPresenterProvider(CtClass membersInjectorClass,
                                                   final String presenterFieldName,
                                                   final String presenterProviderField)
        throws NotFoundException, CannotCompileException {
    CtMethod injectMembers = membersInjectorClass.getDeclaredMethod("injectMembers");
    final int[] injectedPresenterLineNumber = {-1};
    injectMembers.instrument(new ExprEditor() {
        @Override
        public void edit(FieldAccess f) throws CannotCompileException {
            if (f.isWriter() && f.getFieldName().equals(presenterFieldName)) {
                injectedPresenterLineNumber[0] = f.getLineNumber();
            }
        }
    });
    if (injectedPresenterLineNumber[0] != -1) {
        deleteLine(injectMembers, injectedPresenterLineNumber[0]);
        fillPresenterProviderInView(injectMembers, presenterProviderField, injectedPresenterLineNumber[0]);
        viewDelegateBinder.log("DaggerExtension",
                               "Remove presenter injection from " +
                                       membersInjectorClass.getSimpleName() +
                                       " and replace with our presenterProvider ");
        viewDelegateBinder.writeClass(membersInjectorClass);
    }
}
 
Example #3
Source File: MainMenuModList.java    From ModTheSpire with MIT License 6 votes vote down vote up
public static ExprEditor Instrument()
{
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess f) throws CannotCompileException
        {
            if (f.getFieldName().equals("VERSION_INFO")) {
                f.replace(String.format("$_ = %s.alterVersion($proceed($$));", MainMenuModList.class.getName()));
            }
        }

        @Override
        public void edit(MethodCall m) throws CannotCompileException
        {
            if (m.getMethodName().equals("renderSmartText")) {
                m.replace(
                    "{" +
                        String.format("$5 = %s.getSmartHeight($2, $3, $6, $7);", MainMenuModList.class.getName()) +
                        "$proceed($$);" +
                        "}"
                );
            }
        }
    };
}
 
Example #4
Source File: FieldAccessReplaceTransformer.java    From DroidAssist with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean execute(
        CtClass inputClass,
        String inputClassName,
        FieldAccess fieldAccess)
        throws CannotCompileException, NotFoundException {

    String insnClassName = fieldAccess.getClassName();
    String insnSignature = fieldAccess.getSignature();
    String insnFieldName = fieldAccess.getFieldName();

    if (!isMatchFieldSource(insnClassName, insnSignature, insnFieldName)
            || !meetConditions(fieldAccess)) {
        return false;
    }

    String target = getTarget();
    String replacement = replaceInstrument(fieldAccess, target);

    Logger.warning(getPrettyName() + " by: " +
            (fieldAccess.isWriter() ? " write" : " read") + replacement + " at " +
            inputClassName + ".java" + ":" + fieldAccess.getLineNumber());
    return true;
}
 
Example #5
Source File: SourceTargetTransformer.java    From DroidAssist with Apache License 2.0 5 votes vote down vote up
protected String getReplaceStatement(FieldAccess fieldAccess, String statement) {
    int line = fieldAccess.getLineNumber();
    String name = fieldAccess.getFieldName();
    String className = fieldAccess.getClassName();
    String fileName = fieldAccess.getFileName();
    return getReplaceStatement(statement, line, name, className, fileName);
}
 
Example #6
Source File: AlwaysEnableCustomMode.java    From ModTheSpire with MIT License 5 votes vote down vote up
public static ExprEditor Instrument()
{
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess f) throws CannotCompileException
        {
            if (f.getClassName().equals("com.megacrit.cardcrawl.screens.stats.CharStat")
                && f.getFieldName().equals("highestDaily")) {
                f.replace("$_ = 1;");
            }
        }
    };
}
 
Example #7
Source File: TopPanelModList.java    From ModTheSpire with MIT License 5 votes vote down vote up
public static ExprEditor Instrument()
{
    return new ExprEditor() {
        private int count = 0;

        @Override
        public void edit(MethodCall m) throws CannotCompileException
        {
            if (m.getMethodName().equals("renderFontRightTopAligned")) {
                ++count;
                if (count == 2) {
                    m.replace(
                        "{" +
                            String.format("$5 -= 24 * %s.scale;", Settings.class.getName()) +
                            "$_ = $proceed($$);" +
                            "}"
                    );
                }
            }
        }

        @Override
        public void edit(FieldAccess f) throws CannotCompileException
        {
            if (f.getFieldName().equals("VERSION_NUM")) {
                f.replace(String.format("$_ = %s.alterVersion2($proceed($$));", MainMenuModList.class.getName()));
            }
        }
    };
}
 
Example #8
Source File: BranchingUpgradesPatch.java    From StSLib with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        private boolean accessedUpgradeHb = false;
        @Override
        public void edit(FieldAccess f) throws CannotCompileException
        {
            if (accessedUpgradeHb && f.getFieldName().equals("hovered")) {
                f.replace("$_ = $proceed($$) || ((" + Hitbox.class.getName() + ") " + BranchUpgradeButton.class.getName() + ".branchUpgradeHb.get(this)).hovered;");
                accessedUpgradeHb = false;
            } else if (f.getFieldName().equals("upgradeHb")) {
                accessedUpgradeHb = true;
            }
        }
    };
}
 
Example #9
Source File: RetainHpPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess fieldAccess) throws CannotCompileException {
            if (fieldAccess.getClassName().equals(AbstractMonster.class.getName()) && fieldAccess.getFieldName().equals("lastDamageTaken")) {
                fieldAccess.replace("{ damageAmount = " + RetainHpPatch.class.getName() + ".retainHP(this, info, damageAmount); $proceed($$); }");
            }
        }
    };
}
 
Example #10
Source File: BurningPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess f) throws CannotCompileException {
            if (f.getClassName().contains(AbstractCreature.class.getName()) && f.getFieldName().equals("blockColor")) {
                f.replace(String.format("{ $_ = %1$s.renderBurningBlock(this, $proceed()); }",
                        BurningPatch.class.getName()));
            }
        }
    };
}
 
Example #11
Source File: BottledMemoryPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess fieldAccess) throws CannotCompileException {
            // replacing the "((AbstractCard)AbstractDungeon.player.masterDeck.group.get(i)).inBottleFlame"
            // expression in the original if statement to add in an "|| card.hasTag(LEGENDARY)"
            if (fieldAccess.getClassName().equals(AbstractCard.class.getName()) && fieldAccess.getFieldName().equals("inBottleFlame")) {
                fieldAccess.replace("{ $_ = ($proceed() || stsjorbsmod.patches.BottledMemoryPatch.isInBottleMemory($0)); }");
            }
        }
    };
}
 
Example #12
Source File: BottledMemoryPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess fieldAccess) throws CannotCompileException {
            // Transforms `!inBottleTornado` into `!(inBottleTornado || inBottleMemory)`
            // Logically equivalent to `!inBottleTornado && !inBottleMemory`
            if (fieldAccess.getClassName().equals(AbstractCard.class.getName()) && fieldAccess.getFieldName().equals("inBottleTornado")) {
                fieldAccess.replace("{ $_ = ($proceed() || stsjorbsmod.patches.BottledMemoryPatch.isInBottleMemory(c)); }");
            }
        }
    };
}
 
Example #13
Source File: VoiceoverMasterPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess fieldAccess) throws CannotCompileException {
            if (fieldAccess.getClassName().equals(Settings.class.getName()) && fieldAccess.getFieldName().equals("MASTER_VOLUME")) {
                fieldAccess.replace("{" +
                        "$_ = ($proceed() * " + VoiceoverMaster.class.getName() +".MASTER_DAMPENING_FACTOR);" +
                        "}");
            }
        }
    };
}
 
Example #14
Source File: NeowEventPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        public void edit(FieldAccess methodCall) throws CannotCompileException {
            if (methodCall.getClassName().equals(NeowEvent.class.getName()) && methodCall.getFieldName().equals("waitingToSave")) {
                methodCall.replace(String.format(
                        "{ %1$s.doAtStartOfAct(); $_ = $proceed($$); }",
                        AtStartOfActRelicSubscriber.class.getName()));
            }
        }
    };
}
 
Example #15
Source File: AbstractMonsterEscapeLeftPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess f) throws CannotCompileException {
            if (f.getClassName().contains(Settings.class.getName()) && f.getFieldName().equals("scale")) {
                f.replace(String.format("{" +
                        "$_ = $proceed() * (%1$s.isLeftOfPlayer(this) ? -1.0F : 1.0F);" +
                        "}",
                        AbstractMonsterEscapeLeftPatch.class.getName()));
            }
        }
    };
}
 
Example #16
Source File: TipHelperTouchScreenFix.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess f) throws CannotCompileException {
            if (f.getClassName().contains(AbstractPlayer.class.getName()) && f.getFieldName().equals("isHoveringDropZone")) {
                f.replace(String.format("{" +
                                "$_ = (%1$s.isInHoverTargetMode($0) && $proceed());" +
                                "}",
                        TipHelperTouchScreenFix.class.getName()));
            }
        }
    };
}
 
Example #17
Source File: TrueDamagePatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess fa) throws CannotCompileException {
            // suppress the initial Intangible effect on info.output
            if (fa.getClassName().equals(DamageInfo.class.getName()) &&
                fa.getFieldName().equals("output") &&
                fa.isWriter())
            {
                fa.replace(String.format("{ if (!%1$s.isTrueDamage($0)) { $_ = $proceed($$); } }", TrueDamagePatchName));
            }
        }

        @Override
        public void edit(MethodCall mc) throws CannotCompileException {
            String cls = mc.getClassName();
            String method = mc.getMethodName();

            // clamp the onAttackToChangeDamage, onAttackedToChangeDamage, and onAttacked calls
            if ((cls.equals(AbstractPower.class.getName()) || cls.equals(AbstractRelic.class.getName())) &&
                (method.equals("onAttackToChangeDamage") || method.equals("onAttackedToChangeDamage") || method.equals("onAttacked")))
            {
                mc.replace(String.format("{ $_ = %1$s.updateDamage($1, $2, $proceed($$)); }", TrueDamagePatchName));
                return;
            }
        }
    };
}
 
Example #18
Source File: AtStartOfTurnPreLoseBlockPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess f) throws CannotCompileException {
            if (f.getClassName().contains(AbstractDungeon.class.getName()) && f.getFieldName().equals("topLevelEffects")) {
                f.replace(String.format("{ %1$s.applyStartOfTurnPreLoseBlockPowers(); $_ = $proceed(); }",
                        AtStartOfTurnPreLoseBlockPatch.class.getName()));
            }
        }
    };
}
 
Example #19
Source File: EphemeralPatch.java    From jorbs-spire-mod with MIT License 5 votes vote down vote up
public static ExprEditor Instrument() {
    return new ExprEditor() {
        @Override
        public void edit(FieldAccess f) throws CannotCompileException {
            if (f.getClassName().contains(AbstractPlayer.class.getName()) && f.getFieldName().equals("discardPile")) {
                f.replace(String.format("{" +
                                "$_ = (%1$s.isEphemeral(this.card) ? $0.exhaustPile : $proceed());" +
                                "}",
                        EphemeralPatch.class.getName()));
            }
        }
    };
}
 
Example #20
Source File: FieldAccessAroundTransformer.java    From DroidAssist with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean execute(
        CtClass inputClass,
        String inputClassName,
        FieldAccess fieldAccess)
        throws CannotCompileException, NotFoundException {

    String insnClassName = fieldAccess.getClassName();
    String insnSignature = fieldAccess.getSignature();
    String insnFieldName = fieldAccess.getFieldName();

    if (!isMatchFieldSource(insnClassName, insnSignature, insnFieldName)
            || !meetConditions(fieldAccess)) {
        return false;
    }

    String before = getTargetBefore();
    String after = getTargetAfter();

    String proceed = fieldAccess.isWriter() ? "$proceed($$);" : "$_=$proceed($$);";
    String statement = "{" + before + proceed + after + "}";
    String replacement = replaceInstrument(fieldAccess, statement);

    Logger.warning(getPrettyName() + " by: " + replacement
            + " at " + inputClassName + ".java" + ":" + fieldAccess.getLineNumber());
    return true;
}
 
Example #21
Source File: ExprExecTransformer.java    From DroidAssist with Apache License 2.0 5 votes vote down vote up
protected boolean execute(
        CtClass inputClass,
        String inputClassName,
        FieldAccess fieldAccess)
        throws CannotCompileException, NotFoundException {
    return false;
}
 
Example #22
Source File: FieldAccessInsertTransformer.java    From DroidAssist with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean execute(
        CtClass inputClass,
        String inputClassName,
        FieldAccess fieldAccess)
        throws CannotCompileException, NotFoundException {

    String insnClassName = fieldAccess.getClassName();
    String insnSignature = fieldAccess.getSignature();
    String insnFieldName = fieldAccess.getFieldName();

    if (!isMatchFieldSource(insnClassName, insnSignature, insnFieldName)
            || !meetConditions(fieldAccess)) {
        return false;
    }

    String target = getTarget();
    String proceed = fieldAccess.isWriter() ? "$proceed($$);" : "$_=$proceed($$);";
    String statement = ""
            + "{"
            + (isAsBefore() ? target : "")
            + proceed
            + (isAsAfter() ? target : "")
            + "}";

    String replacement = replaceInstrument(fieldAccess, statement);

    Logger.warning(getPrettyName() + " by: " + replacement
            + " at " + inputClassName + ".java" + ":" + fieldAccess.getLineNumber());
    return true;
}
 
Example #23
Source File: SourceTargetTransformer.java    From DroidAssist with Apache License 2.0 5 votes vote down vote up
protected String replaceInstrument(
        FieldAccess fieldAccess,
        String statement)
        throws CannotCompileException {
    String replacement = getReplaceStatement(fieldAccess, statement);
    try {
        String s = replacement.replaceAll("\n", "");
        fieldAccess.replace(s);
    } catch (CannotCompileException e) {
        Logger.error("Replace field access instrument error with statement: "
                + statement + "\n", e);
        throw e;
    }
    return replacement;
}
 
Example #24
Source File: CardBannerImageRarityPatch.java    From jorbs-spire-mod with MIT License 4 votes vote down vote up
@Override
public void edit(FieldAccess fieldAccess) throws CannotCompileException {
    if (fieldAccess.getClassName().equals(AbstractCard.class.getName()) && fieldAccess.getFieldName().equals("rarity")) {
        fieldAccess.replace(String.format("{ $_ = %1$s.getBannerRarity($0); }", CardBannerImageRarityPatch.class.getName()));
    }
}
 
Example #25
Source File: FieldAccessAroundTransformer.java    From DroidAssist with Apache License 2.0 4 votes vote down vote up
private boolean meetConditions(FieldAccess fieldAccess) {
    return fieldAccess.isWriter() == fieldWrite;
}
 
Example #26
Source File: FieldAccessReplaceTransformer.java    From DroidAssist with Apache License 2.0 4 votes vote down vote up
private boolean meetConditions(FieldAccess fieldAccess) {
    return fieldAccess.isWriter() == fieldWrite;
}
 
Example #27
Source File: FieldAccessInsertTransformer.java    From DroidAssist with Apache License 2.0 4 votes vote down vote up
private boolean meetConditions(FieldAccess fieldAccess) {
    return fieldAccess.isWriter() == fieldWrite;
}
 
Example #28
Source File: MatchFinderExprEditor.java    From ModTheSpire with MIT License 4 votes vote down vote up
@Override
public void edit(FieldAccess expr) {
    doMatch(Expectation.FIELD_ACCESS, expr);
}
 
Example #29
Source File: Matcher.java    From ModTheSpire with MIT License 4 votes vote down vote up
public boolean match(Expr toMatch) {
    FieldAccess expr = (FieldAccess) toMatch;

    return expr.getClassName().equals(className) &&
            expr.getFieldName().equals(fieldName);
}