/** * Finds extension by its containing type and the dot separated multi-part name. */ public static ExtensionInfo findExtensionByName(final ExtensionRegistry registry, final Descriptor containingType, final String name) { final int index = name.lastIndexOf("."); final String nameLastPart = index == -1 ? name : name.substring(index + 1); return registry.findImmutableExtensionByName(containingType.getFullName() + "." + nameLastPart); }
@Override public void exitField(FieldContext ctx) { if (ctx.ID() != null && indexPool.contains(ctx.ID().getText())) { // if we see index but not real field, we have pop ROOT from stack and push index expression instead stack.pop(); stack.push(indexPool.getIndex(ctx.ID().getText())); return; } // otherwise, it is a field. Expression indexExpr = null; if (ctx.index() != null) { indexExpr = dereference(ctx.getStart(), stack.pop(), null); } final ObjectExpression objExpr; final FieldDescriptor fd; Expression scopeExpr = dereference(ctx.getStart(), stack.pop(), null); if (scopeExpr.getType() == OBJECT_REFERENCE) { objExpr = ObjectExpression.class.cast(scopeExpr); } else { throw new SemanticException(ctx.getStart(), "Cannot get field from expression of type " + scopeExpr.getType()); } if (ctx.ext() != null) { ExtensionInfo extInfo = registry.findExtensionByName(ctx.ext().extName); if (extInfo == null) { throw new SemanticException(ctx.getStart(), "No such extension: " + ctx.ext().extName); } fd = extInfo.descriptor; } else { String name = ctx.ID().getText(); if (scopeExpr.getType() != OBJECT_REFERENCE) { throw new SemanticException(ctx.getStart(), "Cannot take field " + name + " from " + scopeExpr.getType()); } Descriptor descr = ObjectExpression.class.cast(scopeExpr).getDescriptor(); fd = descr.findFieldByName(name); if (fd == null) { throw new SemanticException(ctx.getStart(), "Message " + descr.getFullName() + " does not have field " + name); } } if (fd.isRepeated()) { if (indexExpr != null) { stack.push(dereference(ctx.getStart(), new FieldArrayReferenceExpression(objExpr, fd), indexExpr)); } else { stack.push(new FieldArrayReferenceExpression(objExpr, fd)); } } else { stack.push(new FieldReferenceExpression(objExpr, fd)); } }
private ExtensionInfo findExtensionByName(final String containingTypeName, final String name) { if (name.startsWith(".")) { return registry.findExtensionByName(name.substring(1)); } ExtensionInfo info; // try first inner scoped if (!containingTypeName.isEmpty()) { info = registry.findExtensionByName(containingTypeName + '.' + name); if (info != null) { return info; } } // then outer scoped for empty package if (file.getPackage().isEmpty()) { return registry.findExtensionByName(name); } // then outer scoped if starts with package if (name.startsWith(file.getPackage() + '.')) { info = registry.findExtensionByName(name); if (info != null) { return info; } // then if contained in a Message with package name? return registry.findExtensionByName(file.getPackage() + '.' + name); } // then try the package prepending the name info = registry.findExtensionByName(file.getPackage() + '.' + name); if (info != null) { return info; } // then try for outer file extension return registry.findExtensionByName(name); }