private void putCapturedInLocal( @NotNull Type type, @Nullable StackValue stackValue, @Nullable ValueParameterDescriptor valueParameterDescriptor, int capturedParamIndex) { if (!asFunctionInline && Type.VOID_TYPE != type) { // TODO remap only inlinable closure => otherwise we could get a lot of problem boolean couldBeRemapped = !shouldPutValue(type, stackValue, valueParameterDescriptor); StackValue remappedIndex = couldBeRemapped ? stackValue : null; ParameterInfo info; if (capturedParamIndex >= 0) { CapturedParamDesc capturedParamInfoInLambda = activeLambda.getCapturedVars().get(capturedParamIndex); info = invocationParamBuilder.addCapturedParam( capturedParamInfoInLambda, capturedParamInfoInLambda.getFieldName()); info.setRemapValue(remappedIndex); } else { info = invocationParamBuilder.addNextParameter(type, false, remappedIndex); } putParameterOnStack(info); } }
public boolean matchesParams(String[] params, String[] dimensions, boolean varargs) { if (mParamStrings == null) { if (mParameters.size() != params.length) { return false; } int i = 0; for (ParameterInfo mine : mParameters) { if (!mine.matchesDimension(dimensions[i], varargs)) { return false; } TypeInfo myType = mine.type(); String qualifiedName = myType.qualifiedTypeName(); String realType = myType.isPrimitive() ? "" : myType.asClassInfo().qualifiedName(); String s = params[i]; int slen = s.length(); int qnlen = qualifiedName.length(); // Check for a matching generic name or best known type if (!matchesType(qualifiedName, s) && !matchesType(realType, s)) { return false; } i++; } } return true; }
private static GrMethodCallExpression createMethodCall(ExtractInfoHelper helper) { StringBuilder buffer = new StringBuilder(); buffer.append(helper.getName()).append("("); int number = 0; for (ParameterInfo info : helper.getParameterInfos()) { if (info.passAsParameter()) number++; } int i = 0; String[] argumentNames = helper.getArgumentNames(); for (String argName : argumentNames) { if (argName.length() > 0) { buffer.append(argName); if (i < number - 1) { buffer.append(","); } i++; } } buffer.append(")"); String callText = buffer.toString(); GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject()); GrExpression expr = factory.createExpressionFromText(callText); LOG.assertTrue(expr instanceof GrMethodCallExpression, callText); return ((GrMethodCallExpression) expr); }
public void leaveTemps() { FrameMap frameMap = codegen.getFrameMap(); List<ParameterInfo> infos = invocationParamBuilder.listAllParams(); for (ListIterator<? extends ParameterInfo> iterator = infos.listIterator(infos.size()); iterator.hasPrevious(); ) { ParameterInfo param = iterator.previous(); if (!param.isSkippedOrRemapped()) { frameMap.leaveTemp(param.type); } } }
public void rememberClosure(JetExpression expression, Type type) { JetExpression lambda = JetPsiUtil.deparenthesize(expression); assert isInlinableParameterExpression(lambda) : "Couldn't find inline expression in " + expression.getText(); LambdaInfo info = new LambdaInfo(lambda, typeMapper); ParameterInfo closureInfo = invocationParamBuilder.addNextParameter(type, true, null); closureInfo.setLambda(info); expressionMap.put(closureInfo.getIndex(), info); }
/** Returns a printable version of the parameters of this method's signature. */ public String prettyParameters() { StringBuilder params = new StringBuilder("("); for (ParameterInfo pInfo : mParameters) { if (params.length() > 1) { params.append(","); } params.append(pInfo.type().simpleTypeName()); } params.append(")"); return params.toString(); }
@Override public boolean setupDefaultValues( ChangeInfo changeInfo, Ref<UsageInfo[]> refUsages, Project project) { if (!(changeInfo instanceof JavaChangeInfo)) return true; for (UsageInfo usageInfo : refUsages.get()) { if (usageInfo instanceof MethodCallUsageInfo) { MethodCallUsageInfo methodCallUsageInfo = (MethodCallUsageInfo) usageInfo; if (methodCallUsageInfo.isToChangeArguments()) { final PsiElement element = methodCallUsageInfo.getElement(); if (element == null) continue; final PsiMethod caller = RefactoringUtil.getEnclosingMethod(element); final boolean needDefaultValue = needDefaultValue(changeInfo, caller); if (needDefaultValue && (caller == null || !MethodSignatureUtil.isSuperMethod( methodCallUsageInfo.getReferencedMethod(), caller))) { final ParameterInfo[] parameters = changeInfo.getNewParameters(); for (ParameterInfo parameter : parameters) { final String defaultValue = parameter.getDefaultValue(); if (defaultValue == null && parameter.getOldIndex() == -1) { ((ParameterInfoImpl) parameter).setDefaultValue(""); if (!ApplicationManager.getApplication().isUnitTestMode()) { final PsiType type = ((ParameterInfoImpl) parameter) .getTypeWrapper() .getType(element, element.getManager()); final DefaultValueChooser chooser = new DefaultValueChooser( project, parameter.getName(), PsiTypesUtil.getDefaultValueOfType(type)); chooser.show(); if (chooser.isOK()) { if (chooser.feelLucky()) { parameter.setUseAnySingleVariable(true); } else { ((ParameterInfoImpl) parameter).setDefaultValue(chooser.getDefaultValue()); } } else { return false; } } } } } } } } return true; }
@Override public String signature() { if (mSignature == null) { StringBuilder params = new StringBuilder("("); for (ParameterInfo pInfo : mParameters) { if (params.length() > 1) { params.append(", "); } params.append(pInfo.type().fullName()); } params.append(")"); mSignature = params.toString(); } return mSignature; }
private void putParameterOnStack(ParameterInfo... infos) { int[] index = new int[infos.length]; for (int i = 0; i < infos.length; i++) { ParameterInfo info = infos[i]; if (!info.isSkippedOrRemapped()) { index[i] = codegen.getFrameMap().enterTemp(info.getType()); } else { index[i] = -1; } } for (int i = infos.length - 1; i >= 0; i--) { ParameterInfo info = infos[i]; if (!info.isSkippedOrRemapped()) { Type type = info.type; StackValue.local(index[i], type).store(StackValue.onStack(type), codegen.v); } } }
public void makeHDF(Data data, String base, Map<String, TypeInfo> typeMapping) { data.setValue(base + ".kind", kind()); data.setValue(base + ".name", name()); data.setValue(base + ".href", htmlPage()); data.setValue(base + ".anchor", anchor()); if (mReturnType != null) { returnType() .getTypeWithArguments(typeMapping) .makeHDF(data, base + ".returnType", false, typeVariables()); data.setValue(base + ".abstract", mIsAbstract ? "abstract" : ""); } data.setValue(base + ".synchronized", mIsSynchronized ? "synchronized" : ""); data.setValue(base + ".final", isFinal() ? "final" : ""); data.setValue(base + ".static", isStatic() ? "static" : ""); TagInfo.makeHDF(data, base + ".shortDescr", firstSentenceTags()); TagInfo.makeHDF(data, base + ".descr", inlineTags()); TagInfo.makeHDF(data, base + ".deprecated", deprecatedTags()); TagInfo.makeHDF(data, base + ".seeAlso", seeTags()); data.setValue(base + ".since", getSince()); if (isDeprecated()) { data.setValue(base + ".deprecatedsince", getDeprecatedSince()); } ParamTagInfo.makeHDF(data, base + ".paramTags", paramTags()); AttrTagInfo.makeReferenceHDF(data, base + ".attrRefs", comment().attrTags()); ThrowsTagInfo.makeHDF(data, base + ".throws", throwsTags()); ParameterInfo.makeHDF( data, base + ".params", mParameters.toArray(new ParameterInfo[mParameters.size()]), isVarArgs(), typeVariables(), typeMapping); if (isProtected()) { data.setValue(base + ".scope", "protected"); } else if (isPublic()) { data.setValue(base + ".scope", "public"); } TagInfo.makeHDF(data, base + ".returns", returnTags()); if (mTypeParameters != null) { TypeInfo.makeHDF(data, base + ".generic.typeArguments", mTypeParameters, false); } AnnotationInstanceInfo.makeLinkListHDF( data, base + ".showAnnotations", showAnnotations().toArray(new AnnotationInstanceInfo[showAnnotations().size()])); setFederatedReferences(data, base); }
public static String[] getParameterString(ExtractInfoHelper helper, boolean useCanonicalText) { int i = 0; ParameterInfo[] infos = helper.getParameterInfos(); int number = 0; for (ParameterInfo info : infos) { if (info.passAsParameter()) number++; } ArrayList<String> params = new ArrayList<String>(); for (ParameterInfo info : infos) { if (info.passAsParameter()) { PsiType paramType = info.getType(); final PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(paramType); if (unboxed != null) paramType = unboxed; String paramTypeText; if (paramType == null || paramType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) { paramTypeText = ""; } else { paramTypeText = (useCanonicalText ? paramType.getCanonicalText() : paramType.getPresentableText()) + " "; } params.add(paramTypeText + info.getName() + (i < number - 1 ? ", " : "")); i++; } } return ArrayUtil.toStringArray(params); }
/** Returns a name consistent with the {@link com.google.doclava.MethodInfo#getHashableName()}. */ public String getHashableName() { StringBuilder result = new StringBuilder(); result.append(name()); if (mParameters == null) { return result.toString(); } int i = 0; for (ParameterInfo param : mParameters) { result.append(":"); if (i == (mParameters.size() - 1) && isVarArgs()) { // TODO: note that this does not attempt to handle hypothetical // vararg methods whose last parameter is a list of arrays, e.g. // "Object[]...". result.append(param.type().fullNameNoDimension(typeVariables())).append("..."); } else { result.append(param.type().fullName(typeVariables())); } i++; } return result.toString(); }
/** * Clone this MethodInfo as if it belonged to the specified ClassInfo and apply the * typeArgumentMapping to the parameters and return types. */ public MethodInfo cloneForClass( ClassInfo newContainingClass, Map<String, TypeInfo> typeArgumentMapping) { TypeInfo returnType = mReturnType.getTypeWithArguments(typeArgumentMapping); ArrayList<ParameterInfo> parameters = new ArrayList<ParameterInfo>(); for (ParameterInfo pi : mParameters) { parameters.add(pi.cloneWithTypeArguments(typeArgumentMapping)); } MethodInfo result = new MethodInfo( getRawCommentText(), mTypeParameters, name(), signature(), newContainingClass, realContainingClass(), isPublic(), isProtected(), isPackagePrivate(), isPrivate(), isFinal(), isStatic(), isSynthetic(), mIsAbstract, mIsSynchronized, mIsNative, mIsAnnotationElement, kind(), mFlatSignature, mOverriddenMethod, returnType, mParameters, mThrownExceptions, position(), annotations()); result.init(mDefaultAnnotationElementValue); return result; }
private List<CapturedParamInfo> extractParametersMappingAndPatchConstructor( @NotNull MethodNode constructor, @NotNull ParametersBuilder capturedParamBuilder, @NotNull ParametersBuilder constructorParamBuilder, @NotNull final AnonymousObjectGeneration anonymousObjectGen, @NotNull FieldRemapper parentFieldRemapper) { CapturedParamOwner owner = new CapturedParamOwner() { @Override public Type getType() { return Type.getObjectType(anonymousObjectGen.getOwnerInternalName()); } }; Set<LambdaInfo> capturedLambdas = new LinkedHashSet<LambdaInfo>(); // captured var of inlined parameter List<CapturedParamInfo> constructorAdditionalFakeParams = new ArrayList<CapturedParamInfo>(); Map<Integer, LambdaInfo> indexToLambda = anonymousObjectGen.getLambdasToInline(); Set<Integer> capturedParams = new HashSet<Integer>(); // load captured parameters and patch instruction list (NB: there is also could be object // fields) AbstractInsnNode cur = constructor.instructions.getFirst(); while (cur != null) { if (cur instanceof FieldInsnNode) { FieldInsnNode fieldNode = (FieldInsnNode) cur; String fieldName = fieldNode.name; if (fieldNode.getOpcode() == Opcodes.PUTFIELD && InlineCodegenUtil.isCapturedFieldName(fieldName)) { boolean isPrevVarNode = fieldNode.getPrevious() instanceof VarInsnNode; boolean isPrevPrevVarNode = isPrevVarNode && fieldNode.getPrevious().getPrevious() instanceof VarInsnNode; if (isPrevPrevVarNode) { VarInsnNode node = (VarInsnNode) fieldNode.getPrevious().getPrevious(); if (node.var == 0) { VarInsnNode previous = (VarInsnNode) fieldNode.getPrevious(); int varIndex = previous.var; LambdaInfo lambdaInfo = indexToLambda.get(varIndex); String newFieldName = isThis0(fieldName) && shouldRenameThis0(parentFieldRemapper, indexToLambda.values()) ? getNewFieldName(fieldName, true) : fieldName; CapturedParamInfo info = capturedParamBuilder.addCapturedParam( owner, fieldName, newFieldName, Type.getType(fieldNode.desc), lambdaInfo != null, null); if (lambdaInfo != null) { info.setLambda(lambdaInfo); capturedLambdas.add(lambdaInfo); } constructorAdditionalFakeParams.add(info); capturedParams.add(varIndex); constructor.instructions.remove(previous.getPrevious()); constructor.instructions.remove(previous); AbstractInsnNode temp = cur; cur = cur.getNext(); constructor.instructions.remove(temp); continue; } } } } cur = cur.getNext(); } constructorParamBuilder.addThis(oldObjectType, false); String constructorDesc = anonymousObjectGen.getConstructorDesc(); if (constructorDesc == null) { // in case of anonymous object with empty closure constructorDesc = Type.getMethodDescriptor(Type.VOID_TYPE); } Type[] types = Type.getArgumentTypes(constructorDesc); for (Type type : types) { LambdaInfo info = indexToLambda.get(constructorParamBuilder.getNextValueParameterIndex()); ParameterInfo parameterInfo = constructorParamBuilder.addNextParameter(type, info != null, null); parameterInfo.setLambda(info); if (capturedParams.contains(parameterInfo.getIndex())) { parameterInfo.setCaptured(true); } else { // otherwise it's super constructor parameter } } // For all inlined lambdas add their captured parameters // TODO: some of such parameters could be skipped - we should perform additional analysis Map<String, LambdaInfo> capturedLambdasToInline = new HashMap<String, LambdaInfo>(); // captured var of inlined parameter List<CapturedParamDesc> allRecapturedParameters = new ArrayList<CapturedParamDesc>(); boolean addCapturedNotAddOuter = parentFieldRemapper.isRoot() || (parentFieldRemapper instanceof InlinedLambdaRemapper && parentFieldRemapper.getParent().isRoot()); Map<String, CapturedParamInfo> alreadyAdded = new HashMap<String, CapturedParamInfo>(); for (LambdaInfo info : capturedLambdas) { if (addCapturedNotAddOuter) { for (CapturedParamDesc desc : info.getCapturedVars()) { String key = desc.getFieldName() + "$$$" + desc.getType().getClassName(); CapturedParamInfo alreadyAddedParam = alreadyAdded.get(key); CapturedParamInfo recapturedParamInfo = capturedParamBuilder.addCapturedParam( desc, alreadyAddedParam != null ? alreadyAddedParam.getNewFieldName() : getNewFieldName(desc.getFieldName(), false)); StackValue composed = StackValue.field( desc.getType(), oldObjectType, /*TODO owner type*/ recapturedParamInfo.getNewFieldName(), false, StackValue.LOCAL_0); recapturedParamInfo.setRemapValue(composed); allRecapturedParameters.add(desc); constructorParamBuilder .addCapturedParam(recapturedParamInfo, recapturedParamInfo.getNewFieldName()) .setRemapValue(composed); if (alreadyAddedParam != null) { recapturedParamInfo.setSkipInConstructor(true); } if (isThis0(desc.getFieldName())) { alreadyAdded.put(key, recapturedParamInfo); } } } capturedLambdasToInline.put(info.getLambdaClassType().getInternalName(), info); } if (parentFieldRemapper instanceof InlinedLambdaRemapper && !capturedLambdas.isEmpty() && !addCapturedNotAddOuter) { // lambda with non InlinedLambdaRemapper already have outer FieldRemapper parent = parentFieldRemapper.getParent(); assert parent instanceof RegeneratedLambdaFieldRemapper; final Type ownerType = Type.getObjectType(parent.getLambdaInternalName()); CapturedParamDesc desc = new CapturedParamDesc( new CapturedParamOwner() { @Override public Type getType() { return ownerType; } }, InlineCodegenUtil.THIS, ownerType); CapturedParamInfo recapturedParamInfo = capturedParamBuilder.addCapturedParam( desc, InlineCodegenUtil.THIS$0 /*outer lambda/object*/); StackValue composed = StackValue.LOCAL_0; recapturedParamInfo.setRemapValue(composed); allRecapturedParameters.add(desc); constructorParamBuilder .addCapturedParam(recapturedParamInfo, recapturedParamInfo.getNewFieldName()) .setRemapValue(composed); } anonymousObjectGen.setAllRecapturedParameters(allRecapturedParameters); anonymousObjectGen.setCapturedLambdasToInline(capturedLambdasToInline); return constructorAdditionalFakeParams; }
public static void generateBody( ExtractInfoHelper helper, boolean isVoid, StringBuilder buffer, boolean forceReturn) { VariableInfo[] outputInfos = helper.getOutputVariableInfos(); ParameterInfo[] infos = helper.getParameterInfos(); Set<String> declaredVars = new HashSet<String>(); for (ParameterInfo info : infos) { declaredVars.add(info.getName()); } for (VariableInfo info : mustAddVariableDeclaration(helper.getStatements(), outputInfos)) { declaredVars.add(info.getName()); } List<VariableInfo> genDecl = new ArrayList<VariableInfo>(); final Collection<GrVariable> outside = collectUsedLocalVarsOrParamsDeclaredOutside(helper.getStatements()); for (final GrVariable variable : outside) { if (!declaredVars.contains(variable.getName())) { genDecl.add( new VariableInfo() { @NotNull @Override public String getName() { return variable.getName(); } @Override public PsiType getType() { return variable.getDeclaredType(); } }); } } final List<GrStatement> statements = generateVarDeclarations(genDecl, helper.getProject(), null); for (GrStatement statement : statements) { buffer.append(statement.getText()).append('\n'); } if (!isSingleExpression(helper.getStatements()) || statements.size() > 0) { for (PsiElement element : helper.getInnerElements()) { buffer.append(element.getText()); } // append return statement if (!isVoid && outputInfos.length > 0) { buffer.append('\n'); if (forceReturn) { buffer.append("return "); } if (outputInfos.length > 1) buffer.append('['); for (VariableInfo info : outputInfos) { buffer.append(info.getName()).append(", "); } buffer.delete(buffer.length() - 2, buffer.length()); if (outputInfos.length > 1) buffer.append(']'); } } else { GrExpression expr = (GrExpression) PsiUtil.skipParentheses(helper.getStatements()[0], false); boolean addReturn = !isVoid && forceReturn; if (addReturn) { buffer.append("return "); expr = ApplicationStatementUtil.convertToMethodCallExpression(expr); buffer.append(expr.getText()); } else { buffer.append(expr != null ? expr.getText() : ""); } } }
public ParamTagInfo[] paramTags() { if (mParamTags == null) { final int N = mParameters.size(); String[] names = new String[N]; String[] comments = new String[N]; SourcePositionInfo[] positions = new SourcePositionInfo[N]; // get the right names so we can handle our names being different from // our parent's names. int i = 0; for (ParameterInfo param : mParameters) { names[i] = param.name(); comments[i] = ""; positions[i] = param.position(); i++; } // gather our comments, and complain about misnamed @param tags for (ParamTagInfo tag : comment().paramTags()) { int index = indexOfParam(tag.parameterName(), names); if (index >= 0) { comments[index] = tag.parameterComment(); positions[index] = tag.position(); } else { Errors.error( Errors.UNKNOWN_PARAM_TAG_NAME, tag.position(), "@param tag with name that doesn't match the parameter list: '" + tag.parameterName() + "'"); } } // get our parent's tags to fill in the blanks MethodInfo overridden = this.findOverriddenMethod(name(), signature()); if (overridden != null) { ParamTagInfo[] maternal = overridden.paramTags(); for (i = 0; i < N; i++) { if (comments[i].equals("")) { comments[i] = maternal[i].parameterComment(); positions[i] = maternal[i].position(); } } } // construct the results, and cache them for next time mParamTags = new ParamTagInfo[N]; for (i = 0; i < N; i++) { mParamTags[i] = new ParamTagInfo( "@param", "@param", names[i] + " " + comments[i], parent(), positions[i]); // while we're here, if we find any parameters that are still undocumented at this // point, complain. (this warning is off by default, because it's really, really // common; but, it's good to be able to enforce it) if (comments[i].equals("")) { Errors.error( Errors.UNDOCUMENTED_PARAMETER, positions[i], "Undocumented parameter '" + names[i] + "' on method '" + name() + "'"); } } } return mParamTags; }
private void generateConstructorAndFields( @NotNull ClassBuilder classBuilder, @NotNull ParametersBuilder allCapturedBuilder, @NotNull ParametersBuilder constructorInlineBuilder, @NotNull AnonymousObjectGeneration anonymousObjectGen, @NotNull FieldRemapper parentRemapper, @NotNull List<CapturedParamInfo> constructorAdditionalFakeParams) { List<Type> descTypes = new ArrayList<Type>(); Parameters constructorParams = constructorInlineBuilder.buildParameters(); int[] capturedIndexes = new int[constructorParams.getReal().size() + constructorParams.getCaptured().size()]; int index = 0; int size = 0; // complex processing cause it could have super constructor call params for (ParameterInfo info : constructorParams) { if (!info.isSkipped()) { // not inlined if (info.isCaptured() || info instanceof CapturedParamInfo) { capturedIndexes[index] = size; index++; } if (size != 0) { // skip this descTypes.add(info.getType()); } size += info.getType().getSize(); } } String constructorDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, descTypes.toArray(new Type[descTypes.size()])); // TODO for inline method make public class anonymousObjectGen.setNewConstructorDescriptor(constructorDescriptor); MethodVisitor constructorVisitor = classBuilder.newMethod( NO_ORIGIN, AsmUtil.NO_FLAG_PACKAGE_PRIVATE, "<init>", constructorDescriptor, null, ArrayUtil.EMPTY_STRING_ARRAY); // initialize captured fields List<NewJavaField> newFieldsWithSkipped = TransformationUtilsKt.getNewFieldsToGenerate(allCapturedBuilder.listCaptured()); List<FieldInfo> fieldInfoWithSkipped = TransformationUtilsKt.transformToFieldInfo(newLambdaType, newFieldsWithSkipped); int paramIndex = 0; InstructionAdapter capturedFieldInitializer = new InstructionAdapter(constructorVisitor); for (int i = 0; i < fieldInfoWithSkipped.size(); i++) { FieldInfo fieldInfo = fieldInfoWithSkipped.get(i); if (!newFieldsWithSkipped.get(i).getSkip()) { AsmUtil.genAssignInstanceFieldFromParam( fieldInfo, capturedIndexes[paramIndex], capturedFieldInitializer); } paramIndex++; } // then transform constructor // HACK: in inlinining into constructor we access original captured fields with field access not // local var // but this fields added to general params (this assumes local var access) not captured one, // so we need to add them to captured params for (CapturedParamInfo info : constructorAdditionalFakeParams) { CapturedParamInfo fake = constructorInlineBuilder.addCapturedParamCopy(info); if (fake.getLambda() != null) { // set remap value to skip this fake (captured with lambda already skipped) StackValue composed = StackValue.field( fake.getType(), oldObjectType, fake.getNewFieldName(), false, StackValue.LOCAL_0); fake.setRemapValue(composed); } } inlineMethodAndUpdateGlobalResult( anonymousObjectGen, parentRemapper, capturedFieldInitializer, constructor, constructorInlineBuilder, true); constructorVisitor.visitEnd(); AsmUtil.genClosureFields( TransformationUtilsKt.toNameTypePair( TransformationUtilsKt.filterSkipped(newFieldsWithSkipped)), classBuilder); }