All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sdmlib.models.classes.templates.AddTemplate Maven / Gradle / Ivy

Go to download

SDMLib is a light weight modeling library. SDMLib intentionally comes without any tool or editor.

There is a newer version: 2.3.2341
Show newest version
package org.sdmlib.models.classes.templates;

import org.sdmlib.codegen.Parser;
import org.sdmlib.models.classes.ClassModel;

import de.uniks.networkparser.list.SimpleList;

public class AddTemplate extends TemplateItem {

	private SimpleList search = new SimpleList();
	private String lastFound;
	
	
	public AddTemplate(int methodStart, String... search) {
		this.withPos(methodStart);
		this.withOffset(methodStart+1);
		this.search.with(search);
	}
	
	public AddTemplate withLast(String last) {
		this.lastFound = last;
		return this;
	}
	
	public int getLast(Parser parser, String item, int start, int end) {
		int result = parser.search(item, start);
		if(result > end) {
			result = -1; 
		}
		while (result >= 0 && result < end) {
			int temp = parser.search(item, result + 4);
			if (temp >= 0 && temp < end) {
				result = temp;
			}else {
				break;
			}
		}
		return result;
	}
	
	@Override
	public boolean validate(Parser parser, ClassModel model, String... values) {
		int methodEnd = parser.methodBodyIndexOf(Parser.METHOD_END, pos);
		if(lastFound!=null) {
			methodEnd = getLast(parser, lastFound, offset, methodEnd);
		}
		int max=-1;
		if (offset >= 0) {
			for(String item : search) {
				int temp = parser.search(item, offset);
				if(temp>max && temp < methodEnd) {
					max = temp;
				}
			}
		} else {
			for(String item : search) {
				int temp = parser.search(item);
				if(temp>max && temp < methodEnd) {
					max = temp;
				}
			}
		}
		if(max<0) {
			this.offset = methodEnd;
			return true;
		}
		return false;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy