Search in sources :

Example 1 with JavaCodeFragment

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

the class SmaliCodeFragmentFactory method createCodeFragment.

@Override
public JavaCodeFragment createCodeFragment(TextWithImports item, PsiElement context, Project project) {
    context = wrapContext(project, context);
    JavaCodeFragment fragment = super.createCodeFragment(item, context, project);
    List<LazyValue> lazyValues = context.getUserData(SMALI_LAZY_VALUES_KEY);
    if (lazyValues != null) {
        fragment.putUserData(SMALI_LAZY_VALUES_KEY, lazyValues);
    }
    return fragment;
}
Also used : LazyValue(org.jf.smalidea.debugging.value.LazyValue) JavaCodeFragment(com.intellij.psi.JavaCodeFragment)

Example 2 with JavaCodeFragment

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

the class SmaliCodeFragmentFactoryTest method assertVariableType.

private void assertVariableType(PsiElement context, String variableName, String expectedType) {
    SmaliCodeFragmentFactory codeFragmentFactory = new SmaliCodeFragmentFactory();
    JavaCodeFragment fragment = codeFragmentFactory.createCodeFragment(new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, variableName), context, getProject());
    Editor editor = createEditor(fragment.getVirtualFile());
    editor.getCaretModel().moveToOffset(1);
    PsiElement element = fragment.findElementAt(0);
    Assert.assertTrue(element.getParent() instanceof PsiReferenceExpressionImpl);
    PsiReferenceExpressionImpl reference = (PsiReferenceExpressionImpl) element.getParent();
    Assert.assertEquals(expectedType, reference.getType().getCanonicalText());
}
Also used : SmaliCodeFragmentFactory(org.jf.smalidea.debugging.SmaliCodeFragmentFactory) TextWithImportsImpl(com.intellij.debugger.engine.evaluation.TextWithImportsImpl) Editor(com.intellij.openapi.editor.Editor) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) PsiReferenceExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl) PsiElement(com.intellij.psi.PsiElement)

Example 3 with JavaCodeFragment

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

the class ExtractMethodObject4DebuggerTest method doTest.

private void doTest(String evaluatedText, String expectedCallSite, String expectedClass, boolean codeBlock) throws Exception {
    final String testName = getTestName(false);
    configureByFile("/refactoring/extractMethodObject4Debugger/" + testName + ".java");
    final int offset = getEditor().getCaretModel().getOffset();
    final PsiElement context = getFile().findElementAt(offset);
    final JavaCodeFragmentFactory fragmentFactory = JavaCodeFragmentFactory.getInstance(getProject());
    final JavaCodeFragment fragment = codeBlock ? fragmentFactory.createCodeBlockCodeFragment(evaluatedText, context, false) : fragmentFactory.createExpressionCodeFragment(evaluatedText, context, null, false);
    final ExtractLightMethodObjectHandler.ExtractedData extractedData = ExtractLightMethodObjectHandler.extractLightMethodObject(getProject(), context, fragment, "test");
    assertNotNull(extractedData);
    assertEquals(expectedCallSite, extractedData.getGeneratedCallText());
    final PsiClass innerClass = extractedData.getGeneratedInnerClass();
    assertEquals(expectedClass, innerClass.getText());
}
Also used : PsiClass(com.intellij.psi.PsiClass) ExtractLightMethodObjectHandler(com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) PsiElement(com.intellij.psi.PsiElement) JavaCodeFragmentFactory(com.intellij.psi.JavaCodeFragmentFactory)

Example 4 with JavaCodeFragment

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

the class UserExpressionDescriptorImpl method getEvaluationCode.

protected PsiCodeFragment getEvaluationCode(final StackFrameContext context) throws EvaluateException {
    Pair<PsiElement, PsiType> psiClassAndType = DebuggerUtilsImpl.getPsiClassAndType(myTypeName, myProject);
    if (psiClassAndType.first == null) {
        throw EvaluateExceptionUtil.createEvaluateException(DebuggerBundle.message("evaluation.error.invalid.type.name", myTypeName));
    }
    PsiCodeFragment fragment = createCodeFragment(psiClassAndType.first);
    if (fragment instanceof JavaCodeFragment) {
        ((JavaCodeFragment) fragment).setThisType(psiClassAndType.second);
    }
    return fragment;
}
Also used : PsiCodeFragment(com.intellij.psi.PsiCodeFragment) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 5 with JavaCodeFragment

use of com.intellij.psi.JavaCodeFragment 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)

Aggregations

JavaCodeFragment (com.intellij.psi.JavaCodeFragment)9 PsiElement (com.intellij.psi.PsiElement)5 LazyValue (org.jf.smalidea.debugging.value.LazyValue)3 TextWithImportsImpl (com.intellij.debugger.engine.evaluation.TextWithImportsImpl)2 Editor (com.intellij.openapi.editor.Editor)2 JavaRecursiveElementVisitor (com.intellij.psi.JavaRecursiveElementVisitor)2 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)2 SmaliCodeFragmentFactory (org.jf.smalidea.debugging.SmaliCodeFragmentFactory)2 CodeCompletionHandlerBase (com.intellij.codeInsight.completion.CodeCompletionHandlerBase)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 ASTNode (com.intellij.lang.ASTNode)1 JavaCodeFragmentFactory (com.intellij.psi.JavaCodeFragmentFactory)1 PsiClass (com.intellij.psi.PsiClass)1 PsiCodeFragment (com.intellij.psi.PsiCodeFragment)1 PsiType (com.intellij.psi.PsiType)1 CodeFormatterFacade (com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade)1 PsiReferenceExpressionImpl (com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl)1 ExtractLightMethodObjectHandler (com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler)1 XDebugSession (com.intellij.xdebugger.XDebugSession)1 XDebugSessionImpl (com.intellij.xdebugger.impl.XDebugSessionImpl)1