Skip to content

Instantly share code, notes, and snippets.

@tuanchauict
Last active March 24, 2017 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuanchauict/3ff1930a60c30b3e06be5c65e40a2756 to your computer and use it in GitHub Desktop.
Save tuanchauict/3ff1930a60c30b3e06be5c65e40a2756 to your computer and use it in GitHub Desktop.
JConst: Automatically make constants

What is this?

Inspirated by Golang's const, I made this script for automation set value for constants white writing Java code.

Just:

//#constant(
int A;
int B;
//)

then save and we have:

public static final int A = 0;
public static final int B = 1;

One more example:

//#constant(
private int A = 1;
protected int B = 10;
int C = 100;
//)

The result will be:

//#constant(
private static final int A = 1;
protected static final int B = 2;
public static final int C = 3;
//)

Usage

  1. Install File Watchers plugin on IDEA or Android Studio.
  2. On File watcher preferences (Open Preferences -> Tools -> File Watchers)
  3. Add new watcher with custom type
  4. Setup watcher
  5. File type: Java Source
  6. Scope: All Places
  7. Program: select jwatcher.sh file (remember to add execute permission)
  8. Arguments: $FilePath$
  9. Uncheck: Immediate file synchronization
  10. Show console: Never

Limitation

No work with this: int A; int B;

Future work

Make this become Intelij's Plugin

import sys
import re
def convert_constants(s):
arr = s.split('\n')
count = 0
for i in range(1, len(arr) - 1):
if i == 1:
value = re.findall(r'(\d+)(?:[lL]?\s?;\s*.*)$', arr[i])
if value:
count = int(value[0])
arr[i], success = modify_constant(arr[i], count)
if success:
count += 1
result = '\n'.join(arr)
return result
def modify_constant(constant, count):
if re.match(r'^\s*(//.*)?$', constant):
return constant, False
indent = re.findall(r'^\s+', constant)[0]
comment = re.sub(r'^.*?;', '', constant)
constant = constant.replace(comment, '')
constant = constant.lstrip()
openess = re.findall(r'private|protected|public', constant)
constant = re.sub(r'(\bprivate\s+|\bprotected\s+|\bpublic\s+|\bfinal\s+|\bstatic\s+)+', '', constant)
extension = ''
if 'private' in openess:
extension += 'private '
elif 'protected' in openess:
extension += 'protected '
else:
extension += 'public '
extension += 'static final '
if re.match(r'.+?\d+\s*;', constant):
constant = re.sub(r's*\d+\s*;', '%d;' % count, constant)
else:
constant = re.sub(r';', ' = %d;' % count, constant)
return indent + extension + constant.strip() + comment, True
def main(args):
with open(args[0]) as f:
text = f.read()
constants = re.findall(r'//#constant\((?:.|\n)+?//\)', text)
new_text = text
for c in constants:
convert_constants(c)
new_text = new_text.replace(c, convert_constants(c))
if new_text != text:
with open(args[0], 'w') as f:
f.write(new_text)
if __name__ == "__main__":
main(sys.argv[1:])
#!/bin/bash
python3.5 /path/to/jwatcher.py $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment