Skip to content

Instantly share code, notes, and snippets.

@vladaman
Last active December 18, 2015 14:49
Show Gist options
  • Save vladaman/5799970 to your computer and use it in GitHub Desktop.
Save vladaman/5799970 to your computer and use it in GitHub Desktop.
Parses generated Java classes with Thrift Objects and adds @JsonIgnore to fields which are not supposed to be saved in Database (MongoDB)
import re
import os
regex = 'public void set.*IsSet|public boolean isSet.*|public int get.*Size|public java.util.Iterator'
for filename in os.listdir("."):
if filename.endswith(".java"):
os.rename(filename, filename + ".bak")
f = open(filename + ".bak", 'r')
n = open(filename, 'w+')
line = f.readline()
imported = False
while line:
cols = re.search(regex, line)
if imported == False:
clz = re.search('^public class', line)
if clz != None:
n.write("import com.fasterxml.jackson.annotation.JsonIgnore;\nimport org.mongojack.Id;\n\n")
imported = True
idBlock = re.search("public String _id",line)
if idBlock != None:
n.write(" @Id\n")
if cols != None:
n.write(" @JsonIgnore\n")
n.write(line)
line = f.readline()
n.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment