Search in sources :

Example 1 with JavaRecursiveElementVisitor

use of com.intellij.psi.JavaRecursiveElementVisitor in project Main by SpartanRefactoring.

the class Playground method spartanizeButtonClicked.

private void spartanizeButtonClicked() {
    if (inputArea.getText().trim().isEmpty()) {
        return;
    }
    Toolbox.getInstance().playground = true;
    PsiFile file = null;
    final boolean[] worked = { true };
    int i;
    for (i = 0; worked[0] && i < before.length; i++, worked[0] = true) {
        file = PsiFileFactory.getInstance(Utils.getProject()).createFileFromText(JavaLanguage.INSTANCE, before[i] + inputArea.getText() + after[i]);
        file.accept(new JavaRecursiveElementVisitor() {

            @Override
            public void visitErrorElement(PsiErrorElement element) {
                worked[0] = false;
                super.visitErrorElement(element);
            }
        });
        if (worked[0]) {
            break;
        }
    }
    if (i < before.length && file != null) {
        Spartanizer.spartanizeFileOnePass(file);
        outputArea.setText(fixString(file.getText(), i));
    } else {
        outputArea.setText(inputArea.getText());
    }
    Toolbox.getInstance().playground = false;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiFile(com.intellij.psi.PsiFile) JavaRecursiveElementVisitor(com.intellij.psi.JavaRecursiveElementVisitor)

Example 2 with JavaRecursiveElementVisitor

use of com.intellij.psi.JavaRecursiveElementVisitor in project smali by JesusFreke.

the class SmaliCodeFragmentFactory method wrapContext.

private PsiElement wrapContext(final Project project, final PsiElement originalContext) {
    if (project.isDefault())
        return originalContext;
    final List<LazyValue> lazyValues = Lists.newArrayList();
    SmaliInstruction currentInstruction = (SmaliInstruction) PsiUtil.searchBackward(originalContext, PsiMatchers.hasClass(SmaliInstruction.class), PsiMatchers.hasClass(SmaliMethod.class));
    if (currentInstruction == null) {
        currentInstruction = (SmaliInstruction) PsiUtil.searchForward(originalContext, PsiMatchers.hasClass(SmaliInstruction.class), PsiMatchers.hasClass(SmaliMethod.class));
        if (currentInstruction == null) {
            return originalContext;
        }
    }
    final SmaliMethod containingMethod = currentInstruction.getParentMethod();
    AnalyzedInstruction analyzedInstruction = currentInstruction.getAnalyzedInstruction();
    if (analyzedInstruction == null) {
        return originalContext;
    }
    final int firstParameterRegister = containingMethod.getRegisterCount() - containingMethod.getParameterRegisterCount();
    final Map<String, String> registerMap = Maps.newHashMap();
    StringBuilder variablesText = new StringBuilder();
    for (int i = 0; i < containingMethod.getRegisterCount(); i++) {
        int parameterRegisterNumber = i - firstParameterRegister;
        RegisterType registerType = analyzedInstruction.getPreInstructionRegisterType(i);
        switch(registerType.category) {
            case RegisterType.UNKNOWN:
            case RegisterType.UNINIT:
            case RegisterType.CONFLICTED:
            case RegisterType.LONG_HI:
            case RegisterType.DOUBLE_HI:
                continue;
            case RegisterType.NULL:
            case RegisterType.ONE:
            case RegisterType.INTEGER:
                variablesText.append("int v").append(i).append(";\n");
                registerMap.put("v" + i, "I");
                if (parameterRegisterNumber >= 0) {
                    variablesText.append("int p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "I");
                }
                break;
            case RegisterType.BOOLEAN:
                variablesText.append("boolean v").append(i).append(";\n");
                registerMap.put("v" + i, "Z");
                if (parameterRegisterNumber >= 0) {
                    variablesText.append("boolean p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "Z");
                }
                break;
            case RegisterType.BYTE:
            case RegisterType.POS_BYTE:
                variablesText.append("byte v").append(i).append(";\n");
                registerMap.put("v" + i, "B");
                if (parameterRegisterNumber >= 0) {
                    variablesText.append("byte p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "B");
                }
                break;
            case RegisterType.SHORT:
            case RegisterType.POS_SHORT:
                variablesText.append("short v").append(i).append(";\n");
                registerMap.put("v" + i, "S");
                if (parameterRegisterNumber >= 0) {
                    variablesText.append("short p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "S");
                }
                break;
            case RegisterType.CHAR:
                variablesText.append("char v").append(i).append(";\n");
                registerMap.put("v" + i, "C");
                if (parameterRegisterNumber >= 0) {
                    variablesText.append("char p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "C");
                }
                break;
            case RegisterType.FLOAT:
                variablesText.append("float v").append(i).append(";\n");
                registerMap.put("v" + i, "F");
                if (parameterRegisterNumber >= 0) {
                    variablesText.append("float p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "F");
                }
                break;
            case RegisterType.LONG_LO:
                variablesText.append("long v").append(i).append(";\n");
                registerMap.put("v" + i, "J");
                if (parameterRegisterNumber >= 0) {
                    variablesText.append("long p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "J");
                }
                break;
            case RegisterType.DOUBLE_LO:
                variablesText.append("double v").append(i).append(";\n");
                registerMap.put("v" + i, "D");
                if (parameterRegisterNumber >= 0) {
                    variablesText.append("double p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "D");
                }
                break;
            case RegisterType.UNINIT_REF:
            case RegisterType.UNINIT_THIS:
            case RegisterType.REFERENCE:
                String smaliType = registerType.type.getType();
                String javaType = NameUtils.smaliToJavaType(smaliType);
                variablesText.append(javaType).append(" v").append(i).append(";\n");
                registerMap.put("v" + i, smaliType);
                if (parameterRegisterNumber >= 0) {
                    variablesText.append(javaType).append(" p").append(parameterRegisterNumber).append(";\n");
                    registerMap.put("p" + parameterRegisterNumber, "Ljava/lang/Object;");
                }
                break;
        }
    }
    final TextWithImportsImpl textWithImports = new TextWithImportsImpl(CodeFragmentKind.CODE_BLOCK, variablesText.toString(), "", getFileType());
    final JavaCodeFragment codeFragment = super.createCodeFragment(textWithImports, originalContext, project);
    codeFragment.accept(new JavaRecursiveElementVisitor() {

        @Override
        public void visitLocalVariable(final PsiLocalVariable variable) {
            final String name = variable.getName();
            if (name != null && registerMap.containsKey(name)) {
                int registerNumber = Integer.parseInt(name.substring(1));
                if (name.charAt(0) == 'p') {
                    registerNumber += ApplicationManager.getApplication().runReadAction(new Computable<Integer>() {

                        @Override
                        public Integer compute() {
                            return containingMethod.getRegisterCount() - containingMethod.getParameterRegisterCount();
                        }
                    });
                }
                LazyValue lazyValue = LazyValue.create(containingMethod, project, registerNumber, registerMap.get(name));
                variable.putUserData(CodeFragmentFactoryContextWrapper.LABEL_VARIABLE_VALUE_KEY, lazyValue);
                lazyValues.add(lazyValue);
            }
        }
    });
    int offset = variablesText.length() - 1;
    final PsiElement newContext = codeFragment.findElementAt(offset);
    if (newContext != null) {
        newContext.putUserData(SMALI_LAZY_VALUES_KEY, lazyValues);
        return newContext;
    }
    return originalContext;
}
Also used : LazyValue(org.jf.smalidea.debugging.value.LazyValue) SmaliInstruction(org.jf.smalidea.psi.impl.SmaliInstruction) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) AnalyzedInstruction(org.jf.dexlib2.analysis.AnalyzedInstruction) SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod) RegisterType(org.jf.dexlib2.analysis.RegisterType) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) JavaRecursiveElementVisitor(com.intellij.psi.JavaRecursiveElementVisitor) PsiElement(com.intellij.psi.PsiElement)

Example 3 with JavaRecursiveElementVisitor

use of com.intellij.psi.JavaRecursiveElementVisitor in project intellij-community by JetBrains.

the class CodeFragmentFactoryContextWrapper method wrapContext.

private PsiElement wrapContext(Project project, final PsiElement originalContext) {
    if (project.isDefault())
        return originalContext;
    //TODO [egor] : does not work for anything other than java anyway, see IDEA-132677
    if (!(myDelegate instanceof DefaultCodeFragmentFactory)) {
        return originalContext;
    }
    PsiElement context = originalContext;
    XDebugSession session = XDebuggerManager.getInstance(project).getCurrentSession();
    if (session != null) {
        XValueMarkers<?, ?> markers = ((XDebugSessionImpl) session).getValueMarkers();
        Map<?, ValueMarkup> markupMap = markers != null ? markers.getAllMarkers() : null;
        //final Map<ObjectReference, ValueMarkup> markupMap = ValueDescriptorImpl.getMarkupMap(process);
        if (!ContainerUtil.isEmpty(markupMap)) {
            final Pair<String, Map<String, ObjectReference>> markupVariables = createMarkupVariablesText(markupMap);
            int offset = markupVariables.getFirst().length() - 1;
            final TextWithImportsImpl textWithImports = new TextWithImportsImpl(CodeFragmentKind.CODE_BLOCK, markupVariables.getFirst(), "", myDelegate.getFileType());
            final JavaCodeFragment codeFragment = myDelegate.createCodeFragment(textWithImports, context, project);
            codeFragment.accept(new JavaRecursiveElementVisitor() {

                public void visitLocalVariable(PsiLocalVariable variable) {
                    final String name = variable.getName();
                    variable.putUserData(LABEL_VARIABLE_VALUE_KEY, markupVariables.getSecond().get(name));
                }
            });
            final PsiElement newContext = codeFragment.findElementAt(offset);
            if (newContext != null) {
                context = newContext;
            }
        }
    }
    return context;
}
Also used : XDebugSession(com.intellij.xdebugger.XDebugSession) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) ValueMarkup(com.intellij.xdebugger.impl.ui.tree.ValueMarkup) HashMap(java.util.HashMap) Map(java.util.Map) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) JavaRecursiveElementVisitor(com.intellij.psi.JavaRecursiveElementVisitor) PsiElement(com.intellij.psi.PsiElement) XDebugSessionImpl(com.intellij.xdebugger.impl.XDebugSessionImpl)

Example 4 with JavaRecursiveElementVisitor

use of com.intellij.psi.JavaRecursiveElementVisitor in project Main by SpartanRefactoring.

the class Toolbox method executeSingleTipper.

/**
     * @param e Psi tree
     * @param tipperName The name of the tipper to execure on e.
     * @return True if the tipper changed anything, false otherwise.
     * */
public boolean executeSingleTipper(PsiElement e, String tipperName) {
    Tipper tipper = getTipperByName(tipperName);
    if (tipper == null) {
        System.out.println("\nNull tipper!\n");
        return false;
    }
    if (e == null) {
        System.out.println("\nNull element!\n");
        return false;
    }
    Wrapper<PsiElement> toReplace = new Wrapper<>(null);
    Wrapper<Boolean> modified = new Wrapper<>(false);
    e.accept(new JavaRecursiveElementVisitor() {

        @Override
        public void visitElement(PsiElement el) {
            super.visitElement(el);
            if (modified.get()) {
                return;
            }
            if (tipper.canTip(el)) {
                toReplace.set(el);
                modified.set(true);
            }
        }
    });
    if (!modified.get()) {
        return false;
    }
    tipper.tip(toReplace.get()).go(new PsiRewrite().psiFile(e.getContainingFile()).project(e.getProject()));
    return true;
}
Also used : Tipper(il.org.spartan.Leonidas.plugin.tipping.Tipper) Wrapper(il.org.spartan.Leonidas.auxilary_layer.Wrapper) PsiRewrite(il.org.spartan.Leonidas.auxilary_layer.PsiRewrite) JavaRecursiveElementVisitor(com.intellij.psi.JavaRecursiveElementVisitor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

JavaRecursiveElementVisitor (com.intellij.psi.JavaRecursiveElementVisitor)4 PsiElement (com.intellij.psi.PsiElement)3 JavaCodeFragment (com.intellij.psi.JavaCodeFragment)2 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 PsiFile (com.intellij.psi.PsiFile)1 XDebugSession (com.intellij.xdebugger.XDebugSession)1 XDebugSessionImpl (com.intellij.xdebugger.impl.XDebugSessionImpl)1 ValueMarkup (com.intellij.xdebugger.impl.ui.tree.ValueMarkup)1 PsiRewrite (il.org.spartan.Leonidas.auxilary_layer.PsiRewrite)1 Wrapper (il.org.spartan.Leonidas.auxilary_layer.Wrapper)1 Tipper (il.org.spartan.Leonidas.plugin.tipping.Tipper)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AnalyzedInstruction (org.jf.dexlib2.analysis.AnalyzedInstruction)1 RegisterType (org.jf.dexlib2.analysis.RegisterType)1 LazyValue (org.jf.smalidea.debugging.value.LazyValue)1 SmaliInstruction (org.jf.smalidea.psi.impl.SmaliInstruction)1 SmaliMethod (org.jf.smalidea.psi.impl.SmaliMethod)1